Skip to content

Commit

Permalink
Create a CachedHashCodeArrayMap subclass to speed up EngineKey hashing.
Browse files Browse the repository at this point in the history
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
paulsowden authored and sjudd committed Feb 12, 2018
1 parent 2d0bc2d commit 7664c82
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
3 changes: 2 additions & 1 deletion library/src/main/java/com/bumptech/glide/load/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
import android.support.annotation.Nullable;
import android.support.v4.util.ArrayMap;
import android.support.v4.util.SimpleArrayMap;
import com.bumptech.glide.util.CachedHashCodeArrayMap;
import java.security.MessageDigest;

/**
* A set of {@link Option Options} to apply to in memory and disk cache keys.
*/
public final class Options implements Key {
private final ArrayMap<Option<?>, Object> values = new ArrayMap<>();
private final ArrayMap<Option<?>, Object> values = new CachedHashCodeArrayMap<>();

public void putAll(@NonNull Options other) {
values.putAll((SimpleArrayMap<Option<?>, Object>) other.values);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
import com.bumptech.glide.load.resource.gif.GifDrawableTransformation;
import com.bumptech.glide.load.resource.gif.GifOptions;
import com.bumptech.glide.signature.EmptySignature;
import com.bumptech.glide.util.CachedHashCodeArrayMap;
import com.bumptech.glide.util.Preconditions;
import com.bumptech.glide.util.Util;
import java.util.HashMap;
import java.util.Map;

/**
Expand Down Expand Up @@ -106,7 +106,7 @@ public class RequestOptions implements Cloneable {
@NonNull
private Options options = new Options();
@NonNull
private Map<Class<?>, Transformation<?>> transformations = new HashMap<>();
private Map<Class<?>, Transformation<?>> transformations = new CachedHashCodeArrayMap<>();
@NonNull
private Class<?> resourceClass = Object.class;
private boolean isLocked;
Expand Down Expand Up @@ -822,7 +822,7 @@ public RequestOptions clone() {
RequestOptions result = (RequestOptions) super.clone();
result.options = new Options();
result.options.putAll(options);
result.transformations = new HashMap<>();
result.transformations = new CachedHashCodeArrayMap<>();
result.transformations.putAll(transformations);
result.isLocked = false;
result.isAutoCloneEnabled = false;
Expand Down
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;
}
}

0 comments on commit 7664c82

Please sign in to comment.