< prev index next >

test/micro/org/openjdk/bench/jdk/incubator/foreign/LoopOverNew.java

Print this page

 27 import jdk.incubator.foreign.SegmentAllocator;
 28 import jdk.incubator.foreign.ResourceScope;
 29 import org.openjdk.jmh.annotations.Benchmark;
 30 import org.openjdk.jmh.annotations.BenchmarkMode;
 31 import org.openjdk.jmh.annotations.Fork;
 32 import org.openjdk.jmh.annotations.Measurement;
 33 import org.openjdk.jmh.annotations.Mode;
 34 import org.openjdk.jmh.annotations.OutputTimeUnit;
 35 import org.openjdk.jmh.annotations.Setup;
 36 import org.openjdk.jmh.annotations.State;
 37 import org.openjdk.jmh.annotations.TearDown;
 38 import org.openjdk.jmh.annotations.Warmup;
 39 import sun.misc.Unsafe;
 40 
 41 import java.lang.invoke.VarHandle;
 42 import java.nio.ByteBuffer;
 43 import java.nio.ByteOrder;
 44 import java.util.concurrent.TimeUnit;
 45 
 46 import static jdk.incubator.foreign.MemoryLayout.PathElement.sequenceElement;
 47 import static jdk.incubator.foreign.MemoryLayouts.JAVA_INT;
 48 
 49 @BenchmarkMode(Mode.AverageTime)
 50 @Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 51 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 52 @State(org.openjdk.jmh.annotations.Scope.Thread)
 53 @OutputTimeUnit(TimeUnit.MILLISECONDS)
 54 @Fork(value = 3, jvmArgsAppend = { "--add-modules=jdk.incubator.foreign" })
 55 public class LoopOverNew {
 56 
 57     static final Unsafe unsafe = Utils.unsafe;
 58 
 59     static final int ELEM_SIZE = 1_000_000;
 60     static final int CARRIER_SIZE = (int)JAVA_INT.byteSize();
 61     static final int ALLOC_SIZE = ELEM_SIZE * CARRIER_SIZE;
 62     static final MemoryLayout ALLOC_LAYOUT = MemoryLayout.sequenceLayout(ELEM_SIZE, JAVA_INT);
 63 
 64     static final VarHandle VH_int = MemoryLayout.sequenceLayout(JAVA_INT).varHandle(int.class, sequenceElement());
 65 
 66     final ResourceScope scope = ResourceScope.newConfinedScope();
 67     final SegmentAllocator recyclingAlloc = SegmentAllocator.ofSegment(MemorySegment.allocateNative(ALLOC_LAYOUT, scope));
 68 
 69     @TearDown
 70     public void tearDown() throws Throwable {
 71         scope.close();
 72     }
 73 
 74     @Benchmark
 75     public void unsafe_loop() {
 76         long unsafe_addr = unsafe.allocateMemory(ALLOC_SIZE);
 77         for (int i = 0; i < ELEM_SIZE; i++) {
 78             unsafe.putInt(unsafe_addr + (i * CARRIER_SIZE) , i);
 79         }
 80         unsafe.freeMemory(unsafe_addr);
 81     }
 82 
 83     @Benchmark
 84     public void segment_loop_confined() {
 85         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
 86             MemorySegment segment = MemorySegment.allocateNative(ALLOC_SIZE, 4, scope);
 87             for (int i = 0; i < ELEM_SIZE; i++) {

 27 import jdk.incubator.foreign.SegmentAllocator;
 28 import jdk.incubator.foreign.ResourceScope;
 29 import org.openjdk.jmh.annotations.Benchmark;
 30 import org.openjdk.jmh.annotations.BenchmarkMode;
 31 import org.openjdk.jmh.annotations.Fork;
 32 import org.openjdk.jmh.annotations.Measurement;
 33 import org.openjdk.jmh.annotations.Mode;
 34 import org.openjdk.jmh.annotations.OutputTimeUnit;
 35 import org.openjdk.jmh.annotations.Setup;
 36 import org.openjdk.jmh.annotations.State;
 37 import org.openjdk.jmh.annotations.TearDown;
 38 import org.openjdk.jmh.annotations.Warmup;
 39 import sun.misc.Unsafe;
 40 
 41 import java.lang.invoke.VarHandle;
 42 import java.nio.ByteBuffer;
 43 import java.nio.ByteOrder;
 44 import java.util.concurrent.TimeUnit;
 45 
 46 import static jdk.incubator.foreign.MemoryLayout.PathElement.sequenceElement;
 47 import static jdk.incubator.foreign.ValueLayout.JAVA_INT;
 48 
 49 @BenchmarkMode(Mode.AverageTime)
 50 @Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 51 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 52 @State(org.openjdk.jmh.annotations.Scope.Thread)
 53 @OutputTimeUnit(TimeUnit.MILLISECONDS)
 54 @Fork(value = 3, jvmArgsAppend = { "--add-modules=jdk.incubator.foreign" })
 55 public class LoopOverNew {
 56 
 57     static final Unsafe unsafe = Utils.unsafe;
 58 
 59     static final int ELEM_SIZE = 1_000_000;
 60     static final int CARRIER_SIZE = (int)JAVA_INT.byteSize();
 61     static final int ALLOC_SIZE = ELEM_SIZE * CARRIER_SIZE;
 62     static final MemoryLayout ALLOC_LAYOUT = MemoryLayout.sequenceLayout(ELEM_SIZE, JAVA_INT);
 63 
 64     static final VarHandle VH_int = MemoryLayout.sequenceLayout(JAVA_INT).varHandle(sequenceElement());
 65 
 66     final ResourceScope scope = ResourceScope.newConfinedScope();
 67     final SegmentAllocator recyclingAlloc = SegmentAllocator.prefixAllocator(MemorySegment.allocateNative(ALLOC_LAYOUT, scope));
 68 
 69     @TearDown
 70     public void tearDown() throws Throwable {
 71         scope.close();
 72     }
 73 
 74     @Benchmark
 75     public void unsafe_loop() {
 76         long unsafe_addr = unsafe.allocateMemory(ALLOC_SIZE);
 77         for (int i = 0; i < ELEM_SIZE; i++) {
 78             unsafe.putInt(unsafe_addr + (i * CARRIER_SIZE) , i);
 79         }
 80         unsafe.freeMemory(unsafe_addr);
 81     }
 82 
 83     @Benchmark
 84     public void segment_loop_confined() {
 85         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
 86             MemorySegment segment = MemorySegment.allocateNative(ALLOC_SIZE, 4, scope);
 87             for (int i = 0; i < ELEM_SIZE; i++) {
< prev index next >