You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2022/11/18 09:49:04 UTC

[groovy] branch GROOVY_4_0_X updated: GROOVY-10772: Possible memory leak, CacheableCallSite retains objects across invocations (additional tweaks)

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

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


The following commit(s) were added to refs/heads/GROOVY_4_0_X by this push:
     new 941569a922 GROOVY-10772: Possible memory leak, CacheableCallSite retains objects across invocations (additional tweaks)
941569a922 is described below

commit 941569a922e00cfdda9ef19d0e1d746bf34cca91
Author: Paul King <pa...@asert.com.au>
AuthorDate: Fri Nov 18 16:50:22 2022 +1000

    GROOVY-10772: Possible memory leak, CacheableCallSite retains objects across invocations (additional tweaks)
---
 .../v8/IndyGuardsFiltersAndSignatures.java         | 12 +++--
 .../org/codehaus/groovy/vmplugin/v8/Selector.java  | 51 ++++++++++++++--------
 2 files changed, 37 insertions(+), 26 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/vmplugin/v8/IndyGuardsFiltersAndSignatures.java b/src/main/java/org/codehaus/groovy/vmplugin/v8/IndyGuardsFiltersAndSignatures.java
index 2ab2767124..97ed448290 100644
--- a/src/main/java/org/codehaus/groovy/vmplugin/v8/IndyGuardsFiltersAndSignatures.java
+++ b/src/main/java/org/codehaus/groovy/vmplugin/v8/IndyGuardsFiltersAndSignatures.java
@@ -78,7 +78,7 @@ public class IndyGuardsFiltersAndSignatures {
     static {
         try {
             SAME_CLASS = LOOKUP.findStatic(IndyGuardsFiltersAndSignatures.class, "sameClass", MethodType.methodType(boolean.class, Class.class, Object.class));
-            SAME_CLASSES = LOOKUP.findStatic(IndyGuardsFiltersAndSignatures.class, "sameClasses", MethodType.methodType(boolean.class, Object[].class, Object[].class));
+            SAME_CLASSES = LOOKUP.findStatic(IndyGuardsFiltersAndSignatures.class, "sameClasses", MethodType.methodType(boolean.class, Class[].class, Object[].class));
             SAME_MC = LOOKUP.findStatic(IndyGuardsFiltersAndSignatures.class, "isSameMetaClass", MethodType.methodType(boolean.class, MetaClass.class, Object.class));
             IS_NULL = LOOKUP.findStatic(IndyGuardsFiltersAndSignatures.class, "isNull", OBJECT_GUARD);
             UNWRAP_METHOD = LOOKUP.findStatic(IndyGuardsFiltersAndSignatures.class, "unwrap", OBJECT_FILTER);
@@ -200,17 +200,15 @@ public class IndyGuardsFiltersAndSignatures {
 
     /**
      * Guard to check if the provided objects have the same
-     * class as the other provided objects.
+     * class as the provided classes.
      */
-    public static boolean sameClasses(Object[] cs, Object[] os) {
+    public static boolean sameClasses(Class<?>[] cs, Object[] os) {
         for (int i = 0; i < cs.length; i++) {
-            Object c = cs[i];
-            if (null == c) continue;
-
+            Class<?> c = cs[i];
             Object o = os[i];
             if (null == o) return false;
 
-            if (o.getClass() != c.getClass())
+            if (o.getClass() != c)
                 return false;
         }
         return true;
diff --git a/src/main/java/org/codehaus/groovy/vmplugin/v8/Selector.java b/src/main/java/org/codehaus/groovy/vmplugin/v8/Selector.java
index 9dde46177e..be9cc77fbf 100644
--- a/src/main/java/org/codehaus/groovy/vmplugin/v8/Selector.java
+++ b/src/main/java/org/codehaus/groovy/vmplugin/v8/Selector.java
@@ -932,26 +932,39 @@ public abstract class Selector {
 
             // guards for receiver and parameter
             Class<?>[] pt = handle.type().parameterArray();
-            for (int i = 0; i < args.length; i++) {
-                Object arg = args[i];
-                Class<?> paramType = pt[i];
-                MethodHandle test;
-
-                if (arg == null) {
-                    test = IS_NULL.asType(MethodType.methodType(boolean.class, paramType));
-                    if (LOG_ENABLED) LOG.info("added null argument check at pos " + i);
-                } else {
-                    Class<?> argClass = arg.getClass();
-                    if (paramType.isPrimitive()) continue;
-                    //if (Modifier.isFinal(argClass.getModifiers()) && TypeHelper.argumentClassIsParameterClass(argClass,pt[i])) continue;
-                    test = SAME_CLASS.
-                            bindTo(argClass).
-                            asType(MethodType.methodType(boolean.class, paramType));
-                    if (LOG_ENABLED) LOG.info("added same class check at pos " + i);
+            if (Arrays.stream(args).anyMatch(arg -> null == arg)) {
+                for (int i = 0; i < args.length; i++) {
+                    Object arg = args[i];
+                    Class<?> paramType = pt[i];
+                    MethodHandle test;
+
+                    if (arg == null) {
+                        test = IS_NULL.asType(MethodType.methodType(boolean.class, paramType));
+                        if (LOG_ENABLED) LOG.info("added null argument check at pos " + i);
+                    } else {
+                        if (Modifier.isFinal(paramType.getModifiers())) {
+                            // primitive types are also `final`
+                            continue;
+                        }
+                        test = SAME_CLASS.
+                                bindTo(arg.getClass()).
+                                asType(MethodType.methodType(boolean.class, paramType));
+                        if (LOG_ENABLED) LOG.info("added same class check at pos " + i);
+                    }
+                    Class<?>[] drops = new Class[i];
+                    System.arraycopy(pt, 0, drops, 0, drops.length);
+                    test = MethodHandles.dropArguments(test, 0, drops);
+                    handle = MethodHandles.guardWithTest(test, handle, fallback);
+                }
+            } else if (Arrays.stream(pt).anyMatch(paramType -> !Modifier.isFinal(paramType.getModifiers()))) {
+                // Avoid guards as much as possible
+                Class<?>[] argClasses = new Class[args.length];
+                for (int i = 0; i < args.length; i++) {
+                    argClasses[i] = args[i].getClass();
                 }
-                Class<?>[] drops = new Class[i];
-                System.arraycopy(pt, 0, drops, 0, drops.length);
-                test = MethodHandles.dropArguments(test, 0, drops);
+                MethodHandle test = SAME_CLASSES.bindTo(argClasses)
+                        .asCollector(Object[].class, pt.length)
+                        .asType(MethodType.methodType(boolean.class, pt));
                 handle = MethodHandles.guardWithTest(test, handle, fallback);
             }
         }