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 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 sun.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(3) 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 ByteBuffer hbb = ByteBuffer.wrap(arr).order(ByteOrder.nativeOrder()); 62 FloatBuffer hfb = hbb.asFloatBuffer(); 63 64 65 @Setup 66 public void setup() { 67 for (int i = 0; i < ELEM_SIZE; i++) { 68 dbb.putFloat(i * 4, i); 69 hbb.putFloat(i * 4, i); 70 } 71 for (int i = 0; i < ELEM_SIZE; i++) { 72 hfb.put(i, i); 73 } 74 } 75 76 @TearDown 77 public void tearDown() { 78 unsafe.invokeCleaner(dbb); 79 arr = null; 80 hbb = null; 81 hfb = null; 82 } 83 84 @Benchmark 85 public int direct_byte_buffer_get_float() { 86 int sum = 0; 87 for (int k = 0; k < ELEM_SIZE; k++) { 88 dbb.putFloat(k, (float)k + 1); 89 float v = dbb.getFloat(k * 4); 90 sum += (int)v; 91 } 92 return sum; 93 } 94 95 @Benchmark 96 public int heap_byte_buffer_get_int() { 97 int sum = 0; 98 for (int k = 0; k < ELEM_SIZE; k++) { 99 hbb.putInt(k, k + 1); 100 int v = hbb.getInt(k * 4); 101 sum += v; 102 } 103 return sum; 104 } 105 106 @Benchmark 107 public int unsafe_get_float() { 108 int sum = 0; 109 for (int k = 0; k < ALLOC_SIZE; k += 4) { 110 unsafe.putFloat(arr, k + Unsafe.ARRAY_BYTE_BASE_OFFSET, k + 1); 111 float v = unsafe.getFloat(arr, k + Unsafe.ARRAY_BYTE_BASE_OFFSET); 112 sum += (int)v; 113 } 114 return sum; 115 } 116 }