Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/micro/org/openjdk/bench/java/nio/DoubleBuffers.java
41161 views
1
/*
2
* Copyright (c) 2020, 2021, 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.nio;
24
25
import org.openjdk.jmh.annotations.Benchmark;
26
import org.openjdk.jmh.annotations.BenchmarkMode;
27
import org.openjdk.jmh.annotations.Fork;
28
import org.openjdk.jmh.annotations.Measurement;
29
import org.openjdk.jmh.annotations.Mode;
30
import org.openjdk.jmh.annotations.OutputTimeUnit;
31
import org.openjdk.jmh.annotations.Param;
32
import org.openjdk.jmh.annotations.Scope;
33
import org.openjdk.jmh.annotations.Setup;
34
import org.openjdk.jmh.annotations.State;
35
import org.openjdk.jmh.annotations.Warmup;
36
import java.nio.*;
37
import java.util.concurrent.TimeUnit;
38
import static java.nio.ByteOrder.*;
39
40
/**
41
* Benchmark for memory access operations on java.nio.Buffer ( and its views )
42
*
43
* A large number of variants are covered. The individual benchmarks conform to
44
* the following convention:
45
* test(Direct|Heap)(Bulk|Loop)(Get|Put)Double(View)?(Swap)?(RO)?
46
*
47
* This allows to easily run a subset of particular interest. For example:
48
* Direct only :- "org.openjdk.bench.java.nio.DoubleBuffers.testDirect.*"
49
* Bulk only :- "org.openjdk.bench.java.nio.DoubleBuffers.test.*Bulk.*"
50
* Loop Put Swapped Views: -
51
* test(Direct|Heap)(Loop)(Put)Double(View)+(Swap)+"
52
*/
53
@BenchmarkMode(Mode.AverageTime)
54
@Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
55
@Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
56
@OutputTimeUnit(TimeUnit.NANOSECONDS)
57
@State(Scope.Thread)
58
@Fork(3)
59
public class DoubleBuffers {
60
61
static final int CARRIER_BYTE_WIDTH = 8;
62
63
@Param({"16", "1024", "131072"})
64
private int size;
65
66
public double doubleValue;
67
public double[] doubleArray;
68
69
public DoubleBuffer heapDoubleBuffer;
70
public DoubleBuffer heapDoubleBufferRO;
71
public DoubleBuffer heapByteBufferAsDoubleBufferView;
72
public DoubleBuffer heapByteBufferAsDoubleBufferViewRO;
73
public DoubleBuffer heapByteBufferAsDoubleBufferViewSwap;
74
public DoubleBuffer heapByteBufferAsDoubleBufferViewSwapRO;
75
public DoubleBuffer directByteBufferAsDoubleBufferView;
76
public DoubleBuffer directByteBufferAsDoubleBufferViewRO;
77
public DoubleBuffer directByteBufferAsDoubleBufferViewSwap;
78
public DoubleBuffer directByteBufferAsDoubleBufferViewSwapRO;
79
80
@Setup
81
public void setup() {
82
doubleArray = new double[size / CARRIER_BYTE_WIDTH];
83
84
// explicitly allocated heap carrier buffer
85
heapDoubleBuffer = DoubleBuffer.allocate(size / CARRIER_BYTE_WIDTH);
86
heapDoubleBufferRO = DoubleBuffer.allocate(size / CARRIER_BYTE_WIDTH).asReadOnlyBuffer();
87
88
// ByteBuffer views
89
heapByteBufferAsDoubleBufferView = ByteBuffer.allocate(size).order(nativeOrder()).asDoubleBuffer();
90
heapByteBufferAsDoubleBufferViewRO = ByteBuffer.allocate(size).order(nativeOrder()).asDoubleBuffer().asReadOnlyBuffer();
91
directByteBufferAsDoubleBufferView = ByteBuffer.allocateDirect(size).order(nativeOrder()).asDoubleBuffer();
92
directByteBufferAsDoubleBufferViewRO = ByteBuffer.allocateDirect(size).order(nativeOrder()).asDoubleBuffer().asReadOnlyBuffer();
93
94
// endianness swapped
95
ByteOrder nonNativeOrder = nativeOrder() == BIG_ENDIAN ? LITTLE_ENDIAN : BIG_ENDIAN;
96
heapByteBufferAsDoubleBufferViewSwap = ByteBuffer.allocate(size).order(nonNativeOrder).asDoubleBuffer();
97
heapByteBufferAsDoubleBufferViewSwapRO = ByteBuffer.allocate(size).order(nonNativeOrder).asDoubleBuffer().asReadOnlyBuffer();
98
directByteBufferAsDoubleBufferViewSwap = ByteBuffer.allocateDirect(size).order(nonNativeOrder).asDoubleBuffer();
99
directByteBufferAsDoubleBufferViewSwapRO = ByteBuffer.allocateDirect(size).order(nonNativeOrder).asDoubleBuffer().asReadOnlyBuffer();
100
}
101
102
// ---------------- HELPER METHODS
103
104
private int innerLoopGetDouble(DoubleBuffer buf) {
105
int r = 0;
106
for (int i = 0; i < buf.capacity(); i++) {
107
r += buf.get(i);
108
}
109
return r;
110
}
111
112
private void innerLoopPutDouble(DoubleBuffer buf) {
113
for (int i = 0; i < buf.capacity(); i++) {
114
buf.put(i, doubleValue);
115
}
116
}
117
118
// -- Heap___
119
120
@Benchmark
121
public double[] testHeapBulkPutDouble() {
122
heapDoubleBuffer.put(0, doubleArray);
123
return doubleArray;
124
}
125
126
@Benchmark
127
public double[] testHeapBulkGetDouble() {
128
heapDoubleBuffer.get(0, doubleArray);
129
return doubleArray;
130
}
131
132
@Benchmark
133
public void testHeapLoopPutDouble() {
134
innerLoopPutDouble(heapDoubleBuffer);
135
}
136
137
@Benchmark
138
public int testHeapLoopGetDouble() {
139
return innerLoopGetDouble(heapDoubleBuffer);
140
}
141
142
// -- Heap_View_Swap_RO
143
144
@Benchmark
145
public double[] testHeapBulkGetDoubleViewSwapRO() {
146
heapByteBufferAsDoubleBufferViewSwapRO.get(0, doubleArray);
147
return doubleArray;
148
}
149
150
@Benchmark
151
public int testHeapLoopGetDoubleViewSwapRO() {
152
return innerLoopGetDouble(heapByteBufferAsDoubleBufferViewSwapRO);
153
}
154
155
// -- Heap_View_Swap_
156
157
@Benchmark
158
public double[] testHeapBulkPutDoubleViewSwap() {
159
heapByteBufferAsDoubleBufferViewSwap.put(0, doubleArray);
160
return doubleArray;
161
}
162
163
@Benchmark
164
public double[] testHeapBulkGetDoubleViewSwap() {
165
heapByteBufferAsDoubleBufferViewSwap.get(0, doubleArray);
166
return doubleArray;
167
}
168
169
@Benchmark
170
public void testHeapLoopPutDoubleViewSwap() {
171
innerLoopPutDouble(heapByteBufferAsDoubleBufferViewSwap);
172
}
173
174
@Benchmark
175
public int testHeapLoopGetDoubleViewSwap() {
176
return innerLoopGetDouble(heapByteBufferAsDoubleBufferViewSwap);
177
}
178
179
// -- Heap_View__RO
180
181
@Benchmark
182
public double[] testHeapBulkGetDoubleViewRO() {
183
heapByteBufferAsDoubleBufferViewRO.get(0, doubleArray);
184
return doubleArray;
185
}
186
187
@Benchmark
188
public int testHeapLoopGetDoubleViewRO() {
189
return innerLoopGetDouble(heapByteBufferAsDoubleBufferViewRO);
190
}
191
192
// -- Heap_View__
193
194
@Benchmark
195
public double[] testHeapBulkPutDoubleView() {
196
heapByteBufferAsDoubleBufferView.put(0, doubleArray);
197
return doubleArray;
198
}
199
200
@Benchmark
201
public double[] testHeapBulkGetDoubleView() {
202
heapByteBufferAsDoubleBufferView.get(0, doubleArray);
203
return doubleArray;
204
}
205
206
@Benchmark
207
public void testHeapLoopPutDoubleView() {
208
innerLoopPutDouble(heapByteBufferAsDoubleBufferView);
209
}
210
211
@Benchmark
212
public int testHeapLoopGetDoubleView() {
213
return innerLoopGetDouble(heapByteBufferAsDoubleBufferView);
214
}
215
216
// -- Direct_View_Swap_RO
217
218
@Benchmark
219
public double[] testDirectBulkGetDoubleViewSwapRO() {
220
directByteBufferAsDoubleBufferViewSwapRO.get(0, doubleArray);
221
return doubleArray;
222
}
223
224
@Benchmark
225
public int testDirectLoopGetDoubleViewSwapRO() {
226
return innerLoopGetDouble(directByteBufferAsDoubleBufferViewSwapRO);
227
}
228
229
// -- Direct_View_Swap_
230
231
@Benchmark
232
public double[] testDirectBulkPutDoubleViewSwap() {
233
directByteBufferAsDoubleBufferViewSwap.put(0, doubleArray);
234
return doubleArray;
235
}
236
237
@Benchmark
238
public double[] testDirectBulkGetDoubleViewSwap() {
239
directByteBufferAsDoubleBufferViewSwap.get(0, doubleArray);
240
return doubleArray;
241
}
242
243
@Benchmark
244
public void testDirectLoopPutDoubleViewSwap() {
245
innerLoopPutDouble(directByteBufferAsDoubleBufferViewSwap);
246
}
247
248
@Benchmark
249
public int testDirectLoopGetDoubleViewSwap() {
250
return innerLoopGetDouble(directByteBufferAsDoubleBufferViewSwap);
251
}
252
253
// -- Direct_View__RO
254
255
@Benchmark
256
public double[] testDirectBulkGetDoubleViewRO() {
257
directByteBufferAsDoubleBufferViewRO.get(0, doubleArray);
258
return doubleArray;
259
}
260
261
@Benchmark
262
public int testDirectLoopGetDoubleViewRO() {
263
return innerLoopGetDouble(directByteBufferAsDoubleBufferViewRO);
264
}
265
266
// -- Direct_View__
267
268
@Benchmark
269
public double[] testDirectBulkPutDoubleView() {
270
directByteBufferAsDoubleBufferView.put(0, doubleArray);
271
return doubleArray;
272
}
273
274
@Benchmark
275
public double[] testDirectBulkGetDoubleView() {
276
directByteBufferAsDoubleBufferView.get(0, doubleArray);
277
return doubleArray;
278
}
279
280
@Benchmark
281
public void testDirectLoopPutDoubleView() {
282
innerLoopPutDouble(directByteBufferAsDoubleBufferView);
283
}
284
285
@Benchmark
286
public int testDirectLoopGetDoubleView() {
287
return innerLoopGetDouble(directByteBufferAsDoubleBufferView);
288
}
289
}
290
291