2 * Copyright 2016 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * Higher performance (up to 10x) atomic increment using thread caching.
20 * @author Spencer Ahrens (sahrens)
27 #include <boost/noncopyable.hpp>
29 #include <folly/Likely.h>
30 #include <folly/ThreadLocal.h>
35 // Note that readFull requires holding a lock and iterating through all of the
36 // thread local objects with the same Tag, so if you have a lot of
37 // ThreadCachedInt's you should considering breaking up the Tag space even
39 template <class IntT, class Tag=IntT>
40 class ThreadCachedInt : boost::noncopyable {
44 explicit ThreadCachedInt(IntT initialVal = 0, uint32_t cacheSize = 1000)
45 : target_(initialVal), cacheSize_(cacheSize) {
48 void increment(IntT inc) {
49 auto cache = cache_.get();
50 if (UNLIKELY(cache == nullptr || cache->parent_ == nullptr)) {
51 cache = new IntCache(*this);
54 cache->increment(inc);
57 // Quickly grabs the current value which may not include some cached
59 IntT readFast() const {
60 return target_.load(std::memory_order_relaxed);
63 // Reads the current value plus all the cached increments. Requires grabbing
64 // a lock, so this is significantly slower than readFast().
65 IntT readFull() const {
66 IntT ret = readFast();
67 for (const auto& cache : cache_.accessAllThreads()) {
68 if (!cache.reset_.load(std::memory_order_acquire)) {
69 ret += cache.val_.load(std::memory_order_relaxed);
75 // Quickly reads and resets current value (doesn't reset cached increments).
76 IntT readFastAndReset() {
77 return target_.exchange(0, std::memory_order_release);
80 // This function is designed for accumulating into another counter, where you
81 // only want to count each increment once. It can still get the count a
82 // little off, however, but it should be much better than calling readFull()
83 // and set(0) sequentially.
84 IntT readFullAndReset() {
85 IntT ret = readFastAndReset();
86 for (auto& cache : cache_.accessAllThreads()) {
87 if (!cache.reset_.load(std::memory_order_acquire)) {
88 ret += cache.val_.load(std::memory_order_relaxed);
89 cache.reset_.store(true, std::memory_order_release);
95 void setCacheSize(uint32_t newSize) {
96 cacheSize_.store(newSize, std::memory_order_release);
99 uint32_t getCacheSize() const {
100 return cacheSize_.load();
103 ThreadCachedInt& operator+=(IntT inc) { increment(inc); return *this; }
104 ThreadCachedInt& operator-=(IntT inc) { increment(-inc); return *this; }
105 // pre-increment (we don't support post-increment)
106 ThreadCachedInt& operator++() { increment(1); return *this; }
107 ThreadCachedInt& operator--() { increment(-1); return *this; }
109 // Thread-safe set function.
110 // This is a best effort implementation. In some edge cases, there could be
111 // data loss (missing counts)
112 void set(IntT newVal) {
113 for (auto& cache : cache_.accessAllThreads()) {
114 cache.reset_.store(true, std::memory_order_release);
116 target_.store(newVal, std::memory_order_release);
119 // This is a little tricky - it's possible that our IntCaches are still alive
120 // in another thread and will get destroyed after this destructor runs, so we
121 // need to make sure we signal that this parent is dead.
123 for (auto& cache : cache_.accessAllThreads()) {
124 cache.parent_ = nullptr;
129 std::atomic<IntT> target_;
130 std::atomic<uint32_t> cacheSize_;
131 ThreadLocalPtr<IntCache,Tag> cache_; // Must be last for dtor ordering
133 // This should only ever be modified by one thread
135 ThreadCachedInt* parent_;
136 mutable std::atomic<IntT> val_;
137 mutable uint32_t numUpdates_;
138 std::atomic<bool> reset_;
140 explicit IntCache(ThreadCachedInt& parent)
141 : parent_(&parent), val_(0), numUpdates_(0), reset_(false) {}
143 void increment(IntT inc) {
144 if (LIKELY(!reset_.load(std::memory_order_acquire))) {
145 // This thread is the only writer to val_, so it's fine do do
146 // a relaxed load and do the addition non-atomically.
148 val_.load(std::memory_order_relaxed) + inc,
149 std::memory_order_release
152 val_.store(inc, std::memory_order_relaxed);
153 reset_.store(false, std::memory_order_release);
156 if (UNLIKELY(numUpdates_ >
157 parent_->cacheSize_.load(std::memory_order_acquire))) {
163 parent_->target_.fetch_add(val_, std::memory_order_release);
164 val_.store(0, std::memory_order_release);