You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2020/02/18 22:34:00 UTC

[groovy] branch danielsun/tweak-resolving updated: Warmup the cache of resolve visitor

This is an automated email from the ASF dual-hosted git repository.

sunlan pushed a commit to branch danielsun/tweak-resolving
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/danielsun/tweak-resolving by this push:
     new 045ea6b  Warmup the cache of resolve visitor
045ea6b is described below

commit 045ea6b61e0cb01832cb02f064e27bd6380b1c60
Author: Daniel Sun <su...@apache.org>
AuthorDate: Wed Feb 19 06:33:44 2020 +0800

    Warmup the cache of resolve visitor
---
 .../codehaus/groovy/control/ResolveVisitor.java    | 51 ++++++++++++++++++++++
 .../groovy/vmplugin/v9/ClassFinderTest.groovy      | 31 +++++++++++++
 2 files changed, 82 insertions(+)

diff --git a/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java b/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java
index 07c3c88..0eda13a 100644
--- a/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java
+++ b/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java
@@ -18,6 +18,7 @@
  */
 package org.codehaus.groovy.control;
 
+import groovy.lang.GroovySystem;
 import groovy.lang.Tuple2;
 import org.apache.groovy.ast.tools.ExpressionUtils;
 import org.codehaus.groovy.GroovyBugError;
@@ -63,16 +64,22 @@ 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.DefaultGroovyMethods;
 import org.codehaus.groovy.runtime.memoize.EvictableCache;
 import org.codehaus.groovy.runtime.memoize.UnlimitedConcurrentCache;
 import org.codehaus.groovy.syntax.Types;
 import org.codehaus.groovy.transform.trait.Traits;
+import org.codehaus.groovy.vmplugin.VMPluginFactory;
+import org.codehaus.groovy.vmplugin.v9.ClassFinder;
 import org.objectweb.asm.Opcodes;
 
 import java.lang.annotation.Annotation;
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
 import java.lang.reflect.Modifier;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.LinkedHashMap;
@@ -84,6 +91,7 @@ import java.util.Objects;
 import java.util.Set;
 import java.util.function.BiConsumer;
 import java.util.function.Predicate;
+import java.util.stream.Collectors;
 
 import static groovy.lang.Tuple.tuple;
 import static org.codehaus.groovy.ast.tools.ClosureUtils.getParametersSafe;
@@ -636,6 +644,49 @@ public class ResolveVisitor extends ClassCodeExpressionTransformer {
     }
 
     private static final EvictableCache<String, Set<String>> DEFAULT_IMPORT_CLASS_AND_PACKAGES_CACHE = new UnlimitedConcurrentCache<>();
+    static {
+        if (VMPluginFactory.getPlugin().getVersion() >= 9) {
+            try {
+                initCache();
+            } catch (Exception e) {
+                System.err.println("[WARNING] The default import cache of resolve visitor failed to be initialized");
+            }
+        }
+    }
+
+    private static void initCache() {
+        URI gsLocation = null;
+        try {
+            gsLocation = DefaultGroovyMethods.getLocation(GroovySystem.class).toURI();
+        } catch (URISyntaxException e) {
+            e.printStackTrace();
+        }
+
+        for (String prefix : DEFAULT_IMPORTS) {
+            String pn = prefix.substring(0, prefix.length() - 1).replace('.', '/');
+
+            Map<String, Set<String>> map = Collections.emptyMap();
+            if (pn.startsWith("java/")) {
+                map = ClassFinder.find(URI.create("jrt:/modules/java.base/"), pn);
+            } else if (pn.startsWith("groovy/")) {
+                if (null != gsLocation) {
+                    map = ClassFinder.find(gsLocation, pn);
+                }
+            }
+
+            map = map.entrySet().stream()
+                    .collect(
+                            Collectors.toMap(
+                                    Map.Entry::getKey,
+                                    entry -> entry.getValue().stream()
+                                                .map(e -> e.replace('/', '.') + ".")
+                                                .collect(Collectors.toSet())
+                            )
+                    );
+
+            DEFAULT_IMPORT_CLASS_AND_PACKAGES_CACHE.putAll(map);
+        }
+    }
 
     protected boolean resolveFromDefaultImports(final ClassNode type, final String[] packagePrefixes) {
         String typeName = type.getName();
diff --git a/src/test/org/codehaus/groovy/vmplugin/v9/ClassFinderTest.groovy b/src/test/org/codehaus/groovy/vmplugin/v9/ClassFinderTest.groovy
index 01de50c..f8c159d 100644
--- a/src/test/org/codehaus/groovy/vmplugin/v9/ClassFinderTest.groovy
+++ b/src/test/org/codehaus/groovy/vmplugin/v9/ClassFinderTest.groovy
@@ -23,12 +23,25 @@ import org.junit.Test
 class ClassFinderTest {
     @Test
     void findGroovyClass() {
+        println "GroovySystem.location.toURI(): ${GroovySystem.location.toURI()}"
         Map<String, Set<String>> result = ClassFinder.find(GroovySystem.location.toURI(), "groovy/lang")
         assert ["groovy/lang"] == result.get("GString")?.toList()
         assert null == result.get("GroovydocHolder")
     }
 
     @Test
+    void findGroovyClass2() {
+        Map<String, Set<String>> result = ClassFinder.find(GroovySystem.location.toURI(), "groovy/util")
+        assert ["groovy/util"] == result.get("NodeBuilder")?.toList()
+    }
+
+    @Test
+    void findGroovyClass3() {
+        Map<String, Set<String>> result = ClassFinder.find(org.codehaus.groovy.control.ResolveVisitor.location.toURI(), "org/codehaus/groovy/control")
+        assert ["org/codehaus/groovy/control"] == result.get("ResolveVisitor")?.toList()
+    }
+
+    @Test
     void findGroovyClassRecursive() {
         Map<String, Set<String>> result = ClassFinder.find(GroovySystem.location.toURI(), "groovy/lang", true)
         assert ["groovy/lang"] == result.get("GString")?.toList()
@@ -43,6 +56,24 @@ class ClassFinderTest {
     }
 
     @Test
+    void findJavaClass2() {
+        Map<String, Set<String>> result = ClassFinder.find(URI.create("jrt:/modules/java.base/"), "java/util")
+        assert ["java/util"] == result.get("Map")?.toList()
+    }
+
+    @Test
+    void findJavaClass3() {
+        Map<String, Set<String>> result = ClassFinder.find(URI.create("jrt:/modules/java.base/"), "java/io")
+        assert ["java/io"] == result.get("InputStream")?.toList()
+    }
+
+    @Test
+    void findJavaClass4() {
+        Map<String, Set<String>> result = ClassFinder.find(URI.create("jrt:/modules/java.base/"), "java/net")
+        assert ["java/net"] == result.get("Inet4Address")?.toList()
+    }
+
+    @Test
     void findJavaClassRecursive() {
         Map<String, Set<String>> result = ClassFinder.find(URI.create("jrt:/modules/java.base/"), "java/lang", true)
         assert ["java/lang/invoke"] == result.get("MethodHandle")?.toList()