On Wed, 9 Apr 2025 17:25:02 GMT, Rémi Forax <fo...@openjdk.org> wrote:

>> 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 +...

@forax The same problem can be observed using pure unmodifiable maps so there 
is a problem elsewhere:


    private static final Map<String, VarHandle> U_MAP = Map.of(
            "x", VH_X,
            "y", VH_Y);
            
...

    @Benchmark
    public int confinedUnmodifiableMapLoop() {
        var sum = 0;
        for(var i = 0; i < 512; i++) {
            var x = (int) U_MAP.get("x").get(confined, 0L, (long) i);
            var y = (int) U_MAP.get("y").get(confined, 0L, (long) i);
            sum += x +y;
        }
        return sum;
    }            
            


I will take a look at it.

-------------

PR Comment: https://git.openjdk.org/jdk/pull/23972#issuecomment-2792664301

Reply via email to