Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/micro/org/openjdk/bench/java/nio/LongBuffers.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)Long(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.LongBuffers.testDirect.*"
49
* Bulk only :- "org.openjdk.bench.java.nio.LongBuffers.test.*Bulk.*"
50
* Loop Put Swapped Views: -
51
* test(Direct|Heap)(Loop)(Put)Long(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 LongBuffers {
60
61
static final int CARRIER_BYTE_WIDTH = 8;
62
63
@Param({"16", "1024", "131072"})
64
private int size;
65
66
public long longValue;
67
public long[] longArray;
68
69
public LongBuffer heapLongBuffer;
70
public LongBuffer heapLongBufferRO;
71
public LongBuffer heapByteBufferAsLongBufferView;
72
public LongBuffer heapByteBufferAsLongBufferViewRO;
73
public LongBuffer heapByteBufferAsLongBufferViewSwap;
74
public LongBuffer heapByteBufferAsLongBufferViewSwapRO;
75
public LongBuffer directByteBufferAsLongBufferView;
76
public LongBuffer directByteBufferAsLongBufferViewRO;
77
public LongBuffer directByteBufferAsLongBufferViewSwap;
78
public LongBuffer directByteBufferAsLongBufferViewSwapRO;
79
80
@Setup
81
public void setup() {
82
longArray = new long[size / CARRIER_BYTE_WIDTH];
83
84
// explicitly allocated heap carrier buffer
85
heapLongBuffer = LongBuffer.allocate(size / CARRIER_BYTE_WIDTH);
86
heapLongBufferRO = LongBuffer.allocate(size / CARRIER_BYTE_WIDTH).asReadOnlyBuffer();
87
88
// ByteBuffer views
89
heapByteBufferAsLongBufferView = ByteBuffer.allocate(size).order(nativeOrder()).asLongBuffer();
90
heapByteBufferAsLongBufferViewRO = ByteBuffer.allocate(size).order(nativeOrder()).asLongBuffer().asReadOnlyBuffer();
91
directByteBufferAsLongBufferView = ByteBuffer.allocateDirect(size).order(nativeOrder()).asLongBuffer();
92
directByteBufferAsLongBufferViewRO = ByteBuffer.allocateDirect(size).order(nativeOrder()).asLongBuffer().asReadOnlyBuffer();
93
94
// endianness swapped
95
ByteOrder nonNativeOrder = nativeOrder() == BIG_ENDIAN ? LITTLE_ENDIAN : BIG_ENDIAN;
96
heapByteBufferAsLongBufferViewSwap = ByteBuffer.allocate(size).order(nonNativeOrder).asLongBuffer();
97
heapByteBufferAsLongBufferViewSwapRO = ByteBuffer.allocate(size).order(nonNativeOrder).asLongBuffer().asReadOnlyBuffer();
98
directByteBufferAsLongBufferViewSwap = ByteBuffer.allocateDirect(size).order(nonNativeOrder).asLongBuffer();
99
directByteBufferAsLongBufferViewSwapRO = ByteBuffer.allocateDirect(size).order(nonNativeOrder).asLongBuffer().asReadOnlyBuffer();
100
}
101
102
// ---------------- HELPER METHODS
103
104
private int innerLoopGetLong(LongBuffer 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 innerLoopPutLong(LongBuffer buf) {
113
for (int i = 0; i < buf.capacity(); i++) {
114
buf.put(i, longValue);
115
}
116
}
117
118
// -- Heap___
119
120
@Benchmark
121
public long[] testHeapBulkPutLong() {
122
heapLongBuffer.put(0, longArray);
123
return longArray;
124
}
125
126
@Benchmark
127
public long[] testHeapBulkGetLong() {
128
heapLongBuffer.get(0, longArray);
129
return longArray;
130
}
131
132
@Benchmark
133
public void testHeapLoopPutLong() {
134
innerLoopPutLong(heapLongBuffer);
135
}
136
137
@Benchmark
138
public int testHeapLoopGetLong() {
139
return innerLoopGetLong(heapLongBuffer);
140
}
141
142
// -- Heap_View_Swap_RO
143
144
@Benchmark
145
public long[] testHeapBulkGetLongViewSwapRO() {
146
heapByteBufferAsLongBufferViewSwapRO.get(0, longArray);
147
return longArray;
148
}
149
150
@Benchmark
151
public int testHeapLoopGetLongViewSwapRO() {
152
return innerLoopGetLong(heapByteBufferAsLongBufferViewSwapRO);
153
}
154
155
// -- Heap_View_Swap_
156
157
@Benchmark
158
public long[] testHeapBulkPutLongViewSwap() {
159
heapByteBufferAsLongBufferViewSwap.put(0, longArray);
160
return longArray;
161
}
162
163
@Benchmark
164
public long[] testHeapBulkGetLongViewSwap() {
165
heapByteBufferAsLongBufferViewSwap.get(0, longArray);
166
return longArray;
167
}
168
169
@Benchmark
170
public void testHeapLoopPutLongViewSwap() {
171
innerLoopPutLong(heapByteBufferAsLongBufferViewSwap);
172
}
173
174
@Benchmark
175
public int testHeapLoopGetLongViewSwap() {
176
return innerLoopGetLong(heapByteBufferAsLongBufferViewSwap);
177
}
178
179
// -- Heap_View__RO
180
181
@Benchmark
182
public long[] testHeapBulkGetLongViewRO() {
183
heapByteBufferAsLongBufferViewRO.get(0, longArray);
184
return longArray;
185
}
186
187
@Benchmark
188
public int testHeapLoopGetLongViewRO() {
189
return innerLoopGetLong(heapByteBufferAsLongBufferViewRO);
190
}
191
192
// -- Heap_View__
193
194
@Benchmark
195
public long[] testHeapBulkPutLongView() {
196
heapByteBufferAsLongBufferView.put(0, longArray);
197
return longArray;
198
}
199
200
@Benchmark
201
public long[] testHeapBulkGetLongView() {
202
heapByteBufferAsLongBufferView.get(0, longArray);
203
return longArray;
204
}
205
206
@Benchmark
207
public void testHeapLoopPutLongView() {
208
innerLoopPutLong(heapByteBufferAsLongBufferView);
209
}
210
211
@Benchmark
212
public int testHeapLoopGetLongView() {
213
return innerLoopGetLong(heapByteBufferAsLongBufferView);
214
}
215
216
// -- Direct_View_Swap_RO
217
218
@Benchmark
219
public long[] testDirectBulkGetLongViewSwapRO() {
220
directByteBufferAsLongBufferViewSwapRO.get(0, longArray);
221
return longArray;
222
}
223
224
@Benchmark
225
public int testDirectLoopGetLongViewSwapRO() {
226
return innerLoopGetLong(directByteBufferAsLongBufferViewSwapRO);
227
}
228
229
// -- Direct_View_Swap_
230
231
@Benchmark
232
public long[] testDirectBulkPutLongViewSwap() {
233
directByteBufferAsLongBufferViewSwap.put(0, longArray);
234
return longArray;
235
}
236
237
@Benchmark
238
public long[] testDirectBulkGetLongViewSwap() {
239
directByteBufferAsLongBufferViewSwap.get(0, longArray);
240
return longArray;
241
}
242
243
@Benchmark
244
public void testDirectLoopPutLongViewSwap() {
245
innerLoopPutLong(directByteBufferAsLongBufferViewSwap);
246
}
247
248
@Benchmark
249
public int testDirectLoopGetLongViewSwap() {
250
return innerLoopGetLong(directByteBufferAsLongBufferViewSwap);
251
}
252
253
// -- Direct_View__RO
254
255
@Benchmark
256
public long[] testDirectBulkGetLongViewRO() {
257
directByteBufferAsLongBufferViewRO.get(0, longArray);
258
return longArray;
259
}
260
261
@Benchmark
262
public int testDirectLoopGetLongViewRO() {
263
return innerLoopGetLong(directByteBufferAsLongBufferViewRO);
264
}
265
266
// -- Direct_View__
267
268
@Benchmark
269
public long[] testDirectBulkPutLongView() {
270
directByteBufferAsLongBufferView.put(0, longArray);
271
return longArray;
272
}
273
274
@Benchmark
275
public long[] testDirectBulkGetLongView() {
276
directByteBufferAsLongBufferView.get(0, longArray);
277
return longArray;
278
}
279
280
@Benchmark
281
public void testDirectLoopPutLongView() {
282
innerLoopPutLong(directByteBufferAsLongBufferView);
283
}
284
285
@Benchmark
286
public int testDirectLoopGetLongView() {
287
return innerLoopGetLong(directByteBufferAsLongBufferView);
288
}
289
}
290
291