Once upon a time, use of Guava was generally frowned upon in Android apps developed at Google, as Guava was designed, tested, benchmarked, and optimized for the conditions found in server Java development. This situation was suboptimal: Android developers found themselves reinventing wheels, awkwardly working around API gaps, and their best practices diverged from best practices for other Java development.
Over 2017, we worked extensively to change this situation: to optimize a branch of Guava for suitability for Android, in collaboration with Google's Android experts. We converged on the following set of priorities:
As a result of this work, Google's internal Android best practices have gone from forbidding the use of Guava to actively recommending key parts of Guava.
Even after this work, not all of Guava is recommended for use on Android. We focused our optimization work on some key data structures and APIs. Loosely, we recommend against the following:
cache
(prefer LruCache
instead)eventbus
(uses reflection)reflect
(uses reflection)graph
(uses expensive data structures, usually not worth it)collect.Table
(uses expensive data structures, usually not worth it)This work consisted of lots of nitty gritty optimization work, but we can pick out some high points. First, we made significant strides on reducing code size footprint of many types, with most of that effort focused on common.collect
. The below chart shows the before and after JAR sizes of several key APIs, with all measurements taken after appropriate ProGuard stripping.
We found many tricks for reducing code size, but the most important were
This work often went hand in hand with rewriting of underlying data structures, especially in common.collect
. Our primary goals for our data structures were:
LinkedHashMap
, etc.)ArrayMap
, ArraySet
, even though those data structures gave up O(1) asymptotics)This required rewriting many of our core data structures in the Android fork, especially requiring us to avoid entry objects wherever possible. For example, ImmutableMap
on Android is now backed by a flattened hash table built of parallel arrays, with the hash table storing indexes into another array rather than storing pointers to entry objects. Similarly, we developed algorithmic tweaks allowing ImmutableSortedMap
to sort the keys and values together without combining them into Entry
objects as would typically be required.
Generally speaking, we did not achieve data structures that were as compact as Android's ArrayMap
and ArraySet
, but we came close while still providing the O(1) asymptotics we expect from these structures.
Other data structures, unique to Guava, showed similar improvements even if they did not have competition. For example:
Data structure Bytes/entry (before) Bytes/entry (after)HashBiMap
64 40 HashMultiset
59 24 LinkedHashMultiset
67 32
We did, in some cases, accept asymptotic slowdowns to view collections where workarounds were easily available. For example, few users know -- or care -- that ImmutableSet.asList()
returned a "magical" ImmutableList
with an O(1) contains
implementation. Eliminating the specialized implementation in the Android branch allowed us to make significant code size savings, and the workaround of just calling contains
on the ImmutableSet
directly is easy enough.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.3