/
ConcurrentHashMultisetBenchmark.java
550 lines (492 loc) · 16.6 KB
/
ConcurrentHashMultisetBenchmark.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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
/*
* Copyright (C) 2011 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.common.collect;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.CollectPreconditions.checkNonnegative;
import com.google.caliper.BeforeExperiment;
import com.google.caliper.Benchmark;
import com.google.caliper.Param;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.primitives.Ints;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import org.checkerframework.checker.nullness.qual.Nullable;
/**
* Benchmarks for {@link ConcurrentHashMultiset}.
*
* @author mike nonemacher
*/
public class ConcurrentHashMultisetBenchmark {
@Param({"1", "2", "4", "8"})
int threads;
@Param({"3", "30", "300"})
int size;
@Param MultisetSupplier implSupplier;
private Multiset<Integer> multiset;
private ImmutableList<Integer> keys;
private ExecutorService threadPool;
@BeforeExperiment
void setUp() throws Exception {
multiset = implSupplier.get();
ImmutableList.Builder<Integer> builder = ImmutableList.builder();
for (int i = 0; i < size; i++) {
builder.add(i);
}
keys = builder.build();
threadPool =
Executors.newFixedThreadPool(threads, new ThreadFactoryBuilder().setDaemon(true).build());
}
@Benchmark
long add(final int reps) throws ExecutionException, InterruptedException {
return doMultithreadedLoop(
new Callable<Long>() {
@Override
public Long call() {
return runAddSingleThread(reps);
}
});
}
@Benchmark
long addRemove(final int reps) throws ExecutionException, InterruptedException {
return doMultithreadedLoop(
new Callable<Long>() {
@Override
public Long call() {
return runAddRemoveSingleThread(reps);
}
});
}
private long doMultithreadedLoop(Callable<Long> task)
throws InterruptedException, ExecutionException {
List<Future<Long>> futures = Lists.newArrayListWithCapacity(threads);
for (int i = 0; i < threads; i++) {
futures.add(threadPool.submit(task));
}
long total = 0;
for (Future<Long> future : futures) {
total += future.get();
}
return total;
}
private long runAddSingleThread(int reps) {
Random random = new Random();
int nKeys = keys.size();
long blah = 0;
for (int i = 0; i < reps; i++) {
Integer key = keys.get(random.nextInt(nKeys));
int delta = random.nextInt(5);
blah += delta;
multiset.add(key, delta);
}
return blah;
}
private long runAddRemoveSingleThread(int reps) {
Random random = new Random();
int nKeys = keys.size();
long blah = 0;
for (int i = 0; i < reps; i++) {
Integer key = keys.get(random.nextInt(nKeys));
// This range is [-5, 4] - slight negative bias so we often hit zero, which brings the
// auto-removal of zeroes into play.
int delta = random.nextInt(10) - 5;
blah += delta;
if (delta >= 0) {
multiset.add(key, delta);
} else {
multiset.remove(key, -delta);
}
}
return blah;
}
private enum MultisetSupplier {
CONCURRENT_HASH_MULTISET() {
@Override
Multiset<Integer> get() {
return ConcurrentHashMultiset.create();
}
},
BOXED_ATOMIC_REPLACE() {
@Override
Multiset<Integer> get() {
return OldConcurrentHashMultiset.create();
}
},
SYNCHRONIZED_MULTISET() {
@Override
Multiset<Integer> get() {
return Synchronized.multiset(HashMultiset.<Integer>create(), null);
}
},
;
abstract Multiset<Integer> get();
}
/**
* Duplication of the old version of ConcurrentHashMultiset (with some unused stuff removed, like
* serialization code) which used a map with boxed integers for the values.
*/
private static final class OldConcurrentHashMultiset<E> extends AbstractMultiset<E> {
/** The number of occurrences of each element. */
private final transient ConcurrentMap<E, Integer> countMap;
/**
* Creates a new, empty {@code OldConcurrentHashMultiset} using the default initial capacity,
* load factor, and concurrency settings.
*/
public static <E> OldConcurrentHashMultiset<E> create() {
return new OldConcurrentHashMultiset<E>(new ConcurrentHashMap<E, Integer>());
}
@VisibleForTesting
OldConcurrentHashMultiset(ConcurrentMap<E, Integer> countMap) {
checkArgument(countMap.isEmpty());
this.countMap = countMap;
}
// Query Operations
/**
* Returns the number of occurrences of {@code element} in this multiset.
*
* @param element the element to look for
* @return the nonnegative number of occurrences of the element
*/
@Override
public int count(@Nullable Object element) {
try {
return unbox(countMap.get(element));
} catch (NullPointerException | ClassCastException e) {
return 0;
}
}
/**
* {@inheritDoc}
*
* <p>If the data in the multiset is modified by any other threads during this method, it is
* undefined which (if any) of these modifications will be reflected in the result.
*/
@Override
public int size() {
long sum = 0L;
for (Integer value : countMap.values()) {
sum += value;
}
return Ints.saturatedCast(sum);
}
/*
* Note: the superclass toArray() methods assume that size() gives a correct
* answer, which ours does not.
*/
@Override
public Object[] toArray() {
return snapshot().toArray();
}
@Override
public <T> T[] toArray(T[] array) {
return snapshot().toArray(array);
}
/*
* We'd love to use 'new ArrayList(this)' or 'list.addAll(this)', but
* either of these would recurse back to us again!
*/
private List<E> snapshot() {
List<E> list = Lists.newArrayListWithExpectedSize(size());
for (Multiset.Entry<E> entry : entrySet()) {
E element = entry.getElement();
for (int i = entry.getCount(); i > 0; i--) {
list.add(element);
}
}
return list;
}
// Modification Operations
/**
* Adds a number of occurrences of the specified element to this multiset.
*
* @param element the element to add
* @param occurrences the number of occurrences to add
* @return the previous count of the element before the operation; possibly zero
* @throws IllegalArgumentException if {@code occurrences} is negative, or if the resulting
* amount would exceed {@link Integer#MAX_VALUE}
*/
@Override
public int add(E element, int occurrences) {
if (occurrences == 0) {
return count(element);
}
checkArgument(occurrences > 0, "Invalid occurrences: %s", occurrences);
while (true) {
int current = count(element);
if (current == 0) {
if (countMap.putIfAbsent(element, occurrences) == null) {
return 0;
}
} else {
checkArgument(
occurrences <= Integer.MAX_VALUE - current,
"Overflow adding %s occurrences to a count of %s",
occurrences,
current);
int next = current + occurrences;
if (countMap.replace(element, current, next)) {
return current;
}
}
// If we're still here, there was a race, so just try again.
}
}
/**
* Removes a number of occurrences of the specified element from this multiset. If the multiset
* contains fewer than this number of occurrences to begin with, all occurrences will be
* removed.
*
* @param element the element whose occurrences should be removed
* @param occurrences the number of occurrences of the element to remove
* @return the count of the element before the operation; possibly zero
* @throws IllegalArgumentException if {@code occurrences} is negative
*/
@Override
public int remove(@Nullable Object element, int occurrences) {
if (occurrences == 0) {
return count(element);
}
checkArgument(occurrences > 0, "Invalid occurrences: %s", occurrences);
while (true) {
int current = count(element);
if (current == 0) {
return 0;
}
if (occurrences >= current) {
if (countMap.remove(element, current)) {
return current;
}
} else {
// We know it's an "E" because it already exists in the map.
@SuppressWarnings("unchecked")
E casted = (E) element;
if (countMap.replace(casted, current, current - occurrences)) {
return current;
}
}
// If we're still here, there was a race, so just try again.
}
}
/**
* Removes <b>all</b> occurrences of the specified element from this multiset. This method
* complements {@link Multiset#remove(Object)}, which removes only one occurrence at a time.
*
* @param element the element whose occurrences should all be removed
* @return the number of occurrences successfully removed, possibly zero
*/
private int removeAllOccurrences(@Nullable Object element) {
try {
return unbox(countMap.remove(element));
} catch (NullPointerException | ClassCastException e) {
return 0;
}
}
/**
* Removes exactly the specified number of occurrences of {@code element}, or makes no change if
* this is not possible.
*
* <p>This method, in contrast to {@link #remove(Object, int)}, has no effect when the element
* count is smaller than {@code occurrences}.
*
* @param element the element to remove
* @param occurrences the number of occurrences of {@code element} to remove
* @return {@code true} if the removal was possible (including if {@code occurrences} is zero)
*/
public boolean removeExactly(@Nullable Object element, int occurrences) {
if (occurrences == 0) {
return true;
}
checkArgument(occurrences > 0, "Invalid occurrences: %s", occurrences);
while (true) {
int current = count(element);
if (occurrences > current) {
return false;
}
if (occurrences == current) {
if (countMap.remove(element, occurrences)) {
return true;
}
} else {
@SuppressWarnings("unchecked") // it's in the map, must be an "E"
E casted = (E) element;
if (countMap.replace(casted, current, current - occurrences)) {
return true;
}
}
// If we're still here, there was a race, so just try again.
}
}
/**
* Adds or removes occurrences of {@code element} such that the {@link #count} of the element
* becomes {@code count}.
*
* @return the count of {@code element} in the multiset before this call
* @throws IllegalArgumentException if {@code count} is negative
*/
@Override
public int setCount(E element, int count) {
checkNonnegative(count, "count");
return (count == 0) ? removeAllOccurrences(element) : unbox(countMap.put(element, count));
}
/**
* Sets the number of occurrences of {@code element} to {@code newCount}, but only if the count
* is currently {@code oldCount}. If {@code element} does not appear in the multiset exactly
* {@code oldCount} times, no changes will be made.
*
* @return {@code true} if the change was successful. This usually indicates that the multiset
* has been modified, but not always: in the case that {@code oldCount == newCount}, the
* method will return {@code true} if the condition was met.
* @throws IllegalArgumentException if {@code oldCount} or {@code newCount} is negative
*/
@Override
public boolean setCount(E element, int oldCount, int newCount) {
checkNonnegative(oldCount, "oldCount");
checkNonnegative(newCount, "newCount");
if (newCount == 0) {
if (oldCount == 0) {
// No change to make, but must return true if the element is not present
return !countMap.containsKey(element);
} else {
return countMap.remove(element, oldCount);
}
}
if (oldCount == 0) {
return countMap.putIfAbsent(element, newCount) == null;
}
return countMap.replace(element, oldCount, newCount);
}
// Views
@Override
Set<E> createElementSet() {
final Set<E> delegate = countMap.keySet();
return new ForwardingSet<E>() {
@Override
protected Set<E> delegate() {
return delegate;
}
@Override
public boolean remove(Object object) {
try {
return delegate.remove(object);
} catch (NullPointerException | ClassCastException e) {
return false;
}
}
};
}
@Override
Iterator<E> elementIterator() {
throw new AssertionError("should never be called");
}
private transient EntrySet entrySet;
@Override
public Set<Multiset.Entry<E>> entrySet() {
EntrySet result = entrySet;
if (result == null) {
entrySet = result = new EntrySet();
}
return result;
}
@Override
int distinctElements() {
return countMap.size();
}
@Override
public boolean isEmpty() {
return countMap.isEmpty();
}
@Override
Iterator<Entry<E>> entryIterator() {
final Iterator<Map.Entry<E, Integer>> backingIterator = countMap.entrySet().iterator();
return new Iterator<Entry<E>>() {
@Override
public boolean hasNext() {
return backingIterator.hasNext();
}
@Override
public Multiset.Entry<E> next() {
Map.Entry<E, Integer> backingEntry = backingIterator.next();
return Multisets.immutableEntry(backingEntry.getKey(), backingEntry.getValue());
}
@Override
public void remove() {
backingIterator.remove();
}
};
}
@Override
public Iterator<E> iterator() {
return Multisets.iteratorImpl(this);
}
@Override
public void clear() {
countMap.clear();
}
private class EntrySet extends AbstractMultiset<E>.EntrySet {
@Override
Multiset<E> multiset() {
return OldConcurrentHashMultiset.this;
}
/*
* Note: the superclass toArray() methods assume that size() gives a correct
* answer, which ours does not.
*/
@Override
public Object[] toArray() {
return snapshot().toArray();
}
@Override
public <T> T[] toArray(T[] array) {
return snapshot().toArray(array);
}
private List<Multiset.Entry<E>> snapshot() {
List<Multiset.Entry<E>> list = Lists.newArrayListWithExpectedSize(size());
// not Iterables.addAll(list, this), because that'll forward back here
Iterators.addAll(list, iterator());
return list;
}
@Override
public boolean remove(Object object) {
if (object instanceof Multiset.Entry) {
Multiset.Entry<?> entry = (Multiset.Entry<?>) object;
Object element = entry.getElement();
int entryCount = entry.getCount();
return countMap.remove(element, entryCount);
}
return false;
}
/** The hash code is the same as countMap's, though the objects aren't equal. */
@Override
public int hashCode() {
return countMap.hashCode();
}
}
/** We use a special form of unboxing that treats null as zero. */
private static int unbox(@Nullable Integer i) {
return (i == null) ? 0 : i;
}
}
}