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 java.lang.foreign.Arena; 26 import java.lang.foreign.MemorySegment; 27 28 import org.openjdk.jmh.annotations.Benchmark; 29 import org.openjdk.jmh.annotations.BenchmarkMode; 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 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-native-access=ALL-UNNAMED" }) 52 public class LoopOverNonConstant 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 Arena arena; 61 MemorySegment segment; 62 long unsafe_addr; 63 64 ByteBuffer byteBuffer; 65 66 @Setup 67 public void setup() { 68 unsafe_addr = unsafe.allocateMemory(ALLOC_SIZE); 69 for (int i = 0; i < ELEM_SIZE; i++) { 70 unsafe.putInt(unsafe_addr + (i * CARRIER_SIZE) , i); 71 } 72 arena = Arena.ofConfined(); 73 segment = arena.allocate(ALLOC_SIZE, 1); 74 for (int i = 0; i < ELEM_SIZE; i++) { 75 VH_INT.set(segment, (long) i, i); 76 } 77 byteBuffer = ByteBuffer.allocateDirect(ALLOC_SIZE).order(ByteOrder.nativeOrder()); 78 for (int i = 0; i < ELEM_SIZE; i++) { 79 byteBuffer.putInt(i * CARRIER_SIZE , i); 80 } 81 } 82 83 @TearDown 84 public void tearDown() { 85 arena.close(); 86 unsafe.invokeCleaner(byteBuffer); 87 unsafe.freeMemory(unsafe_addr); 88 } 89 90 @Benchmark 91 @OutputTimeUnit(TimeUnit.NANOSECONDS) 92 public int unsafe_get() { 93 return unsafe.getInt(unsafe_addr); 94 } 95 96 @Benchmark 97 @OutputTimeUnit(TimeUnit.NANOSECONDS) 98 public int segment_get() { 99 return (int) VH_INT.get(segment, 0L); 100 } 101 102 @Benchmark 103 @OutputTimeUnit(TimeUnit.NANOSECONDS) 104 public int BB_get() { 105 return byteBuffer.getInt(0); 106 } 107 108 @Benchmark 109 public int unsafe_loop() { 110 int res = 0; 111 for (int i = 0; i < ELEM_SIZE; i ++) { 112 res += unsafe.getInt(unsafe_addr + (i * CARRIER_SIZE)); 113 } 114 return res; 115 } 116 117 @Benchmark 118 public int segment_loop() { 119 int sum = 0; 120 for (int i = 0; i < ELEM_SIZE; i++) { 121 sum += (int) VH_INT.get(segment, (long) i); 122 } 123 return sum; 124 } 125 126 @Benchmark 127 public int segment_loop_unaligned() { 128 int sum = 0; 129 for (int i = 0; i < ELEM_SIZE; i++) { 130 sum += (int) VH_INT_UNALIGNED.get(segment, (long) i); 131 } 132 return sum; 133 } 134 135 @Benchmark 136 public int segment_loop_instance() { 137 int sum = 0; 138 for (int i = 0; i < ELEM_SIZE; i++) { 139 sum += segment.get(JAVA_INT, i * CARRIER_SIZE); 140 141 } 142 return sum; 143 } 144 145 @Benchmark 146 public int segment_loop_instance_index() { 147 int sum = 0; 148 for (int i = 0; i < ELEM_SIZE; i++) { 149 sum += segment.getAtIndex(JAVA_INT, i); 150 151 } 152 return sum; 153 } 154 155 @Benchmark 156 public int segment_loop_instance_unaligned() { 157 int res = 0; 158 for (int i = 0; i < ELEM_SIZE; i ++) { 159 res += segment.get(JAVA_INT_UNALIGNED, i * CARRIER_SIZE); 160 } 161 return res; 162 } 163 164 @Benchmark 165 public int segment_loop_slice() { 166 int sum = 0; 167 MemorySegment base = segment.asSlice(0, segment.byteSize()); 168 for (int i = 0; i < ELEM_SIZE; i++) { 169 sum += (int) VH_INT.get(base, (long) i); 170 } 171 return sum; 172 } 173 174 @Benchmark 175 public int segment_loop_readonly() { 176 int sum = 0; 177 MemorySegment base = segment.asReadOnly(); 178 for (int i = 0; i < ELEM_SIZE; i++) { 179 sum += (int) VH_INT.get(base, (long) i); 180 } 181 return sum; 182 } 183 184 @Benchmark 185 public int BB_loop() { 186 int sum = 0; 187 ByteBuffer bb = byteBuffer; 188 for (int i = 0; i < ELEM_SIZE; i++) { 189 sum += bb.getInt(i * CARRIER_SIZE); 190 } 191 return sum; 192 } 193 194 }