< prev index next >

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

Print this page

 27 import org.openjdk.jmh.annotations.Benchmark;
 28 import org.openjdk.jmh.annotations.BenchmarkMode;
 29 import org.openjdk.jmh.annotations.CompilerControl;
 30 import org.openjdk.jmh.annotations.Fork;
 31 import org.openjdk.jmh.annotations.Measurement;
 32 import org.openjdk.jmh.annotations.Mode;
 33 import org.openjdk.jmh.annotations.OutputTimeUnit;
 34 import org.openjdk.jmh.annotations.Setup;
 35 import org.openjdk.jmh.annotations.State;
 36 import org.openjdk.jmh.annotations.TearDown;
 37 import org.openjdk.jmh.annotations.Warmup;
 38 import sun.misc.Unsafe;
 39 
 40 import jdk.incubator.foreign.MemorySegment;
 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 LoopOverConstant {
 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 
 63     static final long unsafe_addr = unsafe.allocateMemory(ALLOC_SIZE);
 64 
 65     //setup unsafe address
 66 
 67     static {
 68         for (int i = 0; i < ELEM_SIZE; i++) {
 69             unsafe.putInt(unsafe_addr + (i * CARRIER_SIZE) , i);
 70         }
 71     }
 72 
 73     //setup native memory segment
 74 
 75     static final MemorySegment segment = MemorySegment.allocateNative(ALLOC_SIZE, ResourceScope.newImplicitScope());
 76     static final VarHandle VH_int = MemoryLayout.sequenceLayout(JAVA_INT).varHandle(int.class, sequenceElement());
 77 
 78     static {
 79         for (int i = 0; i < ELEM_SIZE; i++) {
 80             VH_int.set(segment, (long) i, i);
 81         }
 82     }
 83 
 84     //setup direct buffer
 85 
 86     static final ByteBuffer bb = ByteBuffer.allocateDirect(ALLOC_SIZE).order(ByteOrder.nativeOrder());
 87 
 88     static {
 89         for (int i = 0; i < ELEM_SIZE; i++) {
 90             bb.putInt(i * CARRIER_SIZE , i);
 91         }
 92     }
 93 
 94     @Benchmark
 95     @OutputTimeUnit(TimeUnit.NANOSECONDS)
 96     public int unsafe_get() {

 27 import org.openjdk.jmh.annotations.Benchmark;
 28 import org.openjdk.jmh.annotations.BenchmarkMode;
 29 import org.openjdk.jmh.annotations.CompilerControl;
 30 import org.openjdk.jmh.annotations.Fork;
 31 import org.openjdk.jmh.annotations.Measurement;
 32 import org.openjdk.jmh.annotations.Mode;
 33 import org.openjdk.jmh.annotations.OutputTimeUnit;
 34 import org.openjdk.jmh.annotations.Setup;
 35 import org.openjdk.jmh.annotations.State;
 36 import org.openjdk.jmh.annotations.TearDown;
 37 import org.openjdk.jmh.annotations.Warmup;
 38 import sun.misc.Unsafe;
 39 
 40 import jdk.incubator.foreign.MemorySegment;
 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 LoopOverConstant {
 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 
 63     static final long unsafe_addr = unsafe.allocateMemory(ALLOC_SIZE);
 64 
 65     //setup unsafe address
 66 
 67     static {
 68         for (int i = 0; i < ELEM_SIZE; i++) {
 69             unsafe.putInt(unsafe_addr + (i * CARRIER_SIZE) , i);
 70         }
 71     }
 72 
 73     //setup native memory segment
 74 
 75     static final MemorySegment segment = MemorySegment.allocateNative(ALLOC_SIZE, ResourceScope.newImplicitScope());
 76     static final VarHandle VH_int = MemoryLayout.sequenceLayout(JAVA_INT).varHandle(sequenceElement());
 77 
 78     static {
 79         for (int i = 0; i < ELEM_SIZE; i++) {
 80             VH_int.set(segment, (long) i, i);
 81         }
 82     }
 83 
 84     //setup direct buffer
 85 
 86     static final ByteBuffer bb = ByteBuffer.allocateDirect(ALLOC_SIZE).order(ByteOrder.nativeOrder());
 87 
 88     static {
 89         for (int i = 0; i < ELEM_SIZE; i++) {
 90             bb.putInt(i * CARRIER_SIZE , i);
 91         }
 92     }
 93 
 94     @Benchmark
 95     @OutputTimeUnit(TimeUnit.NANOSECONDS)
 96     public int unsafe_get() {
< prev index next >