Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/micro/org/openjdk/bench/java/nio/FloatBuffers.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)Float(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.FloatBuffers.testDirect.*"
49
* Bulk only :- "org.openjdk.bench.java.nio.FloatBuffers.test.*Bulk.*"
50
* Loop Put Swapped Views: -
51
* test(Direct|Heap)(Loop)(Put)Float(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 FloatBuffers {
60
61
static final int CARRIER_BYTE_WIDTH = 4;
62
63
@Param({"16", "1024", "131072"})
64
private int size;
65
66
public float floatValue;
67
public float[] floatArray;
68
69
public FloatBuffer heapFloatBuffer;
70
public FloatBuffer heapFloatBufferRO;
71
public FloatBuffer heapByteBufferAsFloatBufferView;
72
public FloatBuffer heapByteBufferAsFloatBufferViewRO;
73
public FloatBuffer heapByteBufferAsFloatBufferViewSwap;
74
public FloatBuffer heapByteBufferAsFloatBufferViewSwapRO;
75
public FloatBuffer directByteBufferAsFloatBufferView;
76
public FloatBuffer directByteBufferAsFloatBufferViewRO;
77
public FloatBuffer directByteBufferAsFloatBufferViewSwap;
78
public FloatBuffer directByteBufferAsFloatBufferViewSwapRO;
79
80
@Setup
81
public void setup() {
82
floatArray = new float[size / CARRIER_BYTE_WIDTH];
83
84
// explicitly allocated heap carrier buffer
85
heapFloatBuffer = FloatBuffer.allocate(size / CARRIER_BYTE_WIDTH);
86
heapFloatBufferRO = FloatBuffer.allocate(size / CARRIER_BYTE_WIDTH).asReadOnlyBuffer();
87
88
// ByteBuffer views
89
heapByteBufferAsFloatBufferView = ByteBuffer.allocate(size).order(nativeOrder()).asFloatBuffer();
90
heapByteBufferAsFloatBufferViewRO = ByteBuffer.allocate(size).order(nativeOrder()).asFloatBuffer().asReadOnlyBuffer();
91
directByteBufferAsFloatBufferView = ByteBuffer.allocateDirect(size).order(nativeOrder()).asFloatBuffer();
92
directByteBufferAsFloatBufferViewRO = ByteBuffer.allocateDirect(size).order(nativeOrder()).asFloatBuffer().asReadOnlyBuffer();
93
94
// endianness swapped
95
ByteOrder nonNativeOrder = nativeOrder() == BIG_ENDIAN ? LITTLE_ENDIAN : BIG_ENDIAN;
96
heapByteBufferAsFloatBufferViewSwap = ByteBuffer.allocate(size).order(nonNativeOrder).asFloatBuffer();
97
heapByteBufferAsFloatBufferViewSwapRO = ByteBuffer.allocate(size).order(nonNativeOrder).asFloatBuffer().asReadOnlyBuffer();
98
directByteBufferAsFloatBufferViewSwap = ByteBuffer.allocateDirect(size).order(nonNativeOrder).asFloatBuffer();
99
directByteBufferAsFloatBufferViewSwapRO = ByteBuffer.allocateDirect(size).order(nonNativeOrder).asFloatBuffer().asReadOnlyBuffer();
100
}
101
102
// ---------------- HELPER METHODS
103
104
private int innerLoopGetFloat(FloatBuffer 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 innerLoopPutFloat(FloatBuffer buf) {
113
for (int i = 0; i < buf.capacity(); i++) {
114
buf.put(i, floatValue);
115
}
116
}
117
118
// -- Heap___
119
120
@Benchmark
121
public float[] testHeapBulkPutFloat() {
122
heapFloatBuffer.put(0, floatArray);
123
return floatArray;
124
}
125
126
@Benchmark
127
public float[] testHeapBulkGetFloat() {
128
heapFloatBuffer.get(0, floatArray);
129
return floatArray;
130
}
131
132
@Benchmark
133
public void testHeapLoopPutFloat() {
134
innerLoopPutFloat(heapFloatBuffer);
135
}
136
137
@Benchmark
138
public int testHeapLoopGetFloat() {
139
return innerLoopGetFloat(heapFloatBuffer);
140
}
141
142
// -- Heap_View_Swap_RO
143
144
@Benchmark
145
public float[] testHeapBulkGetFloatViewSwapRO() {
146
heapByteBufferAsFloatBufferViewSwapRO.get(0, floatArray);
147
return floatArray;
148
}
149
150
@Benchmark
151
public int testHeapLoopGetFloatViewSwapRO() {
152
return innerLoopGetFloat(heapByteBufferAsFloatBufferViewSwapRO);
153
}
154
155
// -- Heap_View_Swap_
156
157
@Benchmark
158
public float[] testHeapBulkPutFloatViewSwap() {
159
heapByteBufferAsFloatBufferViewSwap.put(0, floatArray);
160
return floatArray;
161
}
162
163
@Benchmark
164
public float[] testHeapBulkGetFloatViewSwap() {
165
heapByteBufferAsFloatBufferViewSwap.get(0, floatArray);
166
return floatArray;
167
}
168
169
@Benchmark
170
public void testHeapLoopPutFloatViewSwap() {
171
innerLoopPutFloat(heapByteBufferAsFloatBufferViewSwap);
172
}
173
174
@Benchmark
175
public int testHeapLoopGetFloatViewSwap() {
176
return innerLoopGetFloat(heapByteBufferAsFloatBufferViewSwap);
177
}
178
179
// -- Heap_View__RO
180
181
@Benchmark
182
public float[] testHeapBulkGetFloatViewRO() {
183
heapByteBufferAsFloatBufferViewRO.get(0, floatArray);
184
return floatArray;
185
}
186
187
@Benchmark
188
public int testHeapLoopGetFloatViewRO() {
189
return innerLoopGetFloat(heapByteBufferAsFloatBufferViewRO);
190
}
191
192
// -- Heap_View__
193
194
@Benchmark
195
public float[] testHeapBulkPutFloatView() {
196
heapByteBufferAsFloatBufferView.put(0, floatArray);
197
return floatArray;
198
}
199
200
@Benchmark
201
public float[] testHeapBulkGetFloatView() {
202
heapByteBufferAsFloatBufferView.get(0, floatArray);
203
return floatArray;
204
}
205
206
@Benchmark
207
public void testHeapLoopPutFloatView() {
208
innerLoopPutFloat(heapByteBufferAsFloatBufferView);
209
}
210
211
@Benchmark
212
public int testHeapLoopGetFloatView() {
213
return innerLoopGetFloat(heapByteBufferAsFloatBufferView);
214
}
215
216
// -- Direct_View_Swap_RO
217
218
@Benchmark
219
public float[] testDirectBulkGetFloatViewSwapRO() {
220
directByteBufferAsFloatBufferViewSwapRO.get(0, floatArray);
221
return floatArray;
222
}
223
224
@Benchmark
225
public int testDirectLoopGetFloatViewSwapRO() {
226
return innerLoopGetFloat(directByteBufferAsFloatBufferViewSwapRO);
227
}
228
229
// -- Direct_View_Swap_
230
231
@Benchmark
232
public float[] testDirectBulkPutFloatViewSwap() {
233
directByteBufferAsFloatBufferViewSwap.put(0, floatArray);
234
return floatArray;
235
}
236
237
@Benchmark
238
public float[] testDirectBulkGetFloatViewSwap() {
239
directByteBufferAsFloatBufferViewSwap.get(0, floatArray);
240
return floatArray;
241
}
242
243
@Benchmark
244
public void testDirectLoopPutFloatViewSwap() {
245
innerLoopPutFloat(directByteBufferAsFloatBufferViewSwap);
246
}
247
248
@Benchmark
249
public int testDirectLoopGetFloatViewSwap() {
250
return innerLoopGetFloat(directByteBufferAsFloatBufferViewSwap);
251
}
252
253
// -- Direct_View__RO
254
255
@Benchmark
256
public float[] testDirectBulkGetFloatViewRO() {
257
directByteBufferAsFloatBufferViewRO.get(0, floatArray);
258
return floatArray;
259
}
260
261
@Benchmark
262
public int testDirectLoopGetFloatViewRO() {
263
return innerLoopGetFloat(directByteBufferAsFloatBufferViewRO);
264
}
265
266
// -- Direct_View__
267
268
@Benchmark
269
public float[] testDirectBulkPutFloatView() {
270
directByteBufferAsFloatBufferView.put(0, floatArray);
271
return floatArray;
272
}
273
274
@Benchmark
275
public float[] testDirectBulkGetFloatView() {
276
directByteBufferAsFloatBufferView.get(0, floatArray);
277
return floatArray;
278
}
279
280
@Benchmark
281
public void testDirectLoopPutFloatView() {
282
innerLoopPutFloat(directByteBufferAsFloatBufferView);
283
}
284
285
@Benchmark
286
public int testDirectLoopGetFloatView() {
287
return innerLoopGetFloat(directByteBufferAsFloatBufferView);
288
}
289
}
290
291