Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_5_X c58023cd9 -> dd988bf40


Minor refactoring: Use `ConcurrentCommonCache` to cache resolved type and 
packages

(cherry picked from commit 8fc0e4be4e03a4dd4d82c18d47e613e78d22ca85)


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/dd988bf4
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/dd988bf4
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/dd988bf4

Branch: refs/heads/GROOVY_2_5_X
Commit: dd988bf4099a32e6472fa6ee00a3c83eff965318
Parents: c58023c
Author: Daniel Sun <sun...@apache.org>
Authored: Sun Aug 12 00:26:02 2018 +0800
Committer: Daniel Sun <sun...@apache.org>
Committed: Sun Aug 12 00:27:04 2018 +0800

----------------------------------------------------------------------
 .../codehaus/groovy/control/ResolveVisitor.java    | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/dd988bf4/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java 
b/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java
index 30d847c..b5f108f 100644
--- a/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java
+++ b/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java
@@ -59,6 +59,8 @@ import org.codehaus.groovy.ast.stmt.CatchStatement;
 import org.codehaus.groovy.ast.stmt.ForStatement;
 import org.codehaus.groovy.ast.stmt.Statement;
 import org.codehaus.groovy.control.ClassNodeResolver.LookupResult;
+import org.codehaus.groovy.runtime.memoize.ConcurrentCommonCache;
+import org.codehaus.groovy.runtime.memoize.EvictableCache;
 import org.codehaus.groovy.syntax.Types;
 import org.codehaus.groovy.transform.trait.Traits;
 import org.objectweb.asm.Opcodes;
@@ -502,7 +504,7 @@ public class ResolveVisitor extends 
ClassCodeExpressionTransformer {
         return false;
     }
 
-    private static final Map<String, Set<String>> 
DEFAULT_IMPORT_CLASS_AND_PACKAGES_MAP = new HashMap<>();
+    private static final EvictableCache<String, Set<String>> 
DEFAULT_IMPORT_CLASS_AND_PACKAGES_CACHE = new ConcurrentCommonCache<>();
 
     private boolean resolveFromDefaultImports(final ClassNode type, final 
String[] packagePrefixes) {
         final String typeName = type.getName();
@@ -519,11 +521,12 @@ public class ResolveVisitor extends 
ClassCodeExpressionTransformer {
                 type.setRedirect(tmp.redirect());
 
                 if (DEFAULT_IMPORTS == packagePrefixes) { // Only the 
non-cached type and packages should be cached
-                    Set<String> packagePrefixSet = 
DEFAULT_IMPORT_CLASS_AND_PACKAGES_MAP.get(typeName);
-                    if (null == packagePrefixSet) {
-                        packagePrefixSet = new HashSet<>(2);
-                        DEFAULT_IMPORT_CLASS_AND_PACKAGES_MAP.put(typeName, 
packagePrefixSet);
-                    }
+                    Set<String> packagePrefixSet = 
DEFAULT_IMPORT_CLASS_AND_PACKAGES_CACHE.getAndPut(typeName, new 
ConcurrentCommonCache.ValueProvider<String, Set<String>>() {
+                        @Override
+                        public Set<String> provide(String key) {
+                            return new HashSet<>(2);
+                        }
+                    });
                     packagePrefixSet.add(packagePrefix);
                 }
 
@@ -544,7 +547,7 @@ public class ResolveVisitor extends 
ClassCodeExpressionTransformer {
         final String typeName = type.getName();
 
         if (testDefaultImports) {
-            Set<String> packagePrefixSet = 
DEFAULT_IMPORT_CLASS_AND_PACKAGES_MAP.get(typeName);
+            Set<String> packagePrefixSet = 
DEFAULT_IMPORT_CLASS_AND_PACKAGES_CACHE.get(typeName);
             if (null != packagePrefixSet) {
                 // if the type name was resolved before, we can try the 
successfully resolved packages first, which are much less and very likely 
successful to resolve.
                 // As a result, we can avoid trying other default import 
packages and further resolving, which can improve the resolving performance to 
some extent.

Reply via email to