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.Param; 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.nio.ByteBuffer; 42 import java.nio.ByteOrder; 43 import java.util.concurrent.TimeUnit; 44 45 import static java.lang.foreign.ValueLayout.*; 46 47 @BenchmarkMode(Mode.AverageTime) 48 @Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS) 49 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS) 50 @State(org.openjdk.jmh.annotations.Scope.Thread) 51 @OutputTimeUnit(TimeUnit.MILLISECONDS) 52 @Fork(3) 53 public class LoopOverNonConstantHeap extends JavaLayouts { 54 55 static final Unsafe unsafe = Utils.unsafe; 56 57 static final int ELEM_SIZE = 1_000_000; 58 static final int CARRIER_SIZE = (int)JAVA_INT.byteSize(); 59 static final int ALLOC_SIZE = ELEM_SIZE * CARRIER_SIZE; 60 static final int UNSAFE_BYTE_BASE = unsafe.arrayBaseOffset(byte[].class); 61 static final int UNSAFE_INT_BASE = unsafe.arrayBaseOffset(int[].class); 62 63 MemorySegment segment, alignedSegment; 64 byte[] base; 65 int[] alignedBase; 66 67 ByteBuffer byteBuffer; 68 69 @Param(value = {"false", "true"}) 70 boolean polluteProfile; 71 72 @Setup 73 public void setup() { 74 if (polluteProfile) { 75 MemorySegment intB = MemorySegment.ofArray(new byte[ALLOC_SIZE]); 76 MemorySegment intI = MemorySegment.ofArray(new int[ALLOC_SIZE]); 77 MemorySegment intD = MemorySegment.ofArray(new double[ALLOC_SIZE]); 78 MemorySegment intF = MemorySegment.ofArray(new float[ALLOC_SIZE]); 79 Arena scope = Arena.ofAuto(); 80 MemorySegment s = scope.allocate(ALLOC_SIZE, 1); 81 for (int i = 0; i < ALLOC_SIZE; i++) { 82 intB.set(JAVA_BYTE, i, (byte)i); 83 intI.setAtIndex(JAVA_INT, i, i); 84 intD.setAtIndex(JAVA_DOUBLE, i, i); 85 intF.setAtIndex(JAVA_FLOAT, i, i); 86 s.set(JAVA_BYTE, i, (byte) i); 87 } 88 } 89 90 base = new byte[ALLOC_SIZE]; 91 for (int i = 0; i < ELEM_SIZE; i++) { 92 unsafe.putInt(base, UNSAFE_BYTE_BASE + (i * CARRIER_SIZE) , i); 93 } 94 alignedBase = new int[ELEM_SIZE]; 95 for (int i = 0; i < ELEM_SIZE; i++) { 96 unsafe.putInt(base, UNSAFE_INT_BASE + (i * CARRIER_SIZE) , i); 97 } 98 segment = MemorySegment.ofArray(base); 99 alignedSegment = MemorySegment.ofArray(alignedBase); 100 byteBuffer = ByteBuffer.wrap(base).order(ByteOrder.nativeOrder()); 101 } 102 103 @Benchmark 104 @OutputTimeUnit(TimeUnit.NANOSECONDS) 105 public int unsafe_get() { 106 return unsafe.getInt(base, UNSAFE_BYTE_BASE); 107 } 108 109 @Benchmark 110 @OutputTimeUnit(TimeUnit.NANOSECONDS) 111 public int segment_get() { 112 return (int) VH_INT_UNALIGNED.get(segment, 0L); 113 } 114 115 @Benchmark 116 @OutputTimeUnit(TimeUnit.NANOSECONDS) 117 public int BB_get() { 118 return byteBuffer.getInt(0); 119 } 120 121 @Benchmark 122 public int unsafe_loop() { 123 int res = 0; 124 for (int i = 0; i < ELEM_SIZE; i ++) { 125 res += unsafe.getInt(base, UNSAFE_BYTE_BASE + (i * CARRIER_SIZE)); 126 } 127 return res; 128 } 129 130 @Benchmark 131 public int segment_loop_unaligned() { 132 int sum = 0; 133 for (int i = 0; i < ELEM_SIZE; i++) { 134 sum += (int) VH_INT_UNALIGNED.get(segment, (long) i); 135 } 136 return sum; 137 } 138 139 @Benchmark 140 public int segment_loop() { 141 int sum = 0; 142 for (int i = 0; i < ELEM_SIZE; i++) { 143 sum += (int) VH_INT.get(alignedSegment, (long) i); 144 } 145 return sum; 146 } 147 148 @Benchmark 149 public int segment_loop_instance_unaligned() { 150 int res = 0; 151 for (int i = 0; i < ELEM_SIZE; i ++) { 152 res += segment.get(JAVA_INT_UNALIGNED, i * CARRIER_SIZE); 153 } 154 return res; 155 } 156 157 @Benchmark 158 public int segment_loop_instance() { 159 int res = 0; 160 for (int i = 0; i < ELEM_SIZE; i ++) { 161 res += alignedSegment.get(JAVA_INT, i * CARRIER_SIZE); 162 } 163 return res; 164 } 165 166 @Benchmark 167 public int segment_loop_slice() { 168 int sum = 0; 169 MemorySegment base = alignedSegment.asSlice(0, alignedSegment.byteSize()); 170 for (int i = 0; i < ELEM_SIZE; i++) { 171 sum += (int) VH_INT.get(base, (long) i); 172 } 173 return sum; 174 } 175 176 @Benchmark 177 public int segment_loop_readonly() { 178 int sum = 0; 179 MemorySegment base = alignedSegment.asReadOnly(); 180 for (int i = 0; i < ELEM_SIZE; i++) { 181 sum += (int) VH_INT.get(base, (long) i); 182 } 183 return sum; 184 } 185 186 @Benchmark 187 public int BB_loop() { 188 int sum = 0; 189 ByteBuffer bb = byteBuffer; 190 for (int i = 0; i < ELEM_SIZE; i++) { 191 sum += bb.getInt(i * CARRIER_SIZE); 192 } 193 return sum; 194 } 195 196 }