-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a CachedHashCodeArrayMap subclass to speed up EngineKey hashing.
EngineKey is hashed frequently and caches its own hashCode, however for every request a new EngineKey is created and it rehashes the options and transformations. It's common to cache RequestBuilders for generating multiple requests that have the same options so a handy optimisation is to use an ArrayMap subclass that caches the hashCode to prevent recomputing it every time. This is one of the more expensive atoms in starting a glide request. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=185029279
- Loading branch information
1 parent
2d0bc2d
commit 7664c82
Showing
3 changed files
with
53 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
library/src/main/java/com/bumptech/glide/util/CachedHashCodeArrayMap.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.bumptech.glide.util; | ||
|
||
import android.support.v4.util.ArrayMap; | ||
import android.support.v4.util.SimpleArrayMap; | ||
|
||
/** An {@link ArrayMap} that caches its hashCode to support efficient lookup. */ | ||
public final class CachedHashCodeArrayMap<K, V> extends ArrayMap<K, V> { | ||
|
||
private int hashCode; | ||
|
||
@Override | ||
public void clear() { | ||
hashCode = 0; | ||
super.clear(); | ||
} | ||
|
||
@Override | ||
public V setValueAt(int index, V value) { | ||
hashCode = 0; | ||
return super.setValueAt(index, value); | ||
} | ||
|
||
@Override | ||
public V put(K key, V value) { | ||
hashCode = 0; | ||
return super.put(key, value); | ||
} | ||
|
||
@Override | ||
public void putAll(SimpleArrayMap<? extends K, ? extends V> simpleArrayMap) { | ||
hashCode = 0; | ||
super.putAll(simpleArrayMap); | ||
} | ||
|
||
@Override | ||
public V removeAt(int index) { | ||
hashCode = 0; | ||
return super.removeAt(index); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
if (hashCode == 0) { | ||
hashCode = super.hashCode(); | ||
} | ||
return hashCode; | ||
} | ||
} |