-
Notifications
You must be signed in to change notification settings - Fork 585
/
IntIntMapSmallStressTest.java
404 lines (366 loc) · 12.7 KB
/
IntIntMapSmallStressTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*
* Copyright (c) 2024 Goldman Sachs and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*/
package org.eclipse.collections.impl.jmh;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import com.koloboke.collect.map.IntIntMap;
import com.koloboke.collect.map.hash.HashIntIntMaps;
import org.eclipse.collections.api.block.function.primitive.IntToObjectFunction;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.list.primitive.MutableIntList;
import org.eclipse.collections.api.map.primitive.MutableIntIntMap;
import org.eclipse.collections.api.set.primitive.MutableIntSet;
import org.eclipse.collections.impl.SpreadFunctions;
import org.eclipse.collections.impl.jmh.runner.AbstractJMHTestRunner;
import org.eclipse.collections.impl.list.mutable.FastList;
import org.eclipse.collections.impl.list.mutable.primitive.IntArrayList;
import org.eclipse.collections.impl.map.mutable.primitive.IntIntHashMap;
import org.eclipse.collections.impl.set.mutable.primitive.IntHashSet;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;
@State(Scope.Thread)
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.SECONDS)
@Fork(2)
@Warmup(iterations = 10, time = 2)
@Measurement(iterations = 10, time = 2)
public class IntIntMapSmallStressTest
{
private static final int LOOP_COUNT = 100;
private static final int KEY_COUNT = 500;
private static final int MAP_SIZE = 1_000;
@Param({"true", "false"})
public boolean fullyRandom;
private IntIntMap intIntKoloboke;
private MutableIntIntMap intIntEc;
private Map<Integer, Integer> integerIntegerJdk;
private int[] ecIntKeysForMap;
private int[] kolobokeIntKeysForMap;
private Integer[] jdkIntKeysForMap;
private int jdkIndex(int key)
{
return this.mask(key ^ (key >>> 16));
}
private int kolobokeIndex(int key)
{
int h = key * 0x9e3779b9;
return this.mask(h ^ h >> 16);
}
private int ecIndex(int element)
{
return this.mask(element);
}
private int ecIndexTwo(int element)
{
return this.mask(SpreadFunctions.intSpreadTwo(element));
}
private int mask(int spread)
{
return spread & ((1 << 11) - 1);
}
@Setup
public void setUp()
{
this.intIntKoloboke = HashIntIntMaps.newMutableMap(MAP_SIZE);
this.intIntEc = new IntIntHashMap(MAP_SIZE);
this.integerIntegerJdk = new HashMap<>(MAP_SIZE);
Random random = new Random(0x123456789ABCDL);
int[] randomNumbersForMap = this.getRandomKeys(random).toArray();
int number = 23;
int lower = Integer.MIN_VALUE;
int upper = Integer.MAX_VALUE;
this.kolobokeIntKeysForMap = this.fullyRandom
? randomNumbersForMap
: this.getKolobokeArray(number, lower, upper, random);
this.ecIntKeysForMap = this.fullyRandom
? randomNumbersForMap
: this.getECArray(number, lower, upper, random);
this.jdkIntKeysForMap = this.fullyRandom
? IntIntMapSmallStressTest.boxIntArray(randomNumbersForMap)
: this.getJDKArray(lower, upper, random);
for (int i = 0; i < KEY_COUNT; i++)
{
this.intIntKoloboke.put(this.kolobokeIntKeysForMap[i], 5);
this.intIntEc.put(this.ecIntKeysForMap[i], 5);
this.integerIntegerJdk.put(this.jdkIntKeysForMap[i], 5);
}
this.shuffle(this.ecIntKeysForMap, random);
this.shuffle(this.kolobokeIntKeysForMap, random);
this.shuffle(this.jdkIntKeysForMap, random);
}
protected int[] getECArray(int number, int lower, int upper, Random random)
{
int[] ecCollisions = this.getECSequenceCollisions(number, lower, upper).toArray();
this.shuffle(ecCollisions, random);
return ecCollisions;
}
protected MutableIntList getECSequenceCollisions(int number, int lower, int upper)
{
MutableIntList ecCollidingNumbers = new IntArrayList();
for (int i = lower; i < upper && ecCollidingNumbers.size() < KEY_COUNT; i++)
{
if (this.ecIndex(i) - this.ecIndex(number) >= 0
&& this.ecIndex(i) - this.ecIndex(number) < 10
&& (this.ecIndexTwo(i) - this.ecIndexTwo(number) >= 0)
&& (this.ecIndexTwo(i) - this.ecIndexTwo(number) < 10))
{
ecCollidingNumbers.add(i);
}
}
return ecCollidingNumbers;
}
protected Integer[] getJDKArray(int lower, int upper, Random random)
{
MutableList<Integer> collisions = this.getJDKSequenceCollisions(lower, upper);
Integer[] jdkCollision = collisions.toArray(new Integer[collisions.size()]);
this.shuffle(jdkCollision, random);
return jdkCollision;
}
protected MutableList<Integer> getJDKSequenceCollisions(int lower, int upper)
{
MutableList<Integer> jdkCollidingNumbers = FastList.newList();
int slots = 1; // slots = KEY_COUNT / (1 << 32) / (1 << MAP_SIZE) + 1;
MutableIntSet indices = new IntHashSet();
for (int i = lower; i < upper && jdkCollidingNumbers.size() < KEY_COUNT; i++)
{
int index = this.jdkIndex(i);
if (indices.size() < slots)
{
indices.add(index);
jdkCollidingNumbers.add(i);
}
else if (indices.contains(index))
{
jdkCollidingNumbers.add(i);
}
}
return jdkCollidingNumbers;
}
protected int[] getKolobokeArray(int number, int lower, int upper, Random random)
{
int[] kolobokeCollisions = this.getKolobokeSequenceCollisions(number, lower, upper).toArray();
this.shuffle(kolobokeCollisions, random);
return kolobokeCollisions;
}
protected MutableIntList getKolobokeSequenceCollisions(int number, int lower, int upper)
{
MutableIntList kolobokeCollidingNumbers = new IntArrayList();
for (int i = lower; i < upper && kolobokeCollidingNumbers.size() < KEY_COUNT; i++)
{
int index = this.kolobokeIndex(i);
if (index >= number && index <= number + 100)
{
kolobokeCollidingNumbers.add(i);
}
}
return kolobokeCollidingNumbers;
}
protected MutableIntSet getRandomKeys(Random random)
{
MutableIntSet set = new IntHashSet(KEY_COUNT);
while (set.size() < KEY_COUNT)
{
set.add(random.nextInt());
}
return set;
}
@Benchmark
public void jdkGet()
{
for (int j = 0; j < LOOP_COUNT; j++)
{
for (int i = 0; i < KEY_COUNT; i++)
{
if (this.integerIntegerJdk.get(this.jdkIntKeysForMap[i]) == null)
{
throw new AssertionError(this.jdkIntKeysForMap[i] + " not in map");
}
}
if (this.integerIntegerJdk.size() != KEY_COUNT)
{
throw new AssertionError("size is " + this.integerIntegerJdk.size());
}
}
}
@Benchmark
public void kolobokeGet()
{
for (int j = 0; j < LOOP_COUNT; j++)
{
for (int i = 0; i < KEY_COUNT; i++)
{
if (this.intIntKoloboke.get(this.kolobokeIntKeysForMap[i]) == this.intIntKoloboke.defaultValue())
{
throw new AssertionError(this.kolobokeIntKeysForMap[i] + " not in map");
}
}
if (this.intIntKoloboke.size() != KEY_COUNT)
{
throw new AssertionError("size is " + this.intIntKoloboke.size());
}
}
}
@Benchmark
public void ecGet()
{
for (int j = 0; j < LOOP_COUNT; j++)
{
for (int i = 0; i < KEY_COUNT; i++)
{
if (this.intIntEc.get(this.ecIntKeysForMap[i]) == 0)
{
throw new AssertionError(this.ecIntKeysForMap[i] + " not in map");
}
}
if (this.intIntEc.size() != KEY_COUNT)
{
throw new AssertionError("size is " + this.intIntEc.size());
}
}
}
@Benchmark
public void jdkPut()
{
for (int j = 0; j < LOOP_COUNT; j++)
{
Map<Integer, Integer> newMap = new HashMap<>(MAP_SIZE);
for (int i = 0; i < KEY_COUNT; i++)
{
newMap.put(this.jdkIntKeysForMap[i], 4);
}
if (newMap.size() != KEY_COUNT)
{
throw new AssertionError("size is " + newMap.size());
}
}
}
@Benchmark
public void kolobokePut()
{
for (int j = 0; j < LOOP_COUNT; j++)
{
IntIntMap newMap = HashIntIntMaps.newMutableMap(MAP_SIZE);
for (int i = 0; i < KEY_COUNT; i++)
{
newMap.put(this.kolobokeIntKeysForMap[i], 4);
}
if (newMap.size() != KEY_COUNT)
{
throw new AssertionError("size is " + newMap.size());
}
}
}
@Benchmark
public void ecPut()
{
for (int j = 0; j < LOOP_COUNT; j++)
{
MutableIntIntMap newMap = new IntIntHashMap(MAP_SIZE);
for (int i = 0; i < KEY_COUNT; i++)
{
newMap.put(this.ecIntKeysForMap[i], 4);
}
if (newMap.size() != KEY_COUNT)
{
throw new AssertionError("size is " + newMap.size());
}
}
}
@Benchmark
public void ecRemove()
{
for (int j = 0; j < LOOP_COUNT; j++)
{
MutableIntIntMap newMap = new IntIntHashMap(this.intIntEc);
for (int i = 0; i < KEY_COUNT; i++)
{
newMap.remove(this.ecIntKeysForMap[i]);
}
if (newMap.notEmpty())
{
throw new AssertionError("size is " + newMap.size());
}
}
}
@Benchmark
public void jdkRemove()
{
for (int j = 0; j < LOOP_COUNT; j++)
{
Map<Integer, Integer> newMap = new HashMap<>(this.integerIntegerJdk);
for (int i = 0; i < KEY_COUNT; i++)
{
newMap.remove(this.jdkIntKeysForMap[i]);
}
if (!newMap.isEmpty())
{
throw new AssertionError("size is " + newMap.size());
}
}
}
@Benchmark
public void kolobokeRemove()
{
for (int j = 0; j < LOOP_COUNT; j++)
{
IntIntMap newMap = HashIntIntMaps.newMutableMap(this.intIntKoloboke);
for (int i = 0; i < KEY_COUNT; i++)
{
newMap.remove(this.kolobokeIntKeysForMap[i]);
}
if (!newMap.isEmpty())
{
throw new AssertionError("size is " + newMap.size());
}
}
}
public void shuffle(int[] intArray, Random rnd)
{
for (int i = intArray.length; i > 1; i--)
{
IntIntMapSmallStressTest.swap(intArray, i - 1, rnd.nextInt(i));
}
}
public void shuffle(Integer[] integerArray, Random rnd)
{
for (int i = integerArray.length; i > 1; i--)
{
IntIntMapSmallStressTest.swap(integerArray, i - 1, rnd.nextInt(i));
}
}
private static void swap(int[] arr, int i, int j)
{
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
private static void swap(Integer[] arr, int i, int j)
{
Integer tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
private static Integer[] boxIntArray(int[] arr)
{
MutableList<Integer> list = new IntArrayList(arr).collect((IntToObjectFunction<Integer>) Integer::valueOf);
return list.toArray(new Integer[arr.length]);
}
}