Skip to content

GROOVY-8699: SC: emit direct bytecode for simple list - #2395

Merged
eric-milles merged 2 commits into
masterfrom
GROOVY-8699
Mar 13, 2026
Merged

GROOVY-8699: SC: emit direct bytecode for simple list#2395
eric-milles merged 2 commits into
masterfrom
GROOVY-8699

Conversation

@eric-milles

Copy link
Copy Markdown
Member

Given a simple list expression like:

@CompileStatic test() {
 [5,6,7]
}

The class generator will produce a bytecode sequence like

list = new ArrayList(3); // list is top of operand stack
list.add(5);
list.add(6);
list.add(7);

There is a simpler implementation that can replace NewListExpression; I figured a new list followed by calls to add created the least extra stuff. This replaces ListExpression with new ArrayList(Arrays.asList(new Object[]{ /*list items*/ })):

            var array = new ArrayExpression(ClassHelper.OBJECT_TYPE, le.getExpressions().stream().map(scTransformer::transform).toList());

            ClassNode java_util_Arrays = ClassHelper.make(java.util.Arrays.class);
            mn = java_util_Arrays.getDeclaredMethods("asList").get(0);

            var values = new MethodCallExpression(new ClassExpression(java_util_Arrays), "asList", array);
            values.setMethodTarget(mn);

            ClassNode java_util_ArrayList = ClassHelper.makeWithoutCaching(java.util.ArrayList.class);
            mn = java_util_ArrayList.getDeclaredConstructor(new Parameter[] {new Parameter(ClassHelper.COLLECTION_TYPE,"c")});

            var cce = new ConstructorCallExpression(java_util_ArrayList, values);
            cce.putNodeMetaData(StaticTypesMarker.DIRECT_METHOD_CALL_TARGET, mn);
            cce.setSourcePosition(le);
            return cce;

@codecov-commenter

codecov-commenter commented Mar 11, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.93939% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 66.7574%. Comparing base (50f267d) to head (acde6d6).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
...rg/codehaus/groovy/classgen/AsmClassGenerator.java 50.0000% 0 Missing and 1 partial ⚠️
...orm/sc/transformers/ListExpressionTransformer.java 96.7742% 0 Missing and 1 partial ⚠️
Additional details and impacted files

Impacted file tree graph

@@                Coverage Diff                 @@
##               master      #2395        +/-   ##
==================================================
+ Coverage     66.7508%   66.7574%   +0.0066%     
- Complexity      29866      29869         +3     
==================================================
  Files            1382       1382                
  Lines          116111     116134        +23     
  Branches        20472      20476         +4     
==================================================
+ Hits            77505      77528        +23     
+ Misses          32274      32271         -3     
- Partials         6332       6335         +3     
Files with missing lines Coverage Δ
...rg/codehaus/groovy/classgen/AsmClassGenerator.java 84.6494% <50.0000%> (ø)
...orm/sc/transformers/ListExpressionTransformer.java 97.7778% <96.7742%> (-2.2222%) ⬇️

... and 6 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@eric-milles
eric-milles merged commit 7b18440 into master Mar 13, 2026
41 of 42 checks passed
@eric-milles
eric-milles deleted the GROOVY-8699 branch March 13, 2026 14:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants