Skip to content

[GR-64594] Bytecode DSL: Improve Builder Performance. #11321

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions truffle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ This changelog summarizes major changes between Truffle versions relevant to lan
## Version 25.0
* GR-31495 Added ability to specify language and instrument specific options using `Source.Builder.option(String, String)`. Languages may describe available source options by implementing `TruffleLanguage.getSourceOptionDescriptors()` and `TruffleInstrument.getSourceOptionDescriptors()` respectively.
* GR-61493 Added `RootNode.prepareForCall` which allows root nodes to prepare themselves for use as a call target (or to validate whether they can be used as a call target).
* GR-57063 Bytecode-DSL: Improved variadic support. Variadic operands compile and execute now more efficiently with fewer reallocations.
* GR-57063 Bytecode-DSL: Added an `startOffset` parameter to `@Variadic` which allows to reserve a fixed number of slots in the object array for custom use.
* GR-57063 Bytecode-DSL: The `@Variadic` annotation can now also be used on `@Operation` annotated classes to indicate that the operation produces a dynamic variadic return value. Dynamic variadic return values are efficiently flattened into the object array of a variadic operand.
* GR-57063 Bytecode-DSL: Added a `variadicStackLimit` parameter to `@GenerateBytecode` that allows to specify how many variable arguments are stored on the stack before they are collapsed into an object array.
* GR-57063 Bytecode DSL: Improved variadic support. Variadic operands compile and execute now more efficiently with fewer reallocations.
* GR-57063 Bytecode DSL: Added an `startOffset` parameter to `@Variadic` which allows to reserve a fixed number of slots in the object array for custom use.
* GR-57063 Bytecode DSL: The `@Variadic` annotation can now also be used on `@Operation` annotated classes to indicate that the operation produces a dynamic variadic return value. Dynamic variadic return values are efficiently flattened into the object array of a variadic operand.
* GR-57063 Bytecode DSL: Added a `variadicStackLimit` parameter to `@GenerateBytecode` that allows to specify how many variable arguments are stored on the stack before they are collapsed into an object array.
* GR-50017 TruffleStringIterator.NextNode and TruffleStringIterator.PreviousNode now require the iterated string's encoding as a parameter for performance reasons.
* GR-63075 Java host interop again inherits public method methods from non-public base classes if public access is enabled. This was originally changed in 24.1.
* GR-63201 Added `TruffleLanguage.Registration.optionalResources` and `TruffleInstrument.Registration.optionalResources` attributes to support optional resources which implementations are not available at the runtime. Optional resources, if omitted at runtime, can still be used as long as their resource path is specified via the `polyglot.engine.resourcePath.<componentId>` system property.
Expand All @@ -25,6 +25,9 @@ This changelog summarizes major changes between Truffle versions relevant to lan
* GR-64488 Added `TruffleFile#getFileStoreInfo()` providing access to disk-related metadata such as total size, usable space, unallocated space and block size.
* GR-43908 Added a deoptimization cycle detection to Truffle. This feature is enabled by default when running optimized on JDK 25 and later. Whenever a deoptimization cycle is detected, the compilation fails with a permanent bailout that contains the Java stack trace of the location. If this error is encountered too frequently it can be disabled by setting `compiler.DeoptCycleDetectionThreshold` to `-1`. For further information, please see the extended [deopt cycle detection](https://github.com/oracle/graal/blob/master/truffle/docs/Optimizing.md#automatic-detection-of-deoptimization-cycles) guide.
* Added `compiler.DeoptCycleDetectionThreshold` and `compiler.DeoptCycleDetectionAllowedRepeats` options which allow to fine-tune the sensitivity of the deoptimization loop detection.
* GR-64594 Bytecode DSL: Improved builder performance. Creating bytecode nodes with Bytecode DSL is now 40% faster and requires 5 times less temporary memory.
* GR-64594 Bytecode DSL: Builder instances now have a `toString()` implementation that prints current operations as well as the instructions that have been already emitted. This should make it easier to debug problems with builder usage.
* GR-64594 Bytecode DSL: Added `@GenerateBytecode(..., additionalAssertions=true)` to enable additional assertions for Bytecode DSL implementation bugs. This feature can also be enabled with `-A.truffle.dsl.AdditionalAssertions=true` at Java source compile time. These assertions are intentionally disabled by default, as they can lead to slow-downs even when assertions are disabled.

## Version 24.2.0
* GR-60636 Truffle now stops compiling when the code cache fills up on HotSpot. A warning is printed when that happens.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
/*
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
*
* Subject to the condition set forth below, permission is hereby granted to any
* person obtaining a copy of this software, associated documentation and/or
* data (collectively the "Software"), free of charge and under any and all
* copyright rights in the Software, and any and all patent rights owned or
* freely licensable by each licensor hereunder covering either (i) the
* unmodified Software as contributed to or provided by such licensor, or (ii)
* the Larger Works (as defined below), to deal in both
*
* (a) the Software, and
*
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
* one is included with the Software each a "Larger Work" to which the Software
* is contributed by such licensors),
*
* without restriction, including without limitation the rights to copy, create
* derivative works of, display, perform, and distribute the Software and make,
* use, sell, offer for sale, import, export, have made, and have sold the
* Software and the Larger Work(s), and to sublicense the foregoing rights on
* either these or other terms.
*
* This license is subject to the following condition:
*
* The above copyright notice and either this complete permission notice or at a
* minimum a reference to the UPL must be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.oracle.truffle.api.bytecode.test;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertThrows;

import java.util.stream.IntStream;

import org.junit.Test;

import com.oracle.truffle.api.bytecode.BytecodeSupport.ConstantsBuffer;

public class ConstantsBufferTest {

@Test
public void linearPathDeduplicatesBelowThreshold() {
ConstantsBuffer b = new ConstantsBuffer();
int a = b.add("A");
int a2 = b.add(new String("A"));
assertEquals(a, a2);
assertEquals(1, b.materialize().length);
b.clear();
}

@Test
public void migrationHappensExactlyAtThreshold() {
ConstantsBuffer b = new ConstantsBuffer();
IntStream.range(0, 8).forEach(i -> b.add(i));
int dup = b.add(3);
assertEquals(3, dup);
assertEquals(8, b.materialize().length);
b.clear();
}

@Test
public void mapDeduplicatesAndRehashes() {
ConstantsBuffer b = new ConstantsBuffer();
int n = 1_000;
for (int i = 0; i < n; i++) {
b.add(i);
}
for (int i = 0; i < n; i++) {
assertEquals(i, b.add(i));
}
assertEquals(n, b.materialize().length);
b.clear();
}

@Test
public void addNullAllocatesIndependentSlots() {
ConstantsBuffer b = new ConstantsBuffer();
int i1 = b.addNull();
int i2 = b.addNull();
assertNotEquals(i1, i2);
assertArrayEquals(new Object[]{null, null},
b.materialize());
b.clear();
}

@Test
public void materialiseResetsForReuse() {
ConstantsBuffer b = new ConstantsBuffer();
b.add("X");
b.materialize();
assertEquals(0, b.materialize().length);
b.add("Y");
assertEquals(1, b.materialize().length);
b.clear();
}

@Test
public void clearShrinksLargeBuffer() {
ConstantsBuffer b = new ConstantsBuffer();
IntStream.range(0, 600).forEach(b::add);
b.materialize();
// should trigger down size
b.clear();
b.add("Z");
assertEquals(1, b.materialize().length);
}

@Test
public void collisionStress() {
ConstantsBuffer b = new ConstantsBuffer();
for (int i = 0; i < 10_000; i++) {
b.add(new Colliding(i));
}
assertEquals(10_000, b.materialize().length);
b.clear();
}

@Test
public void addRejectsNull() {
ConstantsBuffer b = new ConstantsBuffer();
assertThrows(NullPointerException.class, () -> b.add(null));
assertEquals(0, b.materialize().length);
b.clear();
}

@Test
public void clearWithoutMaterialiseFails() {
ConstantsBuffer b = new ConstantsBuffer();
b.add("A");
assertThrows(IllegalStateException.class, b::clear);
assertEquals(1, b.materialize().length);
b.clear();
}

@Test
public void manyNulls1() {
ConstantsBuffer b = new ConstantsBuffer();
assertEquals(0, b.add(42));
IntStream.range(0, 600).forEach(i -> b.addNull());
assertEquals(0, b.add(42));

assertEquals(601, b.materialize().length);
b.clear();
}

@Test
public void manyNulls2() {
ConstantsBuffer b = new ConstantsBuffer();
IntStream.range(0, 8).forEach(i -> b.add(i));
IntStream.range(0, 600).forEach(i -> b.addNull());
for (int i = 0; i < 8; i++) {
assertEquals(i, b.add(i));
}
assertEquals(608, b.materialize().length);
b.clear();
}

/** distinct objects that all share the same hashCode to stress collision handling. */
private static final class Colliding {
final int id;

Colliding(int id) {
this.id = id;
}

@Override
public boolean equals(Object o) {
return o instanceof Colliding c && c.id == id;
}

@Override
public int hashCode() {
return 42;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
*/
@GenerateBytecodeTestVariants({
@Variant(suffix = "Base", configuration = @GenerateBytecode(languageClass = BytecodeDSLTestLanguage.class, //
additionalAssertions = true, //
enableYield = true, //
enableMaterializedLocalAccesses = true, //
enableSerialization = true, //
Expand All @@ -110,13 +111,15 @@
allowUnsafe = false, //
variadicStackLimit = "4")),
@Variant(suffix = "Unsafe", configuration = @GenerateBytecode(languageClass = BytecodeDSLTestLanguage.class, //
additionalAssertions = true, //
enableYield = true, //
enableMaterializedLocalAccesses = true, //
enableSerialization = true, //
enableTagInstrumentation = true, //
enableSpecializationIntrospection = true, //
variadicStackLimit = "8")),
@Variant(suffix = "WithUncached", configuration = @GenerateBytecode(languageClass = BytecodeDSLTestLanguage.class, //
additionalAssertions = true, //
enableYield = true, //
enableMaterializedLocalAccesses = true, //
enableSerialization = true, //
Expand All @@ -126,6 +129,7 @@
enableSpecializationIntrospection = true, //
variadicStackLimit = "16")),
@Variant(suffix = "WithBE", configuration = @GenerateBytecode(languageClass = BytecodeDSLTestLanguage.class, //
additionalAssertions = true, //
enableYield = true, //
enableMaterializedLocalAccesses = true, //
enableSerialization = true, //
Expand All @@ -134,6 +138,7 @@
boxingEliminationTypes = {boolean.class, long.class}, //
variadicStackLimit = "4")),
@Variant(suffix = "WithOptimizations", configuration = @GenerateBytecode(languageClass = BytecodeDSLTestLanguage.class, //
additionalAssertions = true, //
enableYield = true, //
enableMaterializedLocalAccesses = true, //
enableSerialization = true, //
Expand All @@ -142,6 +147,7 @@
defaultLocalValue = "LOCAL_DEFAULT_VALUE", //
variadicStackLimit = "8")),
@Variant(suffix = "WithRootScoping", configuration = @GenerateBytecode(languageClass = BytecodeDSLTestLanguage.class, //
additionalAssertions = true, //
enableYield = true, //
enableMaterializedLocalAccesses = true, //
enableSerialization = true, //
Expand All @@ -151,6 +157,7 @@
defaultLocalValue = "LOCAL_DEFAULT_VALUE", //
variadicStackLimit = "16")),
@Variant(suffix = "WithStoreBytecodeIndexInFrame", configuration = @GenerateBytecode(languageClass = BytecodeDSLTestLanguage.class, //
additionalAssertions = true, //
enableYield = true, //
enableMaterializedLocalAccesses = true, //
enableSerialization = true, //
Expand All @@ -163,6 +170,7 @@
variadicStackLimit = "4")),
// A typical "production" configuration with all of the bells and whistles.
@Variant(suffix = "ProductionBlockScoping", configuration = @GenerateBytecode(languageClass = BytecodeDSLTestLanguage.class, //
additionalAssertions = true, //
enableYield = true, //
enableMaterializedLocalAccesses = true, //
enableSerialization = true, //
Expand All @@ -173,6 +181,7 @@
boxingEliminationTypes = {boolean.class, long.class}, //
variadicStackLimit = "8")),
@Variant(suffix = "ProductionRootScoping", configuration = @GenerateBytecode(languageClass = BytecodeDSLTestLanguage.class, //
additionalAssertions = true, //
enableYield = true, //
enableMaterializedLocalAccesses = true, //
enableSerialization = true, //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,7 @@ public void testTryFinallyNestedEarlyExitsInFinally() {
b.endTryFinally();
b.endRoot();
});

assertEquals(null, root.getCallTarget().call(false));
assertEquals(42L, root.getCallTarget().call(true));
assertHandlers(root,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ public void testMaterializedAccessUpdatesTag() {
assertEquals("x", x.getName());
assertNull(x.getTypeProfile());

// Force cached.
// force cached
outer.getBytecodeNode().setUncachedThreshold(0);

assertEquals(42L, outer.getCallTarget().call(false));
Expand Down
13 changes: 13 additions & 0 deletions truffle/src/com.oracle.truffle.api.bytecode/snapshot.sigtest
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ hfds parser

CLSS public final com.oracle.truffle.api.bytecode.BytecodeSupport
innr public final static CloneReferenceList
innr public final static ConstantsBuffer
supr java.lang.Object

CLSS public final static com.oracle.truffle.api.bytecode.BytecodeSupport$CloneReferenceList<%0 extends java.lang.Object>
Expand All @@ -194,6 +195,17 @@ meth public void forEach(java.util.function.Consumer<{com.oracle.truffle.api.byt
supr java.lang.Object
hfds references,size

CLSS public final static com.oracle.truffle.api.bytecode.BytecodeSupport$ConstantsBuffer
outer com.oracle.truffle.api.bytecode.BytecodeSupport
cons public init()
meth public int add(java.lang.Object)
meth public int addNull()
meth public java.lang.Object[] create()
meth public java.lang.Object[] materialize()
meth public void clear()
supr java.lang.Object
hfds EMPTY,EMPTY_ARRAY,HASH_THRESHOLD,INITIAL_CAPACITY,MAX_CAPACITY,constants,keys,maxSize,size,values

CLSS public final !enum com.oracle.truffle.api.bytecode.BytecodeTier
fld public final static com.oracle.truffle.api.bytecode.BytecodeTier CACHED
fld public final static com.oracle.truffle.api.bytecode.BytecodeTier UNCACHED
Expand Down Expand Up @@ -289,6 +301,7 @@ CLSS public abstract interface !annotation com.oracle.truffle.api.bytecode.Gener
anno 0 java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy value=SOURCE)
anno 0 java.lang.annotation.Target(java.lang.annotation.ElementType[] value=[TYPE])
intf java.lang.annotation.Annotation
meth public abstract !hasdefault boolean additionalAssertions()
meth public abstract !hasdefault boolean allowUnsafe()
meth public abstract !hasdefault boolean enableBlockScoping()
meth public abstract !hasdefault boolean enableBytecodeDebugListener()
Expand Down
Loading