nharmata | d28af66 | 2018-03-27 14:52:44 -0700 | [diff] [blame] | 1 | // Copyright 2018 The Bazel Authors. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | package com.google.devtools.build.lib.concurrent; |
| 15 | |
| 16 | import com.google.common.collect.ImmutableMap; |
nharmata | d28af66 | 2018-03-27 14:52:44 -0700 | [diff] [blame] | 17 | import com.google.common.collect.Maps; |
| 18 | import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe; |
Googler | 3687f2a | 2018-05-02 13:27:28 -0700 | [diff] [blame] | 19 | import java.util.concurrent.ConcurrentHashMap; |
nharmata | d28af66 | 2018-03-27 14:52:44 -0700 | [diff] [blame] | 20 | import java.util.concurrent.ConcurrentMap; |
| 21 | import java.util.concurrent.atomic.AtomicLong; |
| 22 | |
| 23 | /** |
| 24 | * A map of atomic long counters. A key whose counter's value is currently zero is _not_ |
| 25 | * automatically removed from the map; use {@link #clear} to clear the entire map. |
| 26 | * |
| 27 | * <p>This is very similar to Guava's AtomicLongMap, but optimized for the case where keys are hot, |
| 28 | * e.g. a high number of concurrent calls to {@code map.incrementAndGet(k)} and/or |
| 29 | * {@code map.decrementAndGet(k)}, for the same key {@code k)}. Guava's AtomicLongMap uses |
| 30 | * ConcurrentHashMap#compute, whose implementation unfortunately has internal synchronization even |
| 31 | * when there's already an internal entry for the key in question. |
| 32 | */ |
| 33 | @ThreadSafe |
| 34 | public class FastHotKeyAtomicLongMap<T> { |
| 35 | private final ConcurrentMap<T, AtomicLong> map; |
| 36 | |
| 37 | public static <T> FastHotKeyAtomicLongMap<T> create() { |
Googler | 3687f2a | 2018-05-02 13:27:28 -0700 | [diff] [blame] | 38 | return new FastHotKeyAtomicLongMap<>(); |
nharmata | d28af66 | 2018-03-27 14:52:44 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Googler | 3687f2a | 2018-05-02 13:27:28 -0700 | [diff] [blame] | 41 | // TODO(kak): Delete this in favor of create() |
| 42 | public static <T> FastHotKeyAtomicLongMap<T> create(int concurrencyLevel /* ignored */) { |
| 43 | return new FastHotKeyAtomicLongMap<>(); |
nharmata | d28af66 | 2018-03-27 14:52:44 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Googler | 3687f2a | 2018-05-02 13:27:28 -0700 | [diff] [blame] | 46 | private FastHotKeyAtomicLongMap() { |
| 47 | this.map = new ConcurrentHashMap<>(); |
nharmata | d28af66 | 2018-03-27 14:52:44 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | public long incrementAndGet(T key) { |
nharmata | 81cc413 | 2018-03-28 15:50:02 -0700 | [diff] [blame] | 51 | return getCounter(key).incrementAndGet(); |
nharmata | d28af66 | 2018-03-27 14:52:44 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | public long decrementAndGet(T key) { |
nharmata | 81cc413 | 2018-03-28 15:50:02 -0700 | [diff] [blame] | 55 | return getCounter(key).decrementAndGet(); |
nharmata | d28af66 | 2018-03-27 14:52:44 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | public ImmutableMap<T, Long> asImmutableMap() { |
| 59 | return ImmutableMap.copyOf(Maps.transformValues(map, AtomicLong::get)); |
| 60 | } |
| 61 | |
nharmata | 81cc413 | 2018-03-28 15:50:02 -0700 | [diff] [blame] | 62 | /** |
| 63 | * Returns the {@link AtomicLong} for the given {@code element}. Mutations to this |
| 64 | * {@link AtomicLong} will be reflected in the {@link FastHotKeyAtomicLongMap}: for example, |
| 65 | * {@code map.getCounter(e).incrementAndGet()} has exactly the same side effects as |
| 66 | * {@code map.incrementAndGet(e)}. |
| 67 | * |
| 68 | * <p>Consider using this method when you have a super-hot key that you know about a priori. |
| 69 | * Prefer {@link #incrementAndGet} and {@link #decrementAndGet} otherwise. |
| 70 | */ |
| 71 | public AtomicLong getCounter(T element) { |
nharmata | d28af66 | 2018-03-27 14:52:44 -0700 | [diff] [blame] | 72 | // Optimize for the case where 'element' is already in our map. See the class javadoc. |
| 73 | AtomicLong counter = map.get(element); |
| 74 | return counter != null ? counter : map.computeIfAbsent(element, s -> new AtomicLong(0)); |
| 75 | } |
nharmata | 81cc413 | 2018-03-28 15:50:02 -0700 | [diff] [blame] | 76 | |
| 77 | /** |
| 78 | * Clears the {@link FastHotKeyAtomicLongMap}. |
| 79 | * |
| 80 | * <p>Any {@link AtomicLong} instances previously returned by a call to {@link #getCounter} are |
| 81 | * now meaningless: mutations to them will not be reflected in the |
| 82 | * {@link FastHotKeyAtomicLongMap}. |
| 83 | */ |
| 84 | public void clear() { |
| 85 | map.clear(); |
| 86 | } |
nharmata | d28af66 | 2018-03-27 14:52:44 -0700 | [diff] [blame] | 87 | } |