diff options
author | 2015-08-26 16:52:49 +0000 | |
---|---|---|
committer | 2015-08-27 14:44:49 +0000 | |
commit | 88bee7bdbb0690e96e0a03797898ad31646f62a7 (patch) | |
tree | c41b85ed33ed39c7ef82a5c60c588c6fd6ee3cbe /src/main/java/com/google/devtools/build/lib | |
parent | 4e6bf64a8f958edf8e0d2b0f78c1520e7523f375 (diff) |
Use a concurrent LoadingCache for SkylarkType.Simple as opposed to having to hold a global lock in order to get a skylark type.
--
MOS_MIGRATED_REVID=101585120
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/syntax/SkylarkType.java | 35 |
1 files changed, 21 insertions, 14 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/SkylarkType.java b/src/main/java/com/google/devtools/build/lib/syntax/SkylarkType.java index 62ffcab801..020cc8c7bf 100644 --- a/src/main/java/com/google/devtools/build/lib/syntax/SkylarkType.java +++ b/src/main/java/com/google/devtools/build/lib/syntax/SkylarkType.java @@ -15,6 +15,9 @@ package com.google.devtools.build.lib.syntax; import com.google.common.base.Joiner; import com.google.common.base.Preconditions; +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Interner; @@ -28,7 +31,6 @@ import java.lang.reflect.Type; import java.lang.reflect.WildcardType; import java.util.ArrayList; import java.util.Arrays; -import java.util.HashMap; import java.util.List; import java.util.Map; @@ -244,19 +246,16 @@ public abstract class SkylarkType { @Override public boolean canBeCastTo(Class<?> type) { return this.type == type || super.canBeCastTo(type); } - private static HashMap<Class<?>, Simple> simpleCache = new HashMap<>(); - /** - * The public way to create a Simple type - * @param type a Class - * @return the Simple type that contains exactly the instances of that Class - */ - // NB: synchronized to avoid race conditions filling that cache. - public static synchronized Simple of(Class<?> type) { - Simple cached = simpleCache.get(type); - if (cached != null) { - return cached; - } + private static LoadingCache<Class<?>, Simple> simpleCache = CacheBuilder.newBuilder() + .build(new CacheLoader<Class<?>, Simple>() { + @Override + public Simple load(Class<?> type) { + return create(type); + } + }); + + private static Simple create(Class<?> type) { Simple simple; if (type == Object.class) { // Note that this is a bad encoding for "anything", not for "everything", i.e. @@ -275,9 +274,17 @@ public abstract class SkylarkType { simple = new Simple(type); } } - simpleCache.put(type, simple); return simple; } + + /** + * The public way to create a Simple type + * @param type a Class + * @return the Simple type that contains exactly the instances of that Class + */ + public static Simple of(Class<?> type) { + return simpleCache.getUnchecked(type); + } } /** Combination of a generic type and an argument type */ |