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.
  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.jdk.incubator.foreign;
 24 
 25 import jdk.incubator.foreign.MemoryAddress;
 26 import jdk.incubator.foreign.MemoryLayout;
 27 import jdk.incubator.foreign.MemorySegment;
 28 import jdk.incubator.foreign.ResourceScope;
 29 import org.openjdk.jmh.annotations.Benchmark;
 30 import org.openjdk.jmh.annotations.BenchmarkMode;
 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.Setup;
 36 import org.openjdk.jmh.annotations.State;
 37 import org.openjdk.jmh.annotations.TearDown;
 38 import org.openjdk.jmh.annotations.Warmup;
 39 import sun.misc.Unsafe;
 40 
 41 import java.lang.invoke.VarHandle;
 42 import java.nio.ByteBuffer;
 43 import java.nio.ByteOrder;
 44 import java.util.concurrent.TimeUnit;
 45 
 46 import static jdk.incubator.foreign.MemoryLayout.PathElement.sequenceElement;
 47 import static jdk.incubator.foreign.ValueLayout.JAVA_INT;
 48 
 49 @BenchmarkMode(Mode.AverageTime)
 50 @Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 51 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 52 @State(org.openjdk.jmh.annotations.Scope.Thread)
 53 @OutputTimeUnit(TimeUnit.MILLISECONDS)
 54 @Fork(value = 3, jvmArgsAppend = { "--add-modules=jdk.incubator.foreign" })
 55 public class LoopOverNonConstant {
 56 
 57     static final Unsafe unsafe = Utils.unsafe;
 58 
 59     static final int ELEM_SIZE = 1_000_000;
 60     static final int CARRIER_SIZE = (int)JAVA_INT.byteSize();
 61     static final int ALLOC_SIZE = ELEM_SIZE * CARRIER_SIZE;
 62 
 63     static final VarHandle VH_int = MemoryLayout.sequenceLayout(JAVA_INT).varHandle(sequenceElement());
 64     MemorySegment segment;
 65     long unsafe_addr;
 66 
 67     ByteBuffer byteBuffer;
 68 
 69     @Setup
 70     public void setup() {
 71         unsafe_addr = unsafe.allocateMemory(ALLOC_SIZE);
 72         for (int i = 0; i < ELEM_SIZE; i++) {
 73             unsafe.putInt(unsafe_addr + (i * CARRIER_SIZE) , i);
 74         }
 75         segment = MemorySegment.allocateNative(ALLOC_SIZE, ResourceScope.newConfinedScope());
 76         for (int i = 0; i < ELEM_SIZE; i++) {
 77             VH_int.set(segment, (long) i, i);
 78         }
 79         byteBuffer = ByteBuffer.allocateDirect(ALLOC_SIZE).order(ByteOrder.nativeOrder());
 80         for (int i = 0; i < ELEM_SIZE; i++) {
 81             byteBuffer.putInt(i * CARRIER_SIZE , i);
 82         }
 83     }
 84 
 85     @TearDown
 86     public void tearDown() {
 87         segment.scope().close();
 88         unsafe.invokeCleaner(byteBuffer);
 89         unsafe.freeMemory(unsafe_addr);
 90     }
 91 
 92     @Benchmark
 93     @OutputTimeUnit(TimeUnit.NANOSECONDS)
 94     public int unsafe_get() {
 95         return unsafe.getInt(unsafe_addr);
 96     }
 97 
 98     @Benchmark
 99     @OutputTimeUnit(TimeUnit.NANOSECONDS)
100     public int segment_get() {
101         return (int) VH_int.get(segment, 0L);
102     }
103 
104     @Benchmark
105     @OutputTimeUnit(TimeUnit.NANOSECONDS)
106     public int BB_get() {
107         return byteBuffer.getInt(0);
108     }
109 
110     @Benchmark
111     public int unsafe_loop() {
112         int res = 0;
113         for (int i = 0; i < ELEM_SIZE; i ++) {
114             res += unsafe.getInt(unsafe_addr + (i * CARRIER_SIZE));
115         }
116         return res;
117     }
118 
119     @Benchmark
120     public int segment_loop() {
121         int sum = 0;
122         for (int i = 0; i < ELEM_SIZE; i++) {
123             sum += (int) VH_int.get(segment, (long) i);
124         }
125         return sum;
126     }
127 
128     @Benchmark
129     public int segment_loop_instance() {
130         int sum = 0;
131         for (int i = 0; i < ELEM_SIZE; i++) {
132             sum += segment.get(JAVA_INT, i * CARRIER_SIZE);
133 
134         }
135         return sum;
136     }
137 
138     @Benchmark
139     public int segment_loop_instance_index() {
140         int sum = 0;
141         for (int i = 0; i < ELEM_SIZE; i++) {
142             sum += segment.getAtIndex(JAVA_INT, i);
143 
144         }
145         return sum;
146     }
147 
148     @Benchmark
149     public int segment_loop_instance_address() {
150         int sum = 0;
151         for (int i = 0; i < ELEM_SIZE; i++) {
152             sum += segment.address().get(JAVA_INT, i * CARRIER_SIZE);
153         }
154         return sum;
155     }
156 
157     @Benchmark
158     public int segment_loop_instance_address_index() {
159         int sum = 0;
160         for (int i = 0; i < ELEM_SIZE; i++) {
161             sum += segment.address().getAtIndex(JAVA_INT, i);
162         }
163         return sum;
164     }
165 
166     @Benchmark
167     public int segment_loop_slice() {
168         int sum = 0;
169         MemorySegment base = segment.asSlice(0, segment.byteSize());
170         for (int i = 0; i < ELEM_SIZE; i++) {
171             sum += (int) VH_int.get(base, (long) i);
172         }
173         return sum;
174     }
175 
176     @Benchmark
177     public int segment_loop_readonly() {
178         int sum = 0;
179         MemorySegment base = segment.asReadOnly();
180         for (int i = 0; i < ELEM_SIZE; i++) {
181             sum += (int) VH_int.get(base, (long) i);
182         }
183         return sum;
184     }
185 
186     @Benchmark
187     public int BB_loop() {
188         int sum = 0;
189         ByteBuffer bb = byteBuffer;
190         for (int i = 0; i < ELEM_SIZE; i++) {
191             sum += bb.getInt(i * CARRIER_SIZE);
192         }
193         return sum;
194     }
195 
196 }