daniellansun commented on code in PR #2822:
URL: https://github.com/apache/groovy/pull/2822#discussion_r3834984396
##########
src/main/java/org/codehaus/groovy/vmplugin/v8/Selector.java:
##########
@@ -1533,4 +1533,42 @@ private static Class<?> getThisType(Class<?> sender) {
}
return sender;
}
+
+ /**
+ * Builds a same-class guard that avoids {@code
asCollector(Object[].class, n)}
+ * for the common 1–4 argument shapes (receiver plus 0–3 parameters).
+ * The produced handle has type {@code (pt)boolean} and returns {@code
false}
+ * if any argument is {@code null} or has a different runtime class.
+ */
+ static MethodHandle sameClassesGuard(final Object[] args, final Class<?>[]
pt) {
+ int n = pt.length;
+ MethodType booleanType = MethodType.methodType(boolean.class, pt);
+ if (n == 0) {
+ return MethodHandles.constant(boolean.class, true);
+ }
+ if (n == 1) {
+ return SAME_CLASS.bindTo(args[0].getClass()).asType(booleanType);
+ }
+ if (n == 2) {
+ return SAME_CLASSES_2
+ .bindTo(args[0].getClass()).bindTo(args[1].getClass())
+ .asType(booleanType);
+ }
+ if (n == 3) {
+ return SAME_CLASSES_3
+
.bindTo(args[0].getClass()).bindTo(args[1].getClass()).bindTo(args[2].getClass())
+ .asType(booleanType);
+ }
+ if (n == 4) {
+ return SAME_CLASSES_4
+ .bindTo(args[0].getClass()).bindTo(args[1].getClass())
+ .bindTo(args[2].getClass()).bindTo(args[3].getClass())
+ .asType(booleanType);
+ }
+ Class<?>[] classes = new Class<?>[n];
+ for (int i = 0; i < n; i++) {
+ classes[i] = args[i].getClass();
+ }
+ return SAME_CLASSES.bindTo(classes).asCollector(Object[].class,
n).asType(booleanType);
+ }
Review Comment:
Thanks, Jochen — that is the better shape, and it already matches how
`IndyCompoundAssign` binds its class guard.
`Selector.sameClassesGuard` now does:
```java
return MethodHandles.insertArguments(SAME_CLASS_GUARDS[n], 0, classes)
.asType(callType.changeReturnType(boolean.class));
```
`SAME_CLASS_GUARDS` is `{null, SAME_CLASS, SAME_CLASSES_2, SAME_CLASSES_3,
SAME_CLASSES_4}`.
One adapter at arity 4 instead of four. Link-time only.
`SameClassesGuardMhBench.specialised_bindTo_arity4` keeps the old chained
`bindTo` so we can see if the single `insertArguments` actually moves the
needle. On this host the isolated arity-4 combinator is **0.99×**
(`insertArguments` vs chained `bindTo`, CIs overlap). Production uses
`insertArguments` for the idiomatic one-adapter shape; the end-to-end win
against the collector parent is unchanged (arity 4 still **4.68×**).
--
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]