Skip to content

Commit 9255b30

Browse files
committedOct 28, 2020
Add benchmark for floating point memory access var handle performance
Reviewed-by: jvernee, psandoz
1 parent 3a792d5 commit 9255b30

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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.MemoryAccess;
26+
import jdk.incubator.foreign.MemoryAddress;
27+
import jdk.incubator.foreign.MemoryLayout;
28+
import jdk.incubator.foreign.MemorySegment;
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.MemoryLayouts.JAVA_DOUBLE;
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(3)
55+
public class LoopOverNonConstantFP {
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_DOUBLE.byteSize();
61+
static final int ALLOC_SIZE = ELEM_SIZE * CARRIER_SIZE;
62+
63+
MemorySegment segmentIn, segmentOut;
64+
long unsafe_addrIn, unsafe_addrOut;
65+
ByteBuffer byteBufferIn, byteBufferOut;
66+
67+
@Setup
68+
public void setup() {
69+
unsafe_addrIn = unsafe.allocateMemory(ALLOC_SIZE);
70+
unsafe_addrOut = unsafe.allocateMemory(ALLOC_SIZE);
71+
for (int i = 0; i < ELEM_SIZE; i++) {
72+
unsafe.putDouble(unsafe_addrIn + (i * CARRIER_SIZE) , i);
73+
}
74+
for (int i = 0; i < ELEM_SIZE; i++) {
75+
unsafe.putDouble(unsafe_addrOut + (i * CARRIER_SIZE) , i);
76+
}
77+
segmentIn = MemorySegment.allocateNative(ALLOC_SIZE);
78+
segmentOut = MemorySegment.allocateNative(ALLOC_SIZE);
79+
for (int i = 0; i < ELEM_SIZE; i++) {
80+
MemoryAccess.setDoubleAtIndex(segmentIn, i, i);
81+
}
82+
for (int i = 0; i < ELEM_SIZE; i++) {
83+
MemoryAccess.setDoubleAtIndex(segmentOut, i, i);
84+
}
85+
byteBufferIn = ByteBuffer.allocateDirect(ALLOC_SIZE).order(ByteOrder.nativeOrder());
86+
byteBufferOut = ByteBuffer.allocateDirect(ALLOC_SIZE).order(ByteOrder.nativeOrder());
87+
for (int i = 0; i < ELEM_SIZE; i++) {
88+
byteBufferIn.putDouble(i * CARRIER_SIZE , i);
89+
}
90+
for (int i = 0; i < ELEM_SIZE; i++) {
91+
byteBufferOut.putDouble(i * CARRIER_SIZE , i);
92+
}
93+
}
94+
95+
@TearDown
96+
public void tearDown() {
97+
segmentIn.close();
98+
segmentOut.close();
99+
unsafe.invokeCleaner(byteBufferIn);
100+
unsafe.invokeCleaner(byteBufferOut);
101+
unsafe.freeMemory(unsafe_addrIn);
102+
unsafe.freeMemory(unsafe_addrOut);
103+
}
104+
105+
@Benchmark
106+
public void unsafe_loop() {
107+
for (int i = 0; i < ELEM_SIZE; i ++) {
108+
unsafe.putDouble(unsafe_addrOut + (i * CARRIER_SIZE),
109+
unsafe.getDouble(unsafe_addrIn + (i * CARRIER_SIZE)) +
110+
unsafe.getDouble(unsafe_addrOut + (i * CARRIER_SIZE)));
111+
}
112+
}
113+
114+
@Benchmark
115+
public void segment_loop() {
116+
for (int i = 0; i < ELEM_SIZE; i ++) {
117+
MemoryAccess.setDoubleAtIndex(segmentOut, i,
118+
MemoryAccess.getDoubleAtIndex(segmentIn, i) +
119+
MemoryAccess.getDoubleAtIndex(segmentOut, i));
120+
}
121+
}
122+
123+
@Benchmark
124+
public void BB_loop() {
125+
for (int i = 0; i < ELEM_SIZE; i++) {
126+
byteBufferOut.putDouble(i * CARRIER_SIZE,
127+
byteBufferIn.getDouble(i * CARRIER_SIZE) +
128+
byteBufferOut.getDouble(i * CARRIER_SIZE));
129+
}
130+
}
131+
}

0 commit comments

Comments
 (0)
Please sign in to comment.