1 /* 2 * Copyright (c) 2020, 2022, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package org.openjdk.bench.java.lang.foreign; 24 25 import org.openjdk.jmh.annotations.Benchmark; 26 import org.openjdk.jmh.annotations.BenchmarkMode; 27 import org.openjdk.jmh.annotations.CompilerControl; 28 import org.openjdk.jmh.annotations.Fork; 29 import org.openjdk.jmh.annotations.Measurement; 30 import org.openjdk.jmh.annotations.Mode; 31 import org.openjdk.jmh.annotations.OutputTimeUnit; 32 import org.openjdk.jmh.annotations.Setup; 33 import org.openjdk.jmh.annotations.State; 34 import org.openjdk.jmh.annotations.TearDown; 35 import org.openjdk.jmh.annotations.Warmup; 36 import sun.misc.Unsafe; 37 38 import java.lang.foreign.MemorySegment; 39 import java.lang.foreign.Arena; 40 import java.nio.ByteBuffer; 41 import java.nio.ByteOrder; 42 import java.util.concurrent.TimeUnit; 43 44 import static java.lang.foreign.ValueLayout.*; 45 46 @BenchmarkMode(Mode.AverageTime) 47 @Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS) 48 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS) 49 @State(org.openjdk.jmh.annotations.Scope.Thread) 50 @OutputTimeUnit(TimeUnit.MILLISECONDS) 51 @Fork(value = 3, jvmArgsAppend = "--enable-preview") 52 public class LoopOverConstant extends JavaLayouts { 53 54 static final Unsafe unsafe = Utils.unsafe; 55 56 static final int ELEM_SIZE = 1_000_000; 57 static final int CARRIER_SIZE = (int)JAVA_INT.byteSize(); 58 static final int ALLOC_SIZE = ELEM_SIZE * CARRIER_SIZE; 59 60 static final long unsafe_addr = unsafe.allocateMemory(ALLOC_SIZE); 61 62 //setup unsafe address 63 64 static { 65 for (int i = 0; i < ELEM_SIZE; i++) { 66 unsafe.putInt(unsafe_addr + (i * CARRIER_SIZE) , i); 67 } 68 } 69 70 //setup native memory segment 71 72 static final MemorySegment segment; 73 74 static { 75 Arena scope = Arena.ofAuto(); 76 segment = scope.allocate(ALLOC_SIZE, 1); 77 for (int i = 0; i < ELEM_SIZE; i++) { 78 VH_INT.set(segment, (long) i, i); 79 } 80 } 81 82 //setup direct buffer 83 84 static final ByteBuffer bb = ByteBuffer.allocateDirect(ALLOC_SIZE).order(ByteOrder.nativeOrder()); 85 86 static { 87 for (int i = 0; i < ELEM_SIZE; i++) { 88 bb.putInt(i * CARRIER_SIZE , i); 89 } 90 } 91 92 @Benchmark 93 @OutputTimeUnit(TimeUnit.NANOSECONDS) 94 public int unsafe_get() { 95 return unsafe.getInt(unsafe_addr); 96 } 97 98 @Benchmark 99 @OutputTimeUnit(TimeUnit.NANOSECONDS) 100 public int segment_get() { 101 return (int)VH_INT.get(segment, 0L); 102 } 103 104 @Benchmark 105 @OutputTimeUnit(TimeUnit.NANOSECONDS) 106 public int BB_get() { 107 return bb.getInt(0); 108 } 109 110 @Benchmark 111 public int unsafe_loop() { 112 int res = 0; 113 for (int i = 0; i < ELEM_SIZE; i ++) { 114 res += unsafe.getInt(unsafe_addr + (i * CARRIER_SIZE)); 115 } 116 return res; 117 } 118 119 @Benchmark 120 public int segment_loop() { 121 int res = 0; 122 for (int i = 0; i < ELEM_SIZE; i++) { 123 res += (int) VH_INT.get(segment, (long)i); 124 } 125 return res; 126 } 127 128 @Benchmark 129 public int BB_loop() { 130 int res = 0; 131 for (int i = 0; i < ELEM_SIZE; i++) { 132 res += bb.getInt(i * CARRIER_SIZE); 133 } 134 return res; 135 } 136 }