blackdrag commented on code in PR #2790:
URL: https://github.com/apache/groovy/pull/2790#discussion_r3790709178


##########
src/main/java/groovy/lang/Closure.java:
##########
@@ -594,6 +586,59 @@ public V call(final Object... arguments) {
         }
     }
 
+    /**
+     * Invokes a cached {@code doCall}/{@code call} target. Prefers the adapted
+     * {@link MethodHandle} so {@code Method.invoke} is not on the GDK
+     * {@code each}/{@code collect} hot path. Exceptions thrown by the body —
+     * including a body that itself throws {@link InvocationTargetException} or
+     * {@link IllegalAccessException} — are rethrown as-is on the handle path;
+     * the {@link Method#invoke} fallback unwraps only the wrapper
+     * {@link InvocationTargetException} that reflection introduces.
+     */
+    @SuppressWarnings("unchecked")
+    private static <V> V invokeCached(final MethodHandle handle, final Method 
target, final Closure<?> self, final Object[] arguments) {
+        if (handle != null) {
+            try {
+                return (V) invokeHandle(handle, self, arguments);
+            } catch (Throwable t) {
+                UncheckedThrow.rethrow(t);
+                return null;
+            }
+        }
+        try {
+            return (V) target.invoke(self, arguments);
+        } catch (InvocationTargetException ite) {
+            UncheckedThrow.rethrow(ite.getCause());
+            return null; // unreachable statement
+        } catch (IllegalAccessException iae) {
+            throw new GroovyRuntimeException(iae);
+        }
+    }
+
+    /**
+     * {@code invokeExact} against a handle adapted to
+     * {@link MethodType#genericMethodType(int) genericMethodType(arity+1)}
+     * (fixed-arity {@code Object} receiver and arguments, {@code Object} 
return).
+     * Cases {@code 0..ARITY_LIMIT-1} match that type exactly; the spreader
+     * is the type-correct fallback if the limit grows without a matching case.
+     */
+    private static Object invokeHandle(final MethodHandle handle, final 
Closure<?> self, final Object[] arguments) throws Throwable {
+        switch (arguments.length) {
+            case 0:
+                return handle.invokeExact((Object) self);
+            case 1:
+                return handle.invokeExact((Object) self, arguments[0]);
+            case 2:
+                return handle.invokeExact((Object) self, arguments[0], 
arguments[1]);
+            case 3:
+                return handle.invokeExact((Object) self, arguments[0], 
arguments[1], arguments[2]);
+            case 4:
+                return handle.invokeExact((Object) self, arguments[0], 
arguments[1], arguments[2], arguments[3]);
+            default:
+                return handle.asSpreader(Object[].class, 
arguments.length).invokeExact((Object) self, arguments);

Review Comment:
   I think you have no advantage of invokeExact in this case, unless you cache 
the handle resulting from asSpreader. If not cached I would use 
invokeWithArguments here instead. I doubt it is slower for this case, but it 
sure is better readable.



##########
src/main/java/groovy/lang/Closure.java:
##########
@@ -1593,7 +1650,26 @@ static CallOverride lookup(Class<?> type) {
                 }
                 any = true;
             }
-            return any ? new CallOverride(byArity, guards, callForm) : NONE;
+            if (!any) {
+                return NONE;
+            }
+            MethodHandle[] handles = new MethodHandle[ARITY_LIMIT];
+            for (int arity = 0; arity < ARITY_LIMIT; arity += 1) {
+                if (byArity[arity] != null) {
+                    handles[arity] = unreflect(byArity[arity]);
+                }
+            }

Review Comment:
   the cases over arity limit could be created  here as well, but with taking 
Object[]



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to