Skip to content

Use SparseArrayCompat for better performance#108

Open
isfaaghyth wants to merge 2 commits into
woltapp:masterfrom
isfaaghyth:patch-1
Open

Use SparseArrayCompat for better performance#108
isfaaghyth wants to merge 2 commits into
woltapp:masterfrom
isfaaghyth:patch-1

Conversation

@isfaaghyth
Copy link
Copy Markdown

SparseArray can be used to replace HashMap when the key is a primitive type. There are some variants for different key/value types, even though not all of them are publicly available. let's take a look at the comparison:

SparseIntArray:

class SparseIntArray {
    int[] keys;
    int[] values;
    int size;
}
Class = 12 + 3 * 4 = 24 bytes
Array = 20 + 1000 * 4 = 4024 bytes
Total = 8,072 bytes

HashMap:

class HashMap<K, V> {
    Entry<K, V>[] table;
    Entry<K, V> forNull;
    int size;
    int modCount;
    int threshold;
    Set<K> keys
    Set<Entry<K, V>> entries;
    Collection<V> values;
}
Class = 12 + 8 * 4 = 48 bytes
Entry = 32 + 16 + 16 = 64 bytes
Array = 20 + 1000 * 64 = 64024 bytes
Total = 64,136 bytes

read more: https://speakerdeck.com/romainguy/android-memories?slide=90

SparseArray can be used to replace HashMap when the key is a primitive type. There are some variants for different key/value types, even though not all of them are publicly available. (read more: https://speakerdeck.com/romainguy/android-memories?slide=90)
Comment thread Kotlin/lib/src/main/java/com/wolt/blurhashkt/BlurHashDecoder.kt Outdated
mrousavy added a commit to mrousavy/react-native-blurhash that referenced this pull request Feb 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants