groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject [groovy] branch master updated: GROOVY-9111: Reduce the cost of transforming meta method(closes #925)
Date Fri, 10 May 2019 13:47:21 GMT
This is an automated email from the ASF dual-hosted git repository.

sunlan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/master by this push:
     new 7753124  GROOVY-9111: Reduce the cost of transforming meta method(closes #925)
7753124 is described below

commit 7753124d05ef0b390853bb775afd25a0694bc3d1
Author: Daniel Sun <sunlan@apache.org>
AuthorDate: Fri May 10 21:47:03 2019 +0800

    GROOVY-9111: Reduce the cost of transforming meta method(closes #925)
---
 src/main/groovy/groovy/lang/MetaClassImpl.java                     | 2 +-
 .../java/org/codehaus/groovy/runtime/callsite/CallSiteHelper.java  | 7 ++++++-
 src/main/java/org/codehaus/groovy/vmplugin/VMPlugin.java           | 7 +++++--
 src/main/java/org/codehaus/groovy/vmplugin/v5/Java5.java           | 7 -------
 src/main/java/org/codehaus/groovy/vmplugin/v9/Java9.java           | 5 ++++-
 5 files changed, 16 insertions(+), 12 deletions(-)

diff --git a/src/main/groovy/groovy/lang/MetaClassImpl.java b/src/main/groovy/groovy/lang/MetaClassImpl.java
index 171bf7c..84c806e 100644
--- a/src/main/groovy/groovy/lang/MetaClassImpl.java
+++ b/src/main/groovy/groovy/lang/MetaClassImpl.java
@@ -1276,7 +1276,7 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
         }
 
         if (method != null) {
-            MetaMethod transformedMetaMethod = CallSiteHelper.transformMetaMethod(this, method,
MetaClassHelper.convertToTypeArray(arguments), MetaClassImpl.class);
+            MetaMethod transformedMetaMethod = CallSiteHelper.transformMetaMethod(this, method,
() -> MetaClassHelper.convertToTypeArray(arguments), MetaClassImpl.class);
             return transformedMetaMethod.doMethodInvoke(object, arguments);
         } else {
             return invokePropertyOrMissing(object, methodName, originalArguments, fromInsideClass,
isCallToSuper);
diff --git a/src/main/java/org/codehaus/groovy/runtime/callsite/CallSiteHelper.java b/src/main/java/org/codehaus/groovy/runtime/callsite/CallSiteHelper.java
index 016c537..1d6cdef 100644
--- a/src/main/java/org/codehaus/groovy/runtime/callsite/CallSiteHelper.java
+++ b/src/main/java/org/codehaus/groovy/runtime/callsite/CallSiteHelper.java
@@ -23,11 +23,16 @@ import groovy.lang.MetaMethod;
 import org.codehaus.groovy.vmplugin.VMPlugin;
 import org.codehaus.groovy.vmplugin.VMPluginFactory;
 
+import java.util.function.Supplier;
+
 public class CallSiteHelper {
     private static final VMPlugin VM_PLUGIN = VMPluginFactory.getPlugin();
 
     public static MetaMethod transformMetaMethod(MetaClass metaClass, MetaMethod metaMethod,
Class<?>[] params, Class<?> caller) {
-        return VM_PLUGIN.transformMetaMethod(metaClass, metaMethod, params, caller);
+        return transformMetaMethod(metaClass, metaMethod, () -> params, caller);
     }
 
+    public static MetaMethod transformMetaMethod(MetaClass metaClass, MetaMethod metaMethod,
Supplier<Class<?>[]> paramsSupplier, Class<?> caller) {
+        return VM_PLUGIN.transformMetaMethod(metaClass, metaMethod, paramsSupplier, caller);
+    }
 }
diff --git a/src/main/java/org/codehaus/groovy/vmplugin/VMPlugin.java b/src/main/java/org/codehaus/groovy/vmplugin/VMPlugin.java
index 2848615..170d5a1 100644
--- a/src/main/java/org/codehaus/groovy/vmplugin/VMPlugin.java
+++ b/src/main/java/org/codehaus/groovy/vmplugin/VMPlugin.java
@@ -26,6 +26,7 @@ import org.codehaus.groovy.ast.CompileUnit;
 
 import java.lang.reflect.AccessibleObject;
 import java.lang.reflect.Method;
+import java.util.function.Supplier;
 
 /**
  * Interface to access VM version based actions.
@@ -88,9 +89,11 @@ public interface VMPlugin {
      *
      * @param metaClass meta class
      * @param metaMethod the original meta method
-     * @param params parameter types
+     * @param paramsSupplier parameter types supplier
      * @param caller caller type
      * @return the transformed meta method
      */
-    MetaMethod transformMetaMethod(MetaClass metaClass, MetaMethod metaMethod, Class<?>[]
params, Class<?> caller);
+    default MetaMethod transformMetaMethod(MetaClass metaClass, MetaMethod metaMethod, Supplier<Class<?>[]>
paramsSupplier, Class<?> caller) {
+        return metaMethod;
+    }
 }
diff --git a/src/main/java/org/codehaus/groovy/vmplugin/v5/Java5.java b/src/main/java/org/codehaus/groovy/vmplugin/v5/Java5.java
index b8182d1..58c6ea3 100644
--- a/src/main/java/org/codehaus/groovy/vmplugin/v5/Java5.java
+++ b/src/main/java/org/codehaus/groovy/vmplugin/v5/Java5.java
@@ -18,8 +18,6 @@
  */
 package org.codehaus.groovy.vmplugin.v5;
 
-import groovy.lang.MetaClass;
-import groovy.lang.MetaMethod;
 import org.codehaus.groovy.GroovyBugError;
 import org.codehaus.groovy.ast.AnnotatedNode;
 import org.codehaus.groovy.ast.AnnotationNode;
@@ -584,11 +582,6 @@ public class Java5 implements VMPlugin {
         }
     }
 
-    @Override
-    public MetaMethod transformMetaMethod(MetaClass metaClass, MetaMethod metaMethod, Class<?>[]
params, Class<?> caller) {
-        return metaMethod;
-    }
-
     private static final Permission ACCESS_PERMISSION = new ReflectPermission("suppressAccessChecks");
 }
 
diff --git a/src/main/java/org/codehaus/groovy/vmplugin/v9/Java9.java b/src/main/java/org/codehaus/groovy/vmplugin/v9/Java9.java
index 882d900..c348121 100644
--- a/src/main/java/org/codehaus/groovy/vmplugin/v9/Java9.java
+++ b/src/main/java/org/codehaus/groovy/vmplugin/v9/Java9.java
@@ -50,6 +50,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Optional;
 import java.util.Set;
+import java.util.function.Supplier;
 import java.util.stream.Collectors;
 
 /**
@@ -172,7 +173,7 @@ public class Java9 extends Java8 {
     }
 
     @Override
-    public MetaMethod transformMetaMethod(MetaClass metaClass, MetaMethod metaMethod, Class<?>[]
params, Class<?> caller) {
+    public MetaMethod transformMetaMethod(MetaClass metaClass, MetaMethod metaMethod, Supplier<Class<?>[]>
paramsSupplier, Class<?> caller) {
         if (!(metaMethod instanceof CachedMethod)) {
             return metaMethod;
         }
@@ -203,6 +204,7 @@ public class Java9 extends Java8 {
             classList.add(theClass);
             classList.addAll(superclassList);
 
+            Class<?>[] params = paramsSupplier.get();
             for (Class<?> sc : classList) {
                 Optional<MetaMethod> optionalMetaMethod = getAccessibleMetaMethod(metaMethod,
params, caller, sc);
                 if (optionalMetaMethod.isPresent()) {
@@ -219,6 +221,7 @@ public class Java9 extends Java8 {
         // e.g. StringBuilder sb = new StringBuilder(); sb.setLength(0);
         // `setLength` is the method of `AbstractStringBuilder`, which is `package-private`
         if (declaringClass.isAssignableFrom(theClass)) {
+            Class<?>[] params = paramsSupplier.get();
             Optional<MetaMethod> optionalMetaMethod = getAccessibleMetaMethod(metaMethod,
params, caller, theClass);
             if (optionalMetaMethod.isPresent()) {
                 return optionalMetaMethod.get();


Mime
View raw message