<div dir="ltr"><div dir="ltr" data-smartmail="gmail_signature"><div dir="ltr"><div dir="ltr"><div>Hello,</div><div><br></div><div>I'm running some tests comparing the performance of the various methods available to set a value in a MemorySegment.</div><div><br></div><div>Based on a simple benchmark I wrote, VarHandle seems to be the slowest one, which is odd to me.</div><div><br></div><div>This is the output of the benchmark:</div><div><ul><li>Manual offset set; Total time 147<br></li><li>VarHandle; Total time 3197<br></li><li>Method Handle; Total time 767<br></li></ul></div><div>I'm testing this from a MacOS Air M2 (arm chip). I also ran other tests trying to warm up the virtual machine but I didn't get much improvements.</div><div><br></div><div>And this is the code. I didn't use microbenchmarking but the result wouldn't change:</div><div><font face="monospace">public static void main(String[] args) throws Throwable {<br>        long elementCount = 200_000_000;<br>        var testFile = Path.of("test.data");<br>        FileChannel fc = FileChannel.open(testFile,<br>                StandardOpenOption.CREATE,<br>                StandardOpenOption.READ,<br>                StandardOpenOption.WRITE<br>        );<br>        var segment = fc.map(FileChannel.MapMode.READ_WRITE, 0, elementCount * </font><span style="font-family:monospace">Integer.BYTES</span><span style="font-family:monospace">, MemorySession.global());</span></div><div><font face="monospace"><br>        MemoryLayout memoryLayout = MemoryLayout.sequenceLayout(<br>                elementCount, ValueLayout.JAVA_INT.withName("counter")<br>        );<br><br>        VarHandle varHandle = memoryLayout.varHandle(MemoryLayout.PathElement.sequenceElement());<br>        var arrayMethodHandle = memoryLayout.byteOffsetHandle(MemoryLayout.PathElement.sequenceElement());<br><br><br>        // Init file Manually<br>        for (int i = 0; i < elementCount; i++) {<br>            segment.set(ValueLayout.JAVA_INT, i * 4L, 99);<br>        }<br><br>        long startTime;<br><br><br>        startTime = System.currentTimeMillis();<br>        for (int i = 0; i < elementCount; i++) {<br>            segment.set(ValueLayout.JAVA_INT, i * Integer.BYTES, 33);<br>        }<br>        System.out.println("Manual offset set; Total time " + (System.currentTimeMillis() - startTime));<br><br><br>        startTime = System.currentTimeMillis();<br>        for (int i = 0; i < elementCount; i++) {<br>            varHandle.set(segment, (long) i, 10);<br>        }<br>        System.out.println("VarHandle; Total time " + (System.currentTimeMillis() - startTime));<br><br><br>        startTime = System.currentTimeMillis();<br>        for (int i = 0; i < elementCount; i++) {<br>            // User method handle to calculate offset<br>            long offset = (long) arrayMethodHandle.invokeExact((long) i);<br>            segment.set(ValueLayout.JAVA_INT, offset, 10);<br>        }<br>        System.out.println("Method Handle; Total time " + (System.currentTimeMillis() - startTime));<br><br>        fc.close();<br>        Files.delete(testFile);<br>    }</font><br></div></div></div></div></div>