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 2026/07/31 03:09:39 UTC

(groovy) branch master updated: minor refactor: slight additions to fat-free benchmark

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

paulk-asert 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 b30bd9951c minor refactor: slight additions to fat-free benchmark
b30bd9951c is described below

commit b30bd9951c944e6caedf35408ab79c26ebfc238c
Author: Paul King <pa...@asert.com.au>
AuthorDate: Fri Jul 31 13:09:31 2026 +1000

    minor refactor: slight additions to fat-free benchmark
---
 .../org/apache/groovy/adhoc/FatFreeLambda.groovy   | 109 +++++++++++++++++++++
 .../apache/groovy/adhoc/FatFreeLambdaBench.java    |  31 ++++++
 2 files changed, 140 insertions(+)

diff --git a/subprojects/performance/src/jmh/groovy/org/apache/groovy/adhoc/FatFreeLambda.groovy b/subprojects/performance/src/jmh/groovy/org/apache/groovy/adhoc/FatFreeLambda.groovy
index 18fd2ebb2a..1dac5aa1c4 100644
--- a/subprojects/performance/src/jmh/groovy/org/apache/groovy/adhoc/FatFreeLambda.groovy
+++ b/subprojects/performance/src/jmh/groovy/org/apache/groovy/adhoc/FatFreeLambda.groovy
@@ -23,7 +23,9 @@ import groovy.transform.CompileStatic
 import org.apache.groovy.util.Closures
 import org.apache.groovy.util.Lambdas
 
+import java.util.function.BiFunction
 import java.util.function.BiPredicate
+import java.util.function.Function
 
 /**
  * Helper class for benchmarking the "fat-free lambda" idioms that landed in
@@ -56,6 +58,21 @@ import java.util.function.BiPredicate
  * references (C/D) are emitted as static methods and shared as singletons by
  * {@code LambdaMetafactory} (the GROOVY-11905 optimisation), giving zero
  * per-call lambda allocation.
+ * <p>
+ * Three further families give slight coverage to the fat-free twins added for
+ * non-predicate shapes and array receivers in the follow-up rollout. Each keeps
+ * a compact {@code A/C/D/E} variant set (capturing closure literal, singleton
+ * functional-interface lambda, singleton method reference, hand-written loop) —
+ * the {@code rcurry} / {@code curryWith} variants above are predicate-with-param
+ * specific and do not apply to these single-argument shapes:
+ * <ul>
+ *   <li><b>collect</b> — {@code collect(Iterable, Function)} mapping twin from
+ *       <b>GROOVY-12215</b> (maps each element to its length);</li>
+ *   <li><b>inject</b> — {@code inject(Iterable, init, BiFunction)} fold/reduce
+ *       twin from <b>GROOVY-12216</b> (folds the element lengths into a total);</li>
+ *   <li><b>arrayCollect</b> — the {@code collect(E[], Function)} array-receiver
+ *       twin from <b>GROOVY-12218</b> (same mapping over an {@code Object[]}).</li>
+ * </ul>
  */
 @CompileStatic
 class FatFreeLambda {
@@ -216,4 +233,96 @@ class FatFreeLambda {
         Closure<Boolean> c = Closures.curryWith(STARTS_WITH, prefix)
         data.count(c)
     }
+
+    // ===== collect family (mapping — GROOVY-12215 collect(Iterable, Function) twin) =====
+    // Maps each element to its length; a single-argument shape, so only the core
+    // A/C/D/E contrast applies (no rcurry / curryWith param to carry).
+
+    /** A: capturing closure literal — the classic collect idiom. */
+    @CompileDynamic
+    static List<Integer> collectCaptureClosure(List<String> data) {
+        data.collect { it.length() }
+    }
+
+    /** C: real DGM collect(Function) + stateless Function lambda (singleton via indy). */
+    static List<Integer> collectFunctionLambda(List<String> data) {
+        Function<String, Integer> f = (String s) -> s.length()
+        data.collect(f)
+    }
+
+    /** D: real DGM collect(Function) + unbound method reference (singleton via indy). */
+    static List<Integer> collectFunctionMethodRef(List<String> data) {
+        Function<String, Integer> ref = String::length
+        data.collect(ref)
+    }
+
+    /** E: baseline plain for-loop — lower bound. */
+    static List<Integer> collectBaseline(List<String> data) {
+        List<Integer> out = new ArrayList<>(data.size())
+        for (String s : data) out.add(s.length())
+        return out
+    }
+
+    // ===== inject family (fold/reduce — GROOVY-12216 inject(Iterable, init, BiFunction) twin) =====
+    // Folds the element lengths into a running total starting at 0. The initial-value
+    // form takes a BiFunction (acc, element) -> acc; A uses the classic inject(init, Closure).
+
+    /** A: capturing closure literal — the classic inject idiom. */
+    @CompileDynamic
+    static Integer injectCaptureClosure(List<String> data) {
+        data.inject(0) { acc, s -> acc + s.length() }
+    }
+
+    /** C: real DGM inject(init, BiFunction) + stateless BiFunction lambda (singleton via indy). */
+    static Integer injectBiFunctionLambda(List<String> data) {
+        BiFunction<Integer, String, Integer> f = (Integer acc, String s) -> acc + s.length()
+        data.inject(0, f)
+    }
+
+    /** D: real DGM inject(init, BiFunction) + method reference to a static folder (singleton via indy). */
+    static Integer injectBiFunctionMethodRef(List<String> data) {
+        BiFunction<Integer, String, Integer> ref = FatFreeLambda::addLength
+        data.inject(0, ref)
+    }
+
+    /** Static folding step referenced by {@link #injectBiFunctionMethodRef}. */
+    static Integer addLength(Integer acc, String s) {
+        acc + s.length()
+    }
+
+    /** E: baseline plain for-loop — lower bound. */
+    static Integer injectBaseline(List<String> data) {
+        int acc = 0
+        for (String s : data) acc += s.length()
+        return acc
+    }
+
+    // ===== arrayCollect family (GROOVY-12218 collect(E[], Function) array-receiver twin) =====
+    // The same mapping as the collect family but on an Object[] receiver, exercising
+    // the ArrayGroovyMethods fat-free twin added for arrays.
+
+    /** A: capturing closure literal — the classic array collect idiom. */
+    @CompileDynamic
+    static List<Integer> arrayCollectCaptureClosure(String[] data) {
+        data.collect { it.length() }
+    }
+
+    /** C: real array-DGM collect(Function) + stateless Function lambda (singleton via indy). */
+    static List<Integer> arrayCollectFunctionLambda(String[] data) {
+        Function<String, Integer> f = (String s) -> s.length()
+        data.collect(f)
+    }
+
+    /** D: real array-DGM collect(Function) + unbound method reference (singleton via indy). */
+    static List<Integer> arrayCollectFunctionMethodRef(String[] data) {
+        Function<String, Integer> ref = String::length
+        data.collect(ref)
+    }
+
+    /** E: baseline plain for-loop — lower bound. */
+    static List<Integer> arrayCollectBaseline(String[] data) {
+        List<Integer> out = new ArrayList<>(data.length)
+        for (String s : data) out.add(s.length())
+        return out
+    }
 }
diff --git a/subprojects/performance/src/jmh/groovy/org/apache/groovy/adhoc/FatFreeLambdaBench.java b/subprojects/performance/src/jmh/groovy/org/apache/groovy/adhoc/FatFreeLambdaBench.java
index bb7c829098..be0a6b4669 100644
--- a/subprojects/performance/src/jmh/groovy/org/apache/groovy/adhoc/FatFreeLambdaBench.java
+++ b/subprojects/performance/src/jmh/groovy/org/apache/groovy/adhoc/FatFreeLambdaBench.java
@@ -58,6 +58,13 @@ import java.util.concurrent.TimeUnit;
  * and {@code count} (a reducer that builds no collection — its {@code Predicate}
  * / {@code BiPredicate}+param overloads landed in the GROOVY-12054 follow-up).
  * <p>
+ * Three further families give slight coverage to the fat-free twins added for
+ * non-predicate shapes and array receivers in the follow-up rollout, each with a
+ * compact {@code A/C/D/E} subset (the {@code rcurry} / {@code curryWith} variants
+ * are predicate-with-param specific and do not apply): {@code collect} (mapping,
+ * GROOVY-12215), {@code inject} (fold/reduce, GROOVY-12216), and
+ * {@code arrayCollect} (the array-receiver mapping twin, GROOVY-12218).
+ * <p>
  * All variants perform a full traversal (prefixes never match) and identical
  * per-element work; only the closure / lambda machinery differs.  Prefix
  * values rotate through a pre-built array so capture sites must keep
@@ -84,6 +91,7 @@ public class FatFreeLambdaBench {
     private int size;
 
     private List<String> data;
+    private String[] dataArray;
     private String[] prefixes;
     private int idx;
 
@@ -93,6 +101,8 @@ public class FatFreeLambdaBench {
         for (int i = 0; i < size; i++) {
             data.add("word_" + i);
         }
+        // Same elements as an Object[] receiver for the arrayCollect family.
+        dataArray = data.toArray(new String[0]);
         // Prefixes that never match any element — forces full traversal in every variant.
         // Eight values so the rotate index is a cheap mask-and-increment.
         prefixes = new String[]{"z_4", "z_70", "z_300", "z_99", "z_500", "z_7", "z_888", "z_1"};
@@ -134,4 +144,25 @@ public class FatFreeLambdaBench {
     @Benchmark public Number countF_sharedRcurry()      { return FatFreeLambda.countSharedRcurry(data, nextPrefix()); }
     @Benchmark public Number countG_lambdasCurryWith()  { return FatFreeLambda.countLambdasCurryWith(data, nextPrefix()); }
     @Benchmark public Number countH_closuresCurryWith() { return FatFreeLambda.countClosuresCurryWith(data, nextPrefix()); }
+
+    // ----- collect (mapping twin; GROOVY-12215) — element -> length -----------------
+
+    @Benchmark public List<Integer> collectA_captureClosure()   { return FatFreeLambda.collectCaptureClosure(data); }
+    @Benchmark public List<Integer> collectC_functionLambda()   { return FatFreeLambda.collectFunctionLambda(data); }
+    @Benchmark public List<Integer> collectD_functionMethodRef(){ return FatFreeLambda.collectFunctionMethodRef(data); }
+    @Benchmark public List<Integer> collectE_baseline()         { return FatFreeLambda.collectBaseline(data); }
+
+    // ----- inject (fold/reduce twin; GROOVY-12216) — sum of lengths -----------------
+
+    @Benchmark public Integer injectA_captureClosure()     { return FatFreeLambda.injectCaptureClosure(data); }
+    @Benchmark public Integer injectC_biFunctionLambda()   { return FatFreeLambda.injectBiFunctionLambda(data); }
+    @Benchmark public Integer injectD_biFunctionMethodRef(){ return FatFreeLambda.injectBiFunctionMethodRef(data); }
+    @Benchmark public Integer injectE_baseline()           { return FatFreeLambda.injectBaseline(data); }
+
+    // ----- arrayCollect (array-receiver mapping twin; GROOVY-12218) — element -> length ---
+
+    @Benchmark public List<Integer> arrayCollectA_captureClosure()    { return FatFreeLambda.arrayCollectCaptureClosure(dataArray); }
+    @Benchmark public List<Integer> arrayCollectC_functionLambda()    { return FatFreeLambda.arrayCollectFunctionLambda(dataArray); }
+    @Benchmark public List<Integer> arrayCollectD_functionMethodRef() { return FatFreeLambda.arrayCollectFunctionMethodRef(dataArray); }
+    @Benchmark public List<Integer> arrayCollectE_baseline()          { return FatFreeLambda.arrayCollectBaseline(dataArray); }
 }