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.JAVA_INT; 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(3) 52 public class LoopOverNonConstantShared 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 Arena arena; 60 MemorySegment segment; 61 long unsafe_addr; 62 63 ByteBuffer byteBuffer; 64 65 @Setup 66 public void setup() { 67 unsafe_addr = unsafe.allocateMemory(ALLOC_SIZE); 68 for (int i = 0; i < ELEM_SIZE; i++) { 69 unsafe.putInt(unsafe_addr + (i * CARRIER_SIZE) , i); 70 } 71 arena = Arena.ofConfined(); 72 segment = arena.allocate(ALLOC_SIZE, CARRIER_SIZE); 73 for (int i = 0; i < ELEM_SIZE; i++) { 74 VH_INT.set(segment, (long) i, i); 75 } 76 byteBuffer = ByteBuffer.allocateDirect(ALLOC_SIZE).order(ByteOrder.nativeOrder()); 77 for (int i = 0; i < ELEM_SIZE; i++) { 78 byteBuffer.putInt(i * CARRIER_SIZE , i); 79 } 80 } 81 82 @TearDown 83 public void tearDown() { 84 arena.close(); 85 unsafe.invokeCleaner(byteBuffer); 86 unsafe.freeMemory(unsafe_addr); 87 } 88 89 @Benchmark 90 @OutputTimeUnit(TimeUnit.NANOSECONDS) 91 public int unsafe_get() { 92 return unsafe.getInt(unsafe_addr); 93 } 94 95 @Benchmark 96 @OutputTimeUnit(TimeUnit.NANOSECONDS) 97 public int segment_get() { 98 return (int) VH_INT.get(segment, 0L); 99 } 100 101 @Benchmark 102 @OutputTimeUnit(TimeUnit.NANOSECONDS) 103 public int BB_get() { 104 return byteBuffer.getInt(0); 105 } 106 107 @Benchmark 108 public int unsafe_loop() { 109 int res = 0; 110 for (int i = 0; i < ELEM_SIZE; i ++) { 111 res += unsafe.getInt(unsafe_addr + (i * CARRIER_SIZE)); 112 } 113 return res; 114 } 115 116 @Benchmark 117 public int segment_loop_instance() { 118 int res = 0; 119 for (int i = 0; i < ELEM_SIZE; i ++) { 120 res += segment.get(JAVA_INT, i * CARRIER_SIZE); 121 } 122 return res; 123 } 124 125 @Benchmark 126 public int segment_loop_instance_address() { 127 int res = 0; 128 for (int i = 0; i < ELEM_SIZE; i ++) { 129 res += segment.get(JAVA_INT, i * CARRIER_SIZE); 130 } 131 return res; 132 } 133 134 @Benchmark 135 public int segment_loop() { 136 int sum = 0; 137 for (int i = 0; i < ELEM_SIZE; i++) { 138 sum += (int) VH_INT.get(segment, (long) i); 139 } 140 return sum; 141 } 142 143 @Benchmark 144 public int segment_loop_slice() { 145 int sum = 0; 146 MemorySegment base = segment.asSlice(0, segment.byteSize()); 147 for (int i = 0; i < ELEM_SIZE; i++) { 148 sum += (int) VH_INT.get(base, (long) i); 149 } 150 return sum; 151 } 152 153 @Benchmark 154 public int segment_loop_readonly() { 155 int sum = 0; 156 MemorySegment base = segment.asReadOnly(); 157 for (int i = 0; i < ELEM_SIZE; i++) { 158 sum += (int) VH_INT.get(base, (long) i); 159 } 160 return sum; 161 } 162 163 @Benchmark 164 public int BB_loop() { 165 int sum = 0; 166 ByteBuffer bb = byteBuffer; 167 for (int i = 0; i < ELEM_SIZE; i++) { 168 sum += bb.getInt(i * CARRIER_SIZE); 169 } 170 return sum; 171 } 172 173 }