1 /*
  2  * Copyright (c) 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 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.Param;
 33 import org.openjdk.jmh.annotations.Setup;
 34 import org.openjdk.jmh.annotations.State;
 35 import org.openjdk.jmh.annotations.TearDown;
 36 import org.openjdk.jmh.annotations.Warmup;
 37 
 38 import java.lang.foreign.Arena;
 39 import java.lang.foreign.MemorySegment;
 40 import java.lang.foreign.ValueLayout;
 41 import java.lang.invoke.MethodHandles;
 42 import java.lang.invoke.VarHandle;
 43 import java.nio.ByteBuffer;
 44 import java.nio.LongBuffer;
 45 import java.util.concurrent.ThreadLocalRandom;
 46 import java.util.concurrent.TimeUnit;
 47 
 48 import static java.lang.foreign.ValueLayout.*;
 49 import static java.nio.ByteOrder.BIG_ENDIAN;
 50 
 51 /**
 52  * This benchmark creates an array of longs with random contents. The array
 53  * is then copied into a byte array (using big endian) using different
 54  * methods.
 55  */
 56 @BenchmarkMode(Mode.AverageTime)
 57 @Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 58 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 59 @State(org.openjdk.jmh.annotations.Scope.Thread)
 60 @OutputTimeUnit(TimeUnit.NANOSECONDS)
 61 @Fork(value = 3, jvmArgsAppend = {"--enable-native-access=ALL-UNNAMED", "--enable-preview"})
 62 public class MemorySegmentVsBits {
 63 
 64     public static final VarHandle LONG_ARRAY_VH = MethodHandles.byteArrayViewVarHandle(long[].class, BIG_ENDIAN);
 65 
 66     Arena arena = Arena.ofConfined();
 67 
 68     @Param({"1", "2", "16", "64", "256"})
 69     public int size;
 70     private long[] longs;
 71     private byte[] bytes;
 72 
 73     private ByteBuffer byteBuffer;
 74     private LongBuffer longBuffer;
 75     private MemorySegment segment;
 76     private MemorySegment nativeSegment;
 77 
 78     private static final ValueLayout.OfLong OF_LONG = (JAVA_LONG.order() != BIG_ENDIAN)
 79             ? JAVA_LONG.withOrder(BIG_ENDIAN)
 80             : JAVA_LONG;
 81 
 82     @Setup
 83     public void setup() {
 84         longs = ThreadLocalRandom.current().longs(size).toArray();
 85         bytes = new byte[size * Long.BYTES];
 86         byteBuffer = ByteBuffer.wrap(bytes);
 87         longBuffer = byteBuffer.asLongBuffer();
 88         segment = MemorySegment.ofArray(bytes);
 89         nativeSegment = arena.allocate(size * Long.BYTES);
 90     }
 91 
 92     @TearDown
 93     public void tearDown() {
 94         arena.close();
 95     }
 96 
 97     @Benchmark
 98     public void bitsEquivalent() {
 99         for (int i = 0; i < size; i++) {
100             putLong(bytes, i * Long.BYTES, longs[i]);
101         }
102     }
103     @Benchmark
104     public void byteVarHandle() {
105         for (int i = 0; i < size; i++) {
106             LONG_ARRAY_VH.set(bytes, i * Long.BYTES, longs[i]);
107         }
108     }
109     @Benchmark
110     public void byteBuffer() {
111         for (int i = 0; i < size; i++) {
112             byteBuffer.putLong(i * Long.BYTES, longs[i]);
113         }
114     }
115 
116     @Benchmark
117     public void longBuffer() {
118         for (int i = 0; i < size; i++) {
119             longBuffer.put(i, longs[i]);
120         }
121     }
122 
123     @Benchmark
124     public void panamaHeap() {
125         for (int i = 0; i < size; i++) {
126             segment.set(JAVA_LONG_UNALIGNED, i * Long.BYTES, longs[i]);
127         }
128     }
129 
130     @Benchmark
131     public void panamaNative() {
132         for (int i = 0; i < size; i++) {
133             nativeSegment.set(OF_LONG, i * Long.BYTES, longs[i]);
134         }
135     }
136 
137     @Benchmark
138     public void panamaNativeUnaligned() {
139         for (int i = 0; i < size; i++) {
140             nativeSegment.set(JAVA_LONG_UNALIGNED, i * Long.BYTES, longs[i]);
141         }
142     }
143 
144     // java.io.Bits is package private
145     static void putLong(byte[] b, int off, long val) {
146         b[off + 7] = (byte) (val);
147         b[off + 6] = (byte) (val >>> 8);
148         b[off + 5] = (byte) (val >>> 16);
149         b[off + 4] = (byte) (val >>> 24);
150         b[off + 3] = (byte) (val >>> 32);
151         b[off + 2] = (byte) (val >>> 40);
152         b[off + 1] = (byte) (val >>> 48);
153         b[off] = (byte) (val >>> 56);
154     }
155 
156 }