Fix #includes
[folly.git] / folly / AtomicHashArray.h
1 /*
2  * Copyright 2014 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /**
18  *  AtomicHashArray is the building block for AtomicHashMap.  It provides the
19  *  core lock-free functionality, but is limitted by the fact that it cannot
20  *  grow past it's initialization size and is a little more awkward (no public
21  *  constructor, for example).  If you're confident that you won't run out of
22  *  space, don't mind the awkardness, and really need bare-metal performance,
23  *  feel free to use AHA directly.
24  *
25  *  Check out AtomicHashMap.h for more thorough documentation on perf and
26  *  general pros and cons relative to other hash maps.
27  *
28  *  @author Spencer Ahrens <sahrens@fb.com>
29  *  @author Jordan DeLong <delong.j@fb.com>
30  */
31
32 #ifndef FOLLY_ATOMICHASHARRAY_H_
33 #define FOLLY_ATOMICHASHARRAY_H_
34
35 #include <atomic>
36
37 #include <boost/iterator/iterator_facade.hpp>
38 #include <boost/noncopyable.hpp>
39
40 #include <folly/Hash.h>
41 #include <folly/ThreadCachedInt.h>
42
43 namespace folly {
44
45 template <class KeyT, class ValueT,
46           class HashFcn = std::hash<KeyT>,
47           class EqualFcn = std::equal_to<KeyT>,
48           class Allocator = std::allocator<char>>
49 class AtomicHashMap;
50
51 template <class KeyT, class ValueT,
52           class HashFcn = std::hash<KeyT>,
53           class EqualFcn = std::equal_to<KeyT>,
54           class Allocator = std::allocator<char>>
55 class AtomicHashArray : boost::noncopyable {
56   static_assert((std::is_convertible<KeyT,int32_t>::value ||
57                  std::is_convertible<KeyT,int64_t>::value ||
58                  std::is_convertible<KeyT,const void*>::value),
59              "You are trying to use AtomicHashArray with disallowed key "
60              "types.  You must use atomically compare-and-swappable integer "
61              "keys, or a different container class.");
62  public:
63   typedef KeyT                key_type;
64   typedef ValueT              mapped_type;
65   typedef std::pair<const KeyT, ValueT> value_type;
66   typedef std::size_t         size_type;
67   typedef std::ptrdiff_t      difference_type;
68   typedef value_type&         reference;
69   typedef const value_type&   const_reference;
70   typedef value_type*         pointer;
71   typedef const value_type*   const_pointer;
72
73   const size_t  capacity_;
74   const size_t  maxEntries_;
75   const KeyT    kEmptyKey_;
76   const KeyT    kLockedKey_;
77   const KeyT    kErasedKey_;
78
79   template<class ContT, class IterVal>
80   struct aha_iterator;
81
82   typedef aha_iterator<const AtomicHashArray,const value_type> const_iterator;
83   typedef aha_iterator<AtomicHashArray,value_type> iterator;
84
85   // You really shouldn't need this if you use the SmartPtr provided by create,
86   // but if you really want to do something crazy like stick the released
87   // pointer into a DescriminatedPtr or something, you'll need this to clean up
88   // after yourself.
89   static void destroy(AtomicHashArray*);
90
91  private:
92   const size_t  kAnchorMask_;
93
94   struct Deleter {
95     void operator()(AtomicHashArray* ptr) {
96       AtomicHashArray::destroy(ptr);
97     }
98   };
99
100  public:
101   typedef std::unique_ptr<AtomicHashArray, Deleter> SmartPtr;
102
103   /*
104    * create --
105    *
106    *   Creates AtomicHashArray objects.  Use instead of constructor/destructor.
107    *
108    *   We do things this way in order to avoid the perf penalty of a second
109    *   pointer indirection when composing these into AtomicHashMap, which needs
110    *   to store an array of pointers so that it can perform atomic operations on
111    *   them when growing.
112    *
113    *   Instead of a mess of arguments, we take a max size and a Config struct to
114    *   simulate named ctor parameters.  The Config struct has sensible defaults
115    *   for everything, but is overloaded - if you specify a positive capacity,
116    *   that will be used directly instead of computing it based on
117    *   maxLoadFactor.
118    *
119    *   Create returns an AHA::SmartPtr which is a unique_ptr with a custom
120    *   deleter to make sure everything is cleaned up properly.
121    */
122   struct Config {
123     KeyT   emptyKey;
124     KeyT   lockedKey;
125     KeyT   erasedKey;
126     double maxLoadFactor;
127     double growthFactor;
128     int    entryCountThreadCacheSize;
129     size_t capacity; // if positive, overrides maxLoadFactor
130
131     constexpr Config() : emptyKey((KeyT)-1),
132                          lockedKey((KeyT)-2),
133                          erasedKey((KeyT)-3),
134                          maxLoadFactor(0.8),
135                          growthFactor(-1),
136                          entryCountThreadCacheSize(1000),
137                          capacity(0) {}
138   };
139
140   static const Config defaultConfig;
141   static SmartPtr create(size_t maxSize, const Config& = defaultConfig);
142
143   iterator find(KeyT k) {
144     return iterator(this, findInternal(k).idx);
145   }
146   const_iterator find(KeyT k) const {
147     return const_cast<AtomicHashArray*>(this)->find(k);
148   }
149
150   /*
151    * insert --
152    *
153    *   Returns a pair with iterator to the element at r.first and bool success.
154    *   Retrieve the index with ret.first.getIndex().
155    *
156    *   Fails on key collision (does not overwrite) or if map becomes
157    *   full, at which point no element is inserted, iterator is set to end(),
158    *   and success is set false.  On collisions, success is set false, but the
159    *   iterator is set to the existing entry.
160    */
161   std::pair<iterator,bool> insert(const value_type& r) {
162     SimpleRetT ret = insertInternal(r.first, r.second);
163     return std::make_pair(iterator(this, ret.idx), ret.success);
164   }
165   std::pair<iterator,bool> insert(value_type&& r) {
166     SimpleRetT ret = insertInternal(r.first, std::move(r.second));
167     return std::make_pair(iterator(this, ret.idx), ret.success);
168   }
169
170   // returns the number of elements erased - should never exceed 1
171   size_t erase(KeyT k);
172
173   // clears all keys and values in the map and resets all counters.  Not thread
174   // safe.
175   void clear();
176
177   // Exact number of elements in the map - note that readFull() acquires a
178   // mutex.  See folly/ThreadCachedInt.h for more details.
179   size_t size() const {
180     return numEntries_.readFull() -
181       numErases_.load(std::memory_order_relaxed);
182   }
183
184   bool empty() const { return size() == 0; }
185
186   iterator begin()             { return iterator(this, 0); }
187   iterator end()               { return iterator(this, capacity_); }
188   const_iterator begin() const { return const_iterator(this, 0); }
189   const_iterator end() const   { return const_iterator(this, capacity_); }
190
191   // See AtomicHashMap::findAt - access elements directly
192   // WARNING: The following 2 functions will fail silently for hashtable
193   // with capacity > 2^32
194   iterator findAt(uint32_t idx) {
195     DCHECK_LT(idx, capacity_);
196     return iterator(this, idx);
197   }
198   const_iterator findAt(uint32_t idx) const {
199     return const_cast<AtomicHashArray*>(this)->findAt(idx);
200   }
201
202   iterator makeIter(size_t idx) { return iterator(this, idx); }
203   const_iterator makeIter(size_t idx) const {
204     return const_iterator(this, idx);
205   }
206
207   // The max load factor allowed for this map
208   double maxLoadFactor() const { return ((double) maxEntries_) / capacity_; }
209
210   void setEntryCountThreadCacheSize(uint32_t newSize) {
211     numEntries_.setCacheSize(newSize);
212     numPendingEntries_.setCacheSize(newSize);
213   }
214
215   int getEntryCountThreadCacheSize() const {
216     return numEntries_.getCacheSize();
217   }
218
219   /* Private data and helper functions... */
220
221  private:
222   friend class AtomicHashMap<KeyT, ValueT, HashFcn, EqualFcn, Allocator>;
223
224   struct SimpleRetT { size_t idx; bool success;
225     SimpleRetT(size_t i, bool s) : idx(i), success(s) {}
226     SimpleRetT() {}
227   };
228
229   template <class T>
230   SimpleRetT insertInternal(KeyT key, T&& value);
231
232   SimpleRetT findInternal(const KeyT key);
233
234   static std::atomic<KeyT>* cellKeyPtr(const value_type& r) {
235     // We need some illegal casting here in order to actually store
236     // our value_type as a std::pair<const,>.  But a little bit of
237     // undefined behavior never hurt anyone ...
238     static_assert(sizeof(std::atomic<KeyT>) == sizeof(KeyT),
239                   "std::atomic is implemented in an unexpected way for AHM");
240     return
241       const_cast<std::atomic<KeyT>*>(
242         reinterpret_cast<std::atomic<KeyT> const*>(&r.first));
243   }
244
245   static KeyT relaxedLoadKey(const value_type& r) {
246     return cellKeyPtr(r)->load(std::memory_order_relaxed);
247   }
248
249   static KeyT acquireLoadKey(const value_type& r) {
250     return cellKeyPtr(r)->load(std::memory_order_acquire);
251   }
252
253   // Fun with thread local storage - atomic increment is expensive
254   // (relatively), so we accumulate in the thread cache and periodically
255   // flush to the actual variable, and walk through the unflushed counts when
256   // reading the value, so be careful of calling size() too frequently.  This
257   // increases insertion throughput several times over while keeping the count
258   // accurate.
259   ThreadCachedInt<int64_t> numEntries_;  // Successful key inserts
260   ThreadCachedInt<int64_t> numPendingEntries_; // Used by insertInternal
261   std::atomic<int64_t> isFull_; // Used by insertInternal
262   std::atomic<int64_t> numErases_;   // Successful key erases
263
264   value_type cells_[0];  // This must be the last field of this class
265
266   // Force constructor/destructor private since create/destroy should be
267   // used externally instead
268   AtomicHashArray(size_t capacity, KeyT emptyKey, KeyT lockedKey,
269                   KeyT erasedKey, double maxLoadFactor, size_t cacheSize);
270
271   ~AtomicHashArray() {}
272
273   inline void unlockCell(value_type* const cell, KeyT newKey) {
274     cellKeyPtr(*cell)->store(newKey, std::memory_order_release);
275   }
276
277   inline bool tryLockCell(value_type* const cell) {
278     KeyT expect = kEmptyKey_;
279     return cellKeyPtr(*cell)->compare_exchange_strong(expect, kLockedKey_,
280       std::memory_order_acq_rel);
281   }
282
283   inline size_t keyToAnchorIdx(const KeyT k) const {
284     const size_t hashVal = HashFcn()(k);
285     const size_t probe = hashVal & kAnchorMask_;
286     return LIKELY(probe < capacity_) ? probe : hashVal % capacity_;
287   }
288
289   inline size_t probeNext(size_t idx, size_t numProbes) {
290     //idx += numProbes; // quadratic probing
291     idx += 1; // linear probing
292     // Avoid modulus because it's slow
293     return LIKELY(idx < capacity_) ? idx : (idx - capacity_);
294   }
295 }; // AtomicHashArray
296
297 } // namespace folly
298
299 #include <folly/AtomicHashArray-inl.h>
300
301 #endif // FOLLY_ATOMICHASHARRAY_H_