Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/micro/org/openjdk/bench/java/nio/IntBuffers.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)Int(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.IntBuffers.testDirect.*"
49
* Bulk only :- "org.openjdk.bench.java.nio.IntBuffers.test.*Bulk.*"
50
* Loop Put Swapped Views: -
51
* test(Direct|Heap)(Loop)(Put)Int(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 IntBuffers {
60
61
static final int CARRIER_BYTE_WIDTH = 4;
62
63
@Param({"16", "1024", "131072"})
64
private int size;
65
66
public int intValue;
67
public int[] intArray;
68
69
public IntBuffer heapIntBuffer;
70
public IntBuffer heapIntBufferRO;
71
public IntBuffer heapByteBufferAsIntBufferView;
72
public IntBuffer heapByteBufferAsIntBufferViewRO;
73
public IntBuffer heapByteBufferAsIntBufferViewSwap;
74
public IntBuffer heapByteBufferAsIntBufferViewSwapRO;
75
public IntBuffer directByteBufferAsIntBufferView;
76
public IntBuffer directByteBufferAsIntBufferViewRO;
77
public IntBuffer directByteBufferAsIntBufferViewSwap;
78
public IntBuffer directByteBufferAsIntBufferViewSwapRO;
79
80
@Setup
81
public void setup() {
82
intArray = new int[size / CARRIER_BYTE_WIDTH];
83
84
// explicitly allocated heap carrier buffer
85
heapIntBuffer = IntBuffer.allocate(size / CARRIER_BYTE_WIDTH);
86
heapIntBufferRO = IntBuffer.allocate(size / CARRIER_BYTE_WIDTH).asReadOnlyBuffer();
87
88
// ByteBuffer views
89
heapByteBufferAsIntBufferView = ByteBuffer.allocate(size).order(nativeOrder()).asIntBuffer();
90
heapByteBufferAsIntBufferViewRO = ByteBuffer.allocate(size).order(nativeOrder()).asIntBuffer().asReadOnlyBuffer();
91
directByteBufferAsIntBufferView = ByteBuffer.allocateDirect(size).order(nativeOrder()).asIntBuffer();
92
directByteBufferAsIntBufferViewRO = ByteBuffer.allocateDirect(size).order(nativeOrder()).asIntBuffer().asReadOnlyBuffer();
93
94
// endianness swapped
95
ByteOrder nonNativeOrder = nativeOrder() == BIG_ENDIAN ? LITTLE_ENDIAN : BIG_ENDIAN;
96
heapByteBufferAsIntBufferViewSwap = ByteBuffer.allocate(size).order(nonNativeOrder).asIntBuffer();
97
heapByteBufferAsIntBufferViewSwapRO = ByteBuffer.allocate(size).order(nonNativeOrder).asIntBuffer().asReadOnlyBuffer();
98
directByteBufferAsIntBufferViewSwap = ByteBuffer.allocateDirect(size).order(nonNativeOrder).asIntBuffer();
99
directByteBufferAsIntBufferViewSwapRO = ByteBuffer.allocateDirect(size).order(nonNativeOrder).asIntBuffer().asReadOnlyBuffer();
100
}
101
102
// ---------------- HELPER METHODS
103
104
private int innerLoopGetInt(IntBuffer 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 innerLoopPutInt(IntBuffer buf) {
113
for (int i = 0; i < buf.capacity(); i++) {
114
buf.put(i, intValue);
115
}
116
}
117
118
// -- Heap___
119
120
@Benchmark
121
public int[] testHeapBulkPutInt() {
122
heapIntBuffer.put(0, intArray);
123
return intArray;
124
}
125
126
@Benchmark
127
public int[] testHeapBulkGetInt() {
128
heapIntBuffer.get(0, intArray);
129
return intArray;
130
}
131
132
@Benchmark
133
public void testHeapLoopPutInt() {
134
innerLoopPutInt(heapIntBuffer);
135
}
136
137
@Benchmark
138
public int testHeapLoopGetInt() {
139
return innerLoopGetInt(heapIntBuffer);
140
}
141
142
// -- Heap_View_Swap_RO
143
144
@Benchmark
145
public int[] testHeapBulkGetIntViewSwapRO() {
146
heapByteBufferAsIntBufferViewSwapRO.get(0, intArray);
147
return intArray;
148
}
149
150
@Benchmark
151
public int testHeapLoopGetIntViewSwapRO() {
152
return innerLoopGetInt(heapByteBufferAsIntBufferViewSwapRO);
153
}
154
155
// -- Heap_View_Swap_
156
157
@Benchmark
158
public int[] testHeapBulkPutIntViewSwap() {
159
heapByteBufferAsIntBufferViewSwap.put(0, intArray);
160
return intArray;
161
}
162
163
@Benchmark
164
public int[] testHeapBulkGetIntViewSwap() {
165
heapByteBufferAsIntBufferViewSwap.get(0, intArray);
166
return intArray;
167
}
168
169
@Benchmark
170
public void testHeapLoopPutIntViewSwap() {
171
innerLoopPutInt(heapByteBufferAsIntBufferViewSwap);
172
}
173
174
@Benchmark
175
public int testHeapLoopGetIntViewSwap() {
176
return innerLoopGetInt(heapByteBufferAsIntBufferViewSwap);
177
}
178
179
// -- Heap_View__RO
180
181
@Benchmark
182
public int[] testHeapBulkGetIntViewRO() {
183
heapByteBufferAsIntBufferViewRO.get(0, intArray);
184
return intArray;
185
}
186
187
@Benchmark
188
public int testHeapLoopGetIntViewRO() {
189
return innerLoopGetInt(heapByteBufferAsIntBufferViewRO);
190
}
191
192
// -- Heap_View__
193
194
@Benchmark
195
public int[] testHeapBulkPutIntView() {
196
heapByteBufferAsIntBufferView.put(0, intArray);
197
return intArray;
198
}
199
200
@Benchmark
201
public int[] testHeapBulkGetIntView() {
202
heapByteBufferAsIntBufferView.get(0, intArray);
203
return intArray;
204
}
205
206
@Benchmark
207
public void testHeapLoopPutIntView() {
208
innerLoopPutInt(heapByteBufferAsIntBufferView);
209
}
210
211
@Benchmark
212
public int testHeapLoopGetIntView() {
213
return innerLoopGetInt(heapByteBufferAsIntBufferView);
214
}
215
216
// -- Direct_View_Swap_RO
217
218
@Benchmark
219
public int[] testDirectBulkGetIntViewSwapRO() {
220
directByteBufferAsIntBufferViewSwapRO.get(0, intArray);
221
return intArray;
222
}
223
224
@Benchmark
225
public int testDirectLoopGetIntViewSwapRO() {
226
return innerLoopGetInt(directByteBufferAsIntBufferViewSwapRO);
227
}
228
229
// -- Direct_View_Swap_
230
231
@Benchmark
232
public int[] testDirectBulkPutIntViewSwap() {
233
directByteBufferAsIntBufferViewSwap.put(0, intArray);
234
return intArray;
235
}
236
237
@Benchmark
238
public int[] testDirectBulkGetIntViewSwap() {
239
directByteBufferAsIntBufferViewSwap.get(0, intArray);
240
return intArray;
241
}
242
243
@Benchmark
244
public void testDirectLoopPutIntViewSwap() {
245
innerLoopPutInt(directByteBufferAsIntBufferViewSwap);
246
}
247
248
@Benchmark
249
public int testDirectLoopGetIntViewSwap() {
250
return innerLoopGetInt(directByteBufferAsIntBufferViewSwap);
251
}
252
253
// -- Direct_View__RO
254
255
@Benchmark
256
public int[] testDirectBulkGetIntViewRO() {
257
directByteBufferAsIntBufferViewRO.get(0, intArray);
258
return intArray;
259
}
260
261
@Benchmark
262
public int testDirectLoopGetIntViewRO() {
263
return innerLoopGetInt(directByteBufferAsIntBufferViewRO);
264
}
265
266
// -- Direct_View__
267
268
@Benchmark
269
public int[] testDirectBulkPutIntView() {
270
directByteBufferAsIntBufferView.put(0, intArray);
271
return intArray;
272
}
273
274
@Benchmark
275
public int[] testDirectBulkGetIntView() {
276
directByteBufferAsIntBufferView.get(0, intArray);
277
return intArray;
278
}
279
280
@Benchmark
281
public void testDirectLoopPutIntView() {
282
innerLoopPutInt(directByteBufferAsIntBufferView);
283
}
284
285
@Benchmark
286
public int testDirectLoopGetIntView() {
287
return innerLoopGetInt(directByteBufferAsIntBufferView);
288
}
289
}
290
291