|
1 /* |
|
2 * Copyright (c) 2019 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.util; |
|
24 |
|
25 import org.openjdk.jmh.annotations.*; |
|
26 import org.openjdk.jmh.infra.Blackhole; |
|
27 |
|
28 import java.util.*; |
|
29 import java.util.concurrent.TimeUnit; |
|
30 |
|
31 /** |
|
32 * Micros for the various collections implemented in |
|
33 * java.util.ImmutableCollections |
|
34 */ |
|
35 @State(Scope.Benchmark) |
|
36 @OutputTimeUnit(TimeUnit.MICROSECONDS) |
|
37 public class ImmutableColls { |
|
38 |
|
39 public static String[] STRINGS = {"hi", "all", "of", "you"}; |
|
40 |
|
41 public static List<String> l0 = List.of(); |
|
42 public static List<String> l1 = List.of(Arrays.copyOf(STRINGS, 1)); |
|
43 public static List<String> l2 = List.of(Arrays.copyOf(STRINGS, 2)); |
|
44 public static List<String> l3 = List.of(Arrays.copyOf(STRINGS, 3)); |
|
45 public static List<String> l4 = List.of(Arrays.copyOf(STRINGS, 4)); |
|
46 |
|
47 public static Set<String> s0 = Set.copyOf(l0); |
|
48 public static Set<String> s1 = Set.copyOf(l1); |
|
49 public static Set<String> s2 = Set.copyOf(l2); |
|
50 public static Set<String> s3 = Set.copyOf(l3); |
|
51 public static Set<String> s4 = Set.copyOf(l4); |
|
52 |
|
53 public static Map<String, String> m0 = Map.of(); |
|
54 public static Map<String, String> m1 = Map.of(STRINGS[0], STRINGS[0]); |
|
55 public static Map<String, String> m2 = Map.of(STRINGS[0], STRINGS[0], |
|
56 STRINGS[1], STRINGS[1]); |
|
57 public static Map<String, String> m3 = Map.of(STRINGS[0], STRINGS[0], |
|
58 STRINGS[1], STRINGS[1], |
|
59 STRINGS[2], STRINGS[2]); |
|
60 public static Map<String, String> m4 = Map.of(STRINGS[0], STRINGS[0], |
|
61 STRINGS[1], STRINGS[1], |
|
62 STRINGS[2], STRINGS[2], |
|
63 STRINGS[3], STRINGS[3]); |
|
64 |
|
65 public static List<String> a0 = new ArrayList<>(l0); |
|
66 public static List<String> a1 = new ArrayList<>(l1); |
|
67 public static List<String> a2 = new ArrayList<>(l2); |
|
68 public static List<String> a3 = new ArrayList<>(l3); |
|
69 public static List<String> a4 = new ArrayList<>(l4); |
|
70 |
|
71 public static final List<String> fl0 = List.of(); |
|
72 public static final List<String> fl1 = List.copyOf(l1); |
|
73 public static final List<String> fl2 = List.copyOf(l2); |
|
74 public static final List<String> fl3 = List.copyOf(l3); |
|
75 public static final List<String> fl4 = List.copyOf(l4); |
|
76 |
|
77 public static final List<String> fsl0 = fl0.subList(0, 0); |
|
78 public static final List<String> fsl1 = fl2.subList(1, 2); |
|
79 public static final List<String> fsl2 = fl3.subList(0, 2); |
|
80 public static final List<String> fsl3 = fl4.subList(0, 3); |
|
81 |
|
82 public static final Set<String> fs0 = Set.copyOf(l0); |
|
83 public static final Set<String> fs1 = Set.copyOf(l1); |
|
84 public static final Set<String> fs2 = Set.copyOf(l2); |
|
85 public static final Set<String> fs3 = Set.copyOf(l3); |
|
86 public static final Set<String> fs4 = Set.copyOf(l4); |
|
87 |
|
88 public static final Map<String, String> fm0 = Map.copyOf(m0); |
|
89 public static final Map<String, String> fm1 = Map.copyOf(m1); |
|
90 public static final Map<String, String> fm2 = Map.copyOf(m2); |
|
91 public static final Map<String, String> fm3 = Map.copyOf(m3); |
|
92 public static final Map<String, String> fm4 = Map.copyOf(m4); |
|
93 |
|
94 public static final List<String> fa0 = new ArrayList<>(l0); |
|
95 public static final List<String> fa1 = new ArrayList<>(l1); |
|
96 public static final List<String> fa2 = new ArrayList<>(l2); |
|
97 public static final List<String> fa3 = new ArrayList<>(l3); |
|
98 public static final List<String> fa4 = new ArrayList<>(l4); |
|
99 |
|
100 @Benchmark |
|
101 @CompilerControl(CompilerControl.Mode.DONT_INLINE) |
|
102 public int sumSizesList() { |
|
103 return sizeOf(l0) + |
|
104 sizeOf(l1) + |
|
105 sizeOf(l2) + |
|
106 sizeOf(l3) + |
|
107 sizeOf(l4); |
|
108 } |
|
109 |
|
110 @Benchmark |
|
111 @CompilerControl(CompilerControl.Mode.DONT_INLINE) |
|
112 public int finalSumSizesList() { |
|
113 return sizeOf(fl0) + |
|
114 sizeOf(fl1) + |
|
115 sizeOf(fl2) + |
|
116 sizeOf(fl3) + |
|
117 sizeOf(fl4); |
|
118 } |
|
119 |
|
120 @Benchmark |
|
121 @CompilerControl(CompilerControl.Mode.DONT_INLINE) |
|
122 public int sumSizesArrayList() { |
|
123 return sizeOf2(a0) + |
|
124 sizeOf2(a1) + |
|
125 sizeOf2(a2) + |
|
126 sizeOf2(a3) + |
|
127 sizeOf2(a4); |
|
128 } |
|
129 |
|
130 @Benchmark |
|
131 @CompilerControl(CompilerControl.Mode.DONT_INLINE) |
|
132 public int finalSumSizesArrayList() { |
|
133 return sizeOf2(fa0) + |
|
134 sizeOf2(fa1) + |
|
135 sizeOf2(fa2) + |
|
136 sizeOf2(fa3) + |
|
137 sizeOf2(fa4); |
|
138 } |
|
139 |
|
140 public int sizeOf2(List<String> list) { |
|
141 return list.size(); |
|
142 } |
|
143 |
|
144 public int sizeOf(List<String> list) { |
|
145 return list.size(); |
|
146 } |
|
147 |
|
148 @Benchmark |
|
149 @CompilerControl(CompilerControl.Mode.DONT_INLINE) |
|
150 public int getFromList() { |
|
151 return get(l1, 0).length() + |
|
152 get(l2, 1).length() + |
|
153 get(l3, 2).length() + |
|
154 get(l4, 3).length(); |
|
155 } |
|
156 |
|
157 @Benchmark |
|
158 @CompilerControl(CompilerControl.Mode.DONT_INLINE) |
|
159 public int finalGetFromList() { |
|
160 return get(fl1, 0).length() + |
|
161 get(fl2, 1).length() + |
|
162 get(fl3, 2).length() + |
|
163 get(fl4, 3).length(); |
|
164 } |
|
165 |
|
166 @Benchmark |
|
167 @CompilerControl(CompilerControl.Mode.DONT_INLINE) |
|
168 public void toArrayFromSet(Blackhole bh) { |
|
169 bh.consume(fs4.toArray()); |
|
170 bh.consume(s1.toArray()); |
|
171 bh.consume(s3.toArray()); |
|
172 bh.consume(fs2.toArray()); |
|
173 bh.consume(s0.toArray()); |
|
174 } |
|
175 |
|
176 @Benchmark |
|
177 @CompilerControl(CompilerControl.Mode.DONT_INLINE) |
|
178 public void toArrayFromMap(Blackhole bh) { |
|
179 bh.consume(fm4.entrySet().toArray()); |
|
180 bh.consume(m1.entrySet().toArray()); |
|
181 bh.consume(m3.entrySet().toArray()); |
|
182 bh.consume(fm2.entrySet().toArray()); |
|
183 bh.consume(m0.entrySet().toArray()); |
|
184 } |
|
185 |
|
186 @Benchmark |
|
187 @CompilerControl(CompilerControl.Mode.DONT_INLINE) |
|
188 public void toArrayFromList(Blackhole bh) { |
|
189 bh.consume(fl4.toArray()); |
|
190 bh.consume(fl1.toArray()); |
|
191 bh.consume(l3.toArray()); |
|
192 bh.consume(l0.toArray()); |
|
193 bh.consume(fsl3.toArray()); |
|
194 } |
|
195 |
|
196 @Benchmark |
|
197 @CompilerControl(CompilerControl.Mode.DONT_INLINE) |
|
198 public void toTypedArrayFromSet(Blackhole bh) { |
|
199 bh.consume(fs4.toArray(new String[0])); |
|
200 bh.consume(s1.toArray(new String[0])); |
|
201 bh.consume(s3.toArray(new String[0])); |
|
202 bh.consume(fs2.toArray(new String[0])); |
|
203 bh.consume(s0.toArray(new String[0])); |
|
204 } |
|
205 |
|
206 @Benchmark |
|
207 @CompilerControl(CompilerControl.Mode.DONT_INLINE) |
|
208 public void toTypedArrayFromMap(Blackhole bh) { |
|
209 bh.consume(fm4.entrySet().toArray(new Map.Entry[0])); |
|
210 bh.consume(m1.entrySet().toArray(new Map.Entry[0])); |
|
211 bh.consume(m3.entrySet().toArray(new Map.Entry[0])); |
|
212 bh.consume(fm2.entrySet().toArray(new Map.Entry[0])); |
|
213 bh.consume(m0.entrySet().toArray(new Map.Entry[0])); |
|
214 } |
|
215 |
|
216 @Benchmark |
|
217 @CompilerControl(CompilerControl.Mode.DONT_INLINE) |
|
218 public void toTypedArrayFromList(Blackhole bh) { |
|
219 bh.consume(fl4.toArray(new String[0])); |
|
220 bh.consume(fl1.toArray(new String[0])); |
|
221 bh.consume(l3.toArray(new String[0])); |
|
222 bh.consume(l0.toArray(new String[0])); |
|
223 bh.consume(fsl3.toArray(new String[0])); |
|
224 } |
|
225 |
|
226 @Benchmark |
|
227 @CompilerControl(CompilerControl.Mode.DONT_INLINE) |
|
228 public void copyOfLists(Blackhole bh) { |
|
229 bh.consume(List.copyOf(fl1)); |
|
230 bh.consume(List.copyOf(fl4)); |
|
231 bh.consume(List.copyOf(fsl2)); |
|
232 bh.consume(List.copyOf(fsl3)); |
|
233 } |
|
234 |
|
235 @Benchmark |
|
236 @CompilerControl(CompilerControl.Mode.DONT_INLINE) |
|
237 public int finalGetFromArrayList() { |
|
238 return get2(fa1, 0).length() + |
|
239 get2(fa2, 1).length() + |
|
240 get2(fa3, 2).length() + |
|
241 get2(fa4, 3).length(); |
|
242 } |
|
243 |
|
244 public String get2(List<String> list, int idx) { |
|
245 return list.get(idx); |
|
246 } |
|
247 |
|
248 public String get(List<String> list, int idx) { |
|
249 return list.get(idx); |
|
250 } |
|
251 |
|
252 @Benchmark |
|
253 @CompilerControl(CompilerControl.Mode.DONT_INLINE) |
|
254 public Set<String> createSetOf() { |
|
255 return Set.of(STRINGS); |
|
256 } |
|
257 } |