RFR: 8351565: Implement JEP 502: Stable Values (Preview) [v51]
Rémi Forax
forax at openjdk.org
Wed Apr 9 17:27:49 UTC 2025
On Mon, 7 Apr 2025 16:00:41 GMT, Per Minborg <pminborg at openjdk.org> wrote:
>> Implement JEP 502.
>>
>> The PR passes tier1-tier3 tests.
>
> Per Minborg has updated the pull request incrementally with one additional commit since the last revision:
>
> Fix typo in return type
Hello,
I do not know if you know but StableValue.map() does not seems to be optimized correctly.
Here is the benchmark i use:
@Warmup(iterations = 5, time = 2, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 5, time = 2, timeUnit = TimeUnit.SECONDS)
@Fork(value = 1, jvmArgs = { "--enable-preview" })
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Benchmark)
public class StableValueBenchmarks {
private static final MemoryLayout LAYOUT = MemoryLayout.structLayout(
ValueLayout.JAVA_INT.withName("x"),
ValueLayout.JAVA_INT.withName("y")
);
private static final long SIZEOF = LAYOUT.byteSize();
private static final long OFFSET_X = LAYOUT.byteOffset(groupElement("x"));
private static final long OFFSET_Y = LAYOUT.byteOffset(groupElement("y"));
private static final VarHandle VH_X = LAYOUT.arrayElementVarHandle(groupElement("x"))
.withInvokeExactBehavior();
private static final VarHandle VH_Y = LAYOUT.arrayElementVarHandle(groupElement("y"))
.withInvokeExactBehavior();
private static final Supplier<VarHandle> SV_X = StableValue.supplier(
() -> LAYOUT.arrayElementVarHandle(groupElement("x")).withInvokeExactBehavior());
private static final Supplier<VarHandle> SV_Y = StableValue.supplier(
() -> LAYOUT.arrayElementVarHandle(groupElement("y")).withInvokeExactBehavior());
private static final Map<String, VarHandle> SMAP = StableValue.map(
Set.of("x", "y"),
name -> LAYOUT.arrayElementVarHandle(groupElement(name)).withInvokeExactBehavior());
private final MemorySegment confined;
{
var array = new int[512 * (int) SIZEOF / (int) ValueLayout.JAVA_INT.byteSize()];
var heap = MemorySegment.ofArray(array);
for(var i = 0; i < 512; i++) {
heap.set(ValueLayout.JAVA_INT, i * SIZEOF + OFFSET_X, i);
heap.set(ValueLayout.JAVA_INT, i * SIZEOF + OFFSET_Y, i);
}
confined = Arena.ofConfined().allocate(LAYOUT, 512);
confined.copyFrom(heap);
}
@Benchmark
public int confinedVarHandleLoop() {
var sum = 0;
for(var i = 0; i < 512; i++) {
var x = (int) VH_X.get(confined, 0L, (long) i);
var y = (int) VH_Y.get(confined, 0L, (long) i);
sum += x +y;
}
return sum;
}
@Benchmark
public int confinedStableValueLoop() {
var sum = 0;
for(var i = 0; i < 512; i++) {
var x = (int) SV_X.get().get(confined, 0L, (long) i);
var y = (int) SV_Y.get().get(confined, 0L, (long) i);
sum += x +y;
}
return sum;
}
@Benchmark
public int confinedStableMapLoop() {
var sum = 0;
for(var i = 0; i < 512; i++) {
var x = (int) SMAP.get("x").get(confined, 0L, (long) i);
var y = (int) SMAP.get("y").get(confined, 0L, (long) i);
sum += x +y;
}
return sum;
}
}
Results for Stable.map() are way of:
Benchmark Mode Cnt Score Error Units
StableValueBenchmarks.confinedStableMapLoop avgt 5 21832,999 ± 50,422 ns/op
StableValueBenchmarks.confinedStableValueLoop avgt 5 144,546 ± 0,640 ns/op
StableValueBenchmarks.confinedVarHandleLoop avgt 5 146,320 ± 1,789 ns/op
-------------
PR Comment: https://git.openjdk.org/jdk/pull/23972#issuecomment-2790451306
More information about the core-libs-dev
mailing list