|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, 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. Oracle designates this |
| 8 | + * particular file as subject to the "Classpath" exception as provided |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. |
| 10 | + * |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | + * accompanied this code). |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License version |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | + * |
| 21 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 22 | + * or visit www.oracle.com if you need additional information or have any |
| 23 | + * questions. |
| 24 | + */ |
| 25 | +package org.openjdk.bench.jdk.incubator.foreign; |
| 26 | + |
| 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.nio.FloatBuffer; |
| 43 | +import java.util.concurrent.TimeUnit; |
| 44 | + |
| 45 | +import static jdk.incubator.foreign.MemoryLayouts.JAVA_INT; |
| 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(value = 3, jvmArgsAppend = { "--add-modules=jdk.incubator.foreign" }) |
| 53 | +public class LoopOverPollutedBuffer { |
| 54 | + |
| 55 | + static final int ELEM_SIZE = 1_000_000; |
| 56 | + static final int CARRIER_SIZE = (int) JAVA_INT.byteSize(); |
| 57 | + static final int ALLOC_SIZE = ELEM_SIZE * CARRIER_SIZE; |
| 58 | + |
| 59 | + static final Unsafe unsafe = Utils.unsafe; |
| 60 | + |
| 61 | + ByteBuffer dbb = ByteBuffer.allocateDirect(ALLOC_SIZE).order(ByteOrder.nativeOrder()); |
| 62 | + byte[] arr = new byte[ALLOC_SIZE]; |
| 63 | + ByteBuffer hbb = ByteBuffer.wrap(arr).order(ByteOrder.nativeOrder()); |
| 64 | + FloatBuffer hfb = hbb.asFloatBuffer(); |
| 65 | + |
| 66 | + |
| 67 | + @Setup |
| 68 | + public void setup() { |
| 69 | + for (int i = 0; i < ELEM_SIZE; i++) { |
| 70 | + dbb.putFloat(i * 4, i); |
| 71 | + hbb.putFloat(i * 4, i); |
| 72 | + } |
| 73 | + for (int i = 0; i < ELEM_SIZE; i++) { |
| 74 | + hfb.put(i, i); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + @TearDown |
| 79 | + public void tearDown() { |
| 80 | + unsafe.invokeCleaner(dbb); |
| 81 | + arr = null; |
| 82 | + hbb = null; |
| 83 | + hfb = null; |
| 84 | + } |
| 85 | + |
| 86 | + @Benchmark |
| 87 | + public int direct_byte_buffer_get_float() { |
| 88 | + int sum = 0; |
| 89 | + for (int k = 0; k < ELEM_SIZE; k++) { |
| 90 | + dbb.putFloat(k, (float)k + 1); |
| 91 | + float v = dbb.getFloat(k * 4); |
| 92 | + sum += (int)v; |
| 93 | + } |
| 94 | + return sum; |
| 95 | + } |
| 96 | + |
| 97 | + @Benchmark |
| 98 | + public int heap_byte_buffer_get_int() { |
| 99 | + int sum = 0; |
| 100 | + for (int k = 0; k < ELEM_SIZE; k++) { |
| 101 | + hbb.putInt(k, k + 1); |
| 102 | + int v = hbb.getInt(k * 4); |
| 103 | + sum += v; |
| 104 | + } |
| 105 | + return sum; |
| 106 | + } |
| 107 | + |
| 108 | + @Benchmark |
| 109 | + public int unsafe_get_float() { |
| 110 | + int sum = 0; |
| 111 | + for (int k = 0; k < ALLOC_SIZE; k += 4) { |
| 112 | + unsafe.putFloat(arr, k + Unsafe.ARRAY_BYTE_BASE_OFFSET, k + 1); |
| 113 | + float v = unsafe.getFloat(arr, k + Unsafe.ARRAY_BYTE_BASE_OFFSET); |
| 114 | + sum += (int)v; |
| 115 | + } |
| 116 | + return sum; |
| 117 | + } |
| 118 | +} |
0 commit comments