Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/micro/org/openjdk/bench/java/lang/ArrayFiddle.java
41161 views
1
/*
2
* Copyright 2019 Google Inc. 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.lang;
24
25
import org.openjdk.jmh.annotations.*;
26
27
import java.util.Arrays;
28
import java.util.concurrent.ThreadLocalRandom;
29
import java.util.concurrent.TimeUnit;
30
31
/**
32
* Explores the cost of copying the contents of one array to another, as is
33
* commonly seen in collection classes that need to resize their backing
34
* array, like ArrayList.
35
*
36
* We have multiple variations on copying, and we explore the cost of
37
* clearing the old array, which might help for generational GCs.
38
*
39
* Benchmarks the operations in the ancient
40
* JDK-6428387: array clone() much slower than Arrays.copyOf
41
*
42
* 2019 results on x86:
43
*
44
* The "simple" benchmarks below have the same performance, except that
45
* simple_copyLoop is surprisingly 5x slower. The array copying intrinsics
46
* are very effective and a naive loop does not get optimized the same way.
47
* OTOH there is no intrinsic for Arrays.fill but the naive array zeroing loop
48
* *does* get optimized to something a little faster than than arraycopy.
49
*
50
* System.arraycopy and Arrays.fill have such outstanding performance that
51
* one should use them to replace handwritten loops whenever possible.
52
*
53
* This benchmark is great for measuring cache effects, e.g. size=10^6 has 5x
54
* the per-element cost of size=10^3 (See "The Myth of RAM".)
55
*
56
* (cd $(git rev-parse --show-toplevel) && for size in 3 16 999 999999; do make test TEST='micro:java.lang.ArrayFiddle' MICRO="FORK=2;WARMUP_ITER=4;ITER=4;OPTIONS=-opi $size -p size=$size" |& perl -ne 'print if /^Benchmark/ .. /^Finished running test/'; done)
57
*/
58
@BenchmarkMode(Mode.AverageTime)
59
@Fork(2)
60
@Warmup(iterations = 1)
61
@Measurement(iterations = 4)
62
@OutputTimeUnit(TimeUnit.NANOSECONDS)
63
@State(Scope.Benchmark)
64
public class ArrayFiddle {
65
@Param("999")
66
public int size;
67
68
public int largerSize;
69
public Object[] data;
70
public Object[] copy;
71
72
@Setup
73
public void setup() {
74
largerSize = size + (size >> 1);
75
76
data = new Object[size];
77
ThreadLocalRandom rnd = ThreadLocalRandom.current();
78
for (int i = data.length; i--> 0; )
79
data[i] = rnd.nextInt(256);
80
81
copy = data.clone();
82
}
83
84
// --- "simple" benchmarks just make an array clone
85
86
@Benchmark
87
public Object[] simple_clone() {
88
return data.clone();
89
}
90
91
@Benchmark
92
public Object[] simple_copyOf() {
93
return Arrays.copyOf(data, data.length);
94
}
95
96
@Benchmark
97
public Object[] simple_arraycopy() {
98
Object[] out = new Object[data.length];
99
System.arraycopy(data, 0, out, 0, data.length);
100
return out;
101
}
102
103
@Benchmark
104
public Object[] simple_copyLoop() {
105
final Object[] data = this.data;
106
int len = data.length;
107
Object[] out = new Object[len];
108
for (int i = 0; i < len; i++)
109
out[i] = data[i];
110
return out;
111
}
112
113
// --- "grow" benchmarks have an output array that is larger
114
115
private Object[] input_array() {
116
System.arraycopy(data, 0, copy, 0, size);
117
return copy;
118
}
119
120
@Benchmark
121
public Object[] grow_copyLoop() {
122
Object[] in = input_array();
123
Object[] out = new Object[largerSize];
124
for (int i = 0, len = in.length; i < len; i++)
125
out[i] = in[i];
126
return out;
127
}
128
129
@Benchmark
130
public Object[] grow_copyZeroLoop() {
131
Object[] in = input_array();
132
Object[] out = new Object[largerSize];
133
for (int i = 0, len = in.length; i < len; i++) {
134
out[i] = in[i];
135
in[i] = null;
136
}
137
return out;
138
}
139
140
@Benchmark
141
public Object[] grow_arraycopy() {
142
Object[] in = input_array();
143
Object[] out = new Object[largerSize];
144
System.arraycopy(in, 0, out, 0, size);
145
return out;
146
}
147
148
@Benchmark
149
public Object[] grow_arraycopy_fill() {
150
Object[] in = input_array();
151
Object[] out = new Object[largerSize];
152
System.arraycopy(in, 0, out, 0, size);
153
Arrays.fill(in, null);
154
return out;
155
}
156
157
@Benchmark
158
public Object[] grow_arraycopy_zeroLoop() {
159
Object[] in = input_array();
160
Object[] out = new Object[largerSize];
161
System.arraycopy(in, 0, out, 0, size);
162
for (int i = 0, len = in.length; i < len; i++)
163
in[i] = null;
164
return out;
165
}
166
167
@Benchmark
168
public Object[] grow_copyOf() {
169
Object[] in = input_array();
170
Object[] out = Arrays.copyOf(in, largerSize);
171
return out;
172
}
173
174
@Benchmark
175
public Object[] grow_copyOf_fill() {
176
Object[] in = input_array();
177
Object[] out = Arrays.copyOf(in, largerSize);
178
Arrays.fill(in, null);
179
return out;
180
}
181
182
@Benchmark
183
public Object[] grow_copyOf_zeroLoop() {
184
Object[] in = input_array();
185
Object[] out = Arrays.copyOf(in, largerSize);
186
for (int i = 0, len = in.length; i < len; i++)
187
in[i] = null;
188
return out;
189
}
190
191
}
192
193