Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/micro/org/openjdk/bench/java/nio/CharBuffers.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)Char(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.CharBuffers.testDirect.*"
49
* Bulk only :- "org.openjdk.bench.java.nio.CharBuffers.test.*Bulk.*"
50
* Loop Put Swapped Views: -
51
* test(Direct|Heap)(Loop)(Put)Char(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 CharBuffers {
60
61
static final int CARRIER_BYTE_WIDTH = 2;
62
63
@Param({"16", "1024", "131072"})
64
private int size;
65
66
public char charValue;
67
public char[] charArray;
68
69
public CharBuffer heapCharBuffer;
70
public CharBuffer heapCharBufferRO;
71
public CharBuffer heapByteBufferAsCharBufferView;
72
public CharBuffer heapByteBufferAsCharBufferViewRO;
73
public CharBuffer heapByteBufferAsCharBufferViewSwap;
74
public CharBuffer heapByteBufferAsCharBufferViewSwapRO;
75
public CharBuffer directByteBufferAsCharBufferView;
76
public CharBuffer directByteBufferAsCharBufferViewRO;
77
public CharBuffer directByteBufferAsCharBufferViewSwap;
78
public CharBuffer directByteBufferAsCharBufferViewSwapRO;
79
80
@Setup
81
public void setup() {
82
charArray = new char[size / CARRIER_BYTE_WIDTH];
83
84
// explicitly allocated heap carrier buffer
85
heapCharBuffer = CharBuffer.allocate(size / CARRIER_BYTE_WIDTH);
86
heapCharBufferRO = CharBuffer.allocate(size / CARRIER_BYTE_WIDTH).asReadOnlyBuffer();
87
88
// ByteBuffer views
89
heapByteBufferAsCharBufferView = ByteBuffer.allocate(size).order(nativeOrder()).asCharBuffer();
90
heapByteBufferAsCharBufferViewRO = ByteBuffer.allocate(size).order(nativeOrder()).asCharBuffer().asReadOnlyBuffer();
91
directByteBufferAsCharBufferView = ByteBuffer.allocateDirect(size).order(nativeOrder()).asCharBuffer();
92
directByteBufferAsCharBufferViewRO = ByteBuffer.allocateDirect(size).order(nativeOrder()).asCharBuffer().asReadOnlyBuffer();
93
94
// endianness swapped
95
ByteOrder nonNativeOrder = nativeOrder() == BIG_ENDIAN ? LITTLE_ENDIAN : BIG_ENDIAN;
96
heapByteBufferAsCharBufferViewSwap = ByteBuffer.allocate(size).order(nonNativeOrder).asCharBuffer();
97
heapByteBufferAsCharBufferViewSwapRO = ByteBuffer.allocate(size).order(nonNativeOrder).asCharBuffer().asReadOnlyBuffer();
98
directByteBufferAsCharBufferViewSwap = ByteBuffer.allocateDirect(size).order(nonNativeOrder).asCharBuffer();
99
directByteBufferAsCharBufferViewSwapRO = ByteBuffer.allocateDirect(size).order(nonNativeOrder).asCharBuffer().asReadOnlyBuffer();
100
}
101
102
// ---------------- HELPER METHODS
103
104
private int innerLoopGetChar(CharBuffer 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 innerLoopPutChar(CharBuffer buf) {
113
for (int i = 0; i < buf.capacity(); i++) {
114
buf.put(i, charValue);
115
}
116
}
117
118
// -- Heap___
119
120
@Benchmark
121
public char[] testHeapBulkPutChar() {
122
heapCharBuffer.put(0, charArray);
123
return charArray;
124
}
125
126
@Benchmark
127
public char[] testHeapBulkGetChar() {
128
heapCharBuffer.get(0, charArray);
129
return charArray;
130
}
131
132
@Benchmark
133
public void testHeapLoopPutChar() {
134
innerLoopPutChar(heapCharBuffer);
135
}
136
137
@Benchmark
138
public int testHeapLoopGetChar() {
139
return innerLoopGetChar(heapCharBuffer);
140
}
141
142
// -- Heap_View_Swap_RO
143
144
@Benchmark
145
public char[] testHeapBulkGetCharViewSwapRO() {
146
heapByteBufferAsCharBufferViewSwapRO.get(0, charArray);
147
return charArray;
148
}
149
150
@Benchmark
151
public int testHeapLoopGetCharViewSwapRO() {
152
return innerLoopGetChar(heapByteBufferAsCharBufferViewSwapRO);
153
}
154
155
// -- Heap_View_Swap_
156
157
@Benchmark
158
public char[] testHeapBulkPutCharViewSwap() {
159
heapByteBufferAsCharBufferViewSwap.put(0, charArray);
160
return charArray;
161
}
162
163
@Benchmark
164
public char[] testHeapBulkGetCharViewSwap() {
165
heapByteBufferAsCharBufferViewSwap.get(0, charArray);
166
return charArray;
167
}
168
169
@Benchmark
170
public void testHeapLoopPutCharViewSwap() {
171
innerLoopPutChar(heapByteBufferAsCharBufferViewSwap);
172
}
173
174
@Benchmark
175
public int testHeapLoopGetCharViewSwap() {
176
return innerLoopGetChar(heapByteBufferAsCharBufferViewSwap);
177
}
178
179
// -- Heap_View__RO
180
181
@Benchmark
182
public char[] testHeapBulkGetCharViewRO() {
183
heapByteBufferAsCharBufferViewRO.get(0, charArray);
184
return charArray;
185
}
186
187
@Benchmark
188
public int testHeapLoopGetCharViewRO() {
189
return innerLoopGetChar(heapByteBufferAsCharBufferViewRO);
190
}
191
192
// -- Heap_View__
193
194
@Benchmark
195
public char[] testHeapBulkPutCharView() {
196
heapByteBufferAsCharBufferView.put(0, charArray);
197
return charArray;
198
}
199
200
@Benchmark
201
public char[] testHeapBulkGetCharView() {
202
heapByteBufferAsCharBufferView.get(0, charArray);
203
return charArray;
204
}
205
206
@Benchmark
207
public void testHeapLoopPutCharView() {
208
innerLoopPutChar(heapByteBufferAsCharBufferView);
209
}
210
211
@Benchmark
212
public int testHeapLoopGetCharView() {
213
return innerLoopGetChar(heapByteBufferAsCharBufferView);
214
}
215
216
// -- Direct_View_Swap_RO
217
218
@Benchmark
219
public char[] testDirectBulkGetCharViewSwapRO() {
220
directByteBufferAsCharBufferViewSwapRO.get(0, charArray);
221
return charArray;
222
}
223
224
@Benchmark
225
public int testDirectLoopGetCharViewSwapRO() {
226
return innerLoopGetChar(directByteBufferAsCharBufferViewSwapRO);
227
}
228
229
// -- Direct_View_Swap_
230
231
@Benchmark
232
public char[] testDirectBulkPutCharViewSwap() {
233
directByteBufferAsCharBufferViewSwap.put(0, charArray);
234
return charArray;
235
}
236
237
@Benchmark
238
public char[] testDirectBulkGetCharViewSwap() {
239
directByteBufferAsCharBufferViewSwap.get(0, charArray);
240
return charArray;
241
}
242
243
@Benchmark
244
public void testDirectLoopPutCharViewSwap() {
245
innerLoopPutChar(directByteBufferAsCharBufferViewSwap);
246
}
247
248
@Benchmark
249
public int testDirectLoopGetCharViewSwap() {
250
return innerLoopGetChar(directByteBufferAsCharBufferViewSwap);
251
}
252
253
// -- Direct_View__RO
254
255
@Benchmark
256
public char[] testDirectBulkGetCharViewRO() {
257
directByteBufferAsCharBufferViewRO.get(0, charArray);
258
return charArray;
259
}
260
261
@Benchmark
262
public int testDirectLoopGetCharViewRO() {
263
return innerLoopGetChar(directByteBufferAsCharBufferViewRO);
264
}
265
266
// -- Direct_View__
267
268
@Benchmark
269
public char[] testDirectBulkPutCharView() {
270
directByteBufferAsCharBufferView.put(0, charArray);
271
return charArray;
272
}
273
274
@Benchmark
275
public char[] testDirectBulkGetCharView() {
276
directByteBufferAsCharBufferView.get(0, charArray);
277
return charArray;
278
}
279
280
@Benchmark
281
public void testDirectLoopPutCharView() {
282
innerLoopPutChar(directByteBufferAsCharBufferView);
283
}
284
285
@Benchmark
286
public int testDirectLoopGetCharView() {
287
return innerLoopGetChar(directByteBufferAsCharBufferView);
288
}
289
}
290
291