1 /* 2 * Copyright (c) 2020, 2024, 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 26 import org.openjdk.jmh.annotations.Benchmark; 27 import org.openjdk.jmh.annotations.BenchmarkMode; 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 jdk.internal.misc.Unsafe; 37 38 import java.nio.ByteBuffer; 39 import java.nio.ByteOrder; 40 import java.nio.FloatBuffer; 41 import java.util.concurrent.TimeUnit; 42 43 import static java.lang.foreign.ValueLayout.JAVA_INT; 44 45 @BenchmarkMode(Mode.AverageTime) 46 @Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS) 47 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS) 48 @State(org.openjdk.jmh.annotations.Scope.Thread) 49 @OutputTimeUnit(TimeUnit.MILLISECONDS) 50 @Fork(value = 3, jvmArgs = { "--add-opens=java.base/jdk.internal.misc=ALL-UNNAMED" }) 51 public class LoopOverPollutedBuffer { 52 53 static final int ELEM_SIZE = 1_000_000; 54 static final int CARRIER_SIZE = (int) JAVA_INT.byteSize(); 55 static final int ALLOC_SIZE = ELEM_SIZE * CARRIER_SIZE; 56 57 static final Unsafe unsafe = Utils.unsafe; 58 59 ByteBuffer dbb = ByteBuffer.allocateDirect(ALLOC_SIZE).order(ByteOrder.nativeOrder()); 60 byte[] arr = new byte[ALLOC_SIZE]; 61 @SuppressWarnings("initialization") 62 ByteBuffer hbb = ByteBuffer.wrap(arr).order(ByteOrder.nativeOrder()); 63 FloatBuffer hfb = hbb.asFloatBuffer(); 64 65 66 @Setup 67 public void setup() { 68 for (int i = 0; i < ELEM_SIZE; i++) { 69 dbb.putFloat(i * 4, i); 70 hbb.putFloat(i * 4, i); 71 } 72 for (int i = 0; i < ELEM_SIZE; i++) { 73 hfb.put(i, i); 74 } 75 } 76 77 @TearDown 78 public void tearDown() { 79 unsafe.invokeCleaner(dbb); 80 arr = null; 81 hbb = null; 82 hfb = null; 83 } 84 85 @Benchmark 86 public int direct_byte_buffer_get_float() { 87 int sum = 0; 88 for (int k = 0; k < ELEM_SIZE; k++) { 89 dbb.putFloat(k, (float)k + 1); 90 float v = dbb.getFloat(k * 4); 91 sum += (int)v; 92 } 93 return sum; 94 } 95 96 @Benchmark 97 public int heap_byte_buffer_get_int() { 98 int sum = 0; 99 for (int k = 0; k < ELEM_SIZE; k++) { 100 hbb.putInt(k, k + 1); 101 int v = hbb.getInt(k * 4); 102 sum += v; 103 } 104 return sum; 105 } 106 107 @Benchmark 108 public int unsafe_get_float() { 109 int sum = 0; 110 for (int k = 0; k < ALLOC_SIZE; k += 4) { 111 unsafe.putFloat(arr, k + Unsafe.ARRAY_BYTE_BASE_OFFSET, k + 1); 112 float v = unsafe.getFloat(arr, k + Unsafe.ARRAY_BYTE_BASE_OFFSET); 113 sum += (int)v; 114 } 115 return sum; 116 } 117 }