Closed Bug 962968 Opened 11 years ago Closed 11 years ago

Consider using SparseArray instead of HashMap for numeric keys

Categories

(Firefox for Android Graveyard :: General, defect)

All
Android
defect
Not set
normal

Tracking

(Not tracked)

RESOLVED FIXED
Firefox 29

People

(Reporter: rnewman, Assigned: lucasr)

References

Details

Attachments

(6 files)

---- # Use optimized data containers Take advantage of optimized containers in the Android framework, such as SparseArray, SparseBooleanArray, and LongSparseArray. The generic HashMap implementation can be quite memory inefficient because it needs a separate entry object for every mapping. Additionally, the SparseArray classes are more efficient because they avoid the system's need to autobox the key and sometimes value (which creates yet another object or two per entry). And don't be afraid of dropping down to raw arrays when that makes sense. ---- mobile/android/base/favicons/decoders/ICODecoder.java 155: HashMap<Integer, IconDirectoryEntry> preferenceMap = new HashMap<Integer, IconDirectoryEntry>(); mobile/android/base/favicons/Favicons.java 65: private static final Map<Integer, LoadFaviconTask> sLoadTasks = Collections.synchronizedMap(new HashMap<Integer, LoadFaviconTask>()); mobile/android/base/GeckoJavaSampler.java 66: private Map<Integer,Sample[]> mSamples = new HashMap<Integer,Sample[]>(); mobile/android/base/home/PanelManager.java 54: private static final Map<Integer, RequestCallback> sCallbacks = Collections.synchronizedMap(new HashMap<Integer, RequestCallback>()); mobile/android/base/PrefsHelper.java 27: private static final Map<Integer, PrefHandler> sCallbacks = new HashMap<Integer, PrefHandler>(); mobile/android/base/util/ActivityResultHandlerMap.java 11: private Map<Integer, ActivityResultHandler> mMap = new HashMap<Integer, ActivityResultHandler>();
Assignee: nobody → lucasr.at.mozilla
Attachment #8364416 - Flags: review?(rnewman)
Attachment #8364418 - Flags: review?(rnewman)
Attachment #8364419 - Flags: review?(rnewman)
Attachment #8364420 - Flags: review?(rnewman)
Attachment #8364421 - Flags: review?(rnewman)
Attachment #8364417 - Flags: review?(rnewman)
Attachment #8364416 - Flags: review?(rnewman) → review+
Attachment #8364417 - Flags: review?(rnewman) → review+
Comment on attachment 8364418 [details] [diff] [review] Replace HashMap with SparseArray in GeckoJavaSampler (r=rnewman) Review of attachment 8364418 [details] [diff] [review]: ----------------------------------------------------------------- This is fine, but looking at the code it seems like GeckoJavaSampler should be rewritten to do something else -- ThreadLocals, maybe.
Attachment #8364418 - Flags: review?(rnewman) → review+
Attachment #8364419 - Flags: review?(rnewman) → review+
Attachment #8364420 - Flags: review?(rnewman) → review+
Attachment #8364421 - Flags: review?(rnewman) → review+
Thanks for tackling this so quickly, Lucas! Now we'll see how much of a saving it is…
Status: NEW → ASSIGNED
Any reason you stopped at those six examples, Richard? Ex: [1] [2] On that note, we can probably be more explicit about setting initial sizes too, such as in [1] where I imagine we have 6 results. [1]: https://mxr.mozilla.org/mozilla-central/source/mobile/android/base/home/TopSitesPanel.java#780 [2]: https://mxr.mozilla.org/mozilla-central/source/mobile/android/base/Tabs.java#50
Flags: needinfo?(rnewman)
(In reply to Michael Comella (:mcomella) from comment #11) > Any reason you stopped at those six examples, Richard? Ex: [1] [2] Didn't want to keep copy-pasting from grep :D > On that note, we can probably be more explicit about setting initial sizes > too, such as in [1] where I imagine we have 6 results. Concur. > [1]: > https://mxr.mozilla.org/mozilla-central/source/mobile/android/base/home/ > TopSitesPanel.java#780 Err, this is a HashMap<String, Bitmap>… > [2]: > https://mxr.mozilla.org/mozilla-central/source/mobile/android/base/Tabs. > java#50 HashMap is sometimes faster for lookup than SparseArray (depends on size); we should profile before attacking this kind of hot-path stuff. File two follow-ups?
Flags: needinfo?(rnewman)
(In reply to Richard Newman [:rnewman] from comment #12) > File two follow-ups? bug 963863 - initialCapacity bug 963873 - SparseArray (again)
Product: Firefox for Android → Firefox for Android Graveyard
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: