1 /* 2 * Copyright (c) 2021, 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 24 package org.openjdk.bench.java.lang.foreign; 25 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.CompilerControl; 31 import org.openjdk.jmh.annotations.Fork; 32 import org.openjdk.jmh.annotations.Measurement; 33 import org.openjdk.jmh.annotations.Mode; 34 import org.openjdk.jmh.annotations.OutputTimeUnit; 35 import org.openjdk.jmh.annotations.Param; 36 import org.openjdk.jmh.annotations.Setup; 37 import org.openjdk.jmh.annotations.State; 38 import org.openjdk.jmh.annotations.Warmup; 39 40 import java.lang.foreign.Arena; 41 import java.nio.ByteBuffer; 42 import java.util.concurrent.TimeUnit; 43 44 import static java.lang.foreign.ValueLayout.JAVA_BYTE; 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.NANOSECONDS) 51 @Fork(value = 1, jvmArgsAppend = { 52 "-Dforeign.restricted=permit", 53 "--enable-native-access", "ALL-UNNAMED"}) 54 public class TestLoadBytes { 55 @Param("1024") 56 private int size; 57 58 private byte[] srcArray; 59 private ByteBuffer srcBufferNative; 60 private MemorySegment srcSegmentImplicit; 61 62 @Setup 63 public void setup() { 64 srcArray = new byte[size]; 65 for (int i = 0; i < srcArray.length; i++) { 66 srcArray[i] = (byte) i; 67 } 68 69 srcBufferNative = ByteBuffer.allocateDirect(size); 70 Arena scope = Arena.ofAuto(); 71 srcSegmentImplicit = scope.allocate(size, 1); 72 } 73 74 @Benchmark 75 public int arrayScalar() { 76 int size = 0; 77 for (int i = 0; i < srcArray.length; i ++) { 78 var v = srcArray[i]; 79 size += v; 80 } 81 return size; 82 } 83 84 @Benchmark 85 public int arrayScalarConst() { 86 int size = 0; 87 for (int i = 0; i < 1024; i ++) { 88 var v = srcArray[i]; 89 size += v; 90 } 91 return size; 92 } 93 94 @Benchmark 95 public int bufferNativeScalar() { 96 int size = 0; 97 for (int i = 0; i < srcArray.length; i++) { 98 var v = srcBufferNative.get(i); 99 size += v; 100 } 101 return size; 102 } 103 104 @Benchmark 105 public int bufferNativeScalarConst() { 106 int size = 0; 107 for (int i = 0; i < 1024; i++) { 108 var v = srcBufferNative.get(i); 109 size += v; 110 } 111 return size; 112 } 113 114 @Benchmark 115 public int segmentNativeScalar() { 116 int size = 0; 117 for (int i = 0; i < srcArray.length; i++) { 118 var v = srcSegmentImplicit.get(JAVA_BYTE, i); 119 size += v; 120 } 121 return size; 122 } 123 124 @Benchmark 125 public int segmentNativeScalarConst() { 126 int size = 0; 127 for (int i = 0; i < 1024; i++) { 128 var v = srcSegmentImplicit.get(JAVA_BYTE, i); 129 size += v; 130 } 131 return size; 132 } 133 }