packed-cache-inl.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
  2. // Copyright (c) 2007, Google Inc.
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // ---
  31. // Author: Geoff Pike
  32. //
  33. // This file provides a minimal cache that can hold a <key, value> pair
  34. // with little if any wasted space. The types of the key and value
  35. // must be unsigned integral types or at least have unsigned semantics
  36. // for >>, casting, and similar operations.
  37. //
  38. // Synchronization is not provided. However, the cache is implemented
  39. // as an array of cache entries whose type is chosen at compile time.
  40. // If a[i] is atomic on your hardware for the chosen array type then
  41. // raciness will not necessarily lead to bugginess. The cache entries
  42. // must be large enough to hold a partial key and a value packed
  43. // together. The partial keys are bit strings of length
  44. // kKeybits - kHashbits, and the values are bit strings of length kValuebits.
  45. //
  46. // In an effort to use minimal space, every cache entry represents
  47. // some <key, value> pair; the class provides no way to mark a cache
  48. // entry as empty or uninitialized. In practice, you may want to have
  49. // reserved keys or values to get around this limitation. For example, in
  50. // tcmalloc's PageID-to-sizeclass cache, a value of 0 is used as
  51. // "unknown sizeclass."
  52. //
  53. // Usage Considerations
  54. // --------------------
  55. //
  56. // kHashbits controls the size of the cache. The best value for
  57. // kHashbits will of course depend on the application. Perhaps try
  58. // tuning the value of kHashbits by measuring different values on your
  59. // favorite benchmark. Also remember not to be a pig; other
  60. // programs that need resources may suffer if you are.
  61. //
  62. // The main uses for this class will be when performance is
  63. // critical and there's a convenient type to hold the cache's
  64. // entries. As described above, the number of bits required
  65. // for a cache entry is (kKeybits - kHashbits) + kValuebits. Suppose
  66. // kKeybits + kValuebits is 43. Then it probably makes sense to
  67. // chose kHashbits >= 11 so that cache entries fit in a uint32.
  68. //
  69. // On the other hand, suppose kKeybits = kValuebits = 64. Then
  70. // using this class may be less worthwhile. You'll probably
  71. // be using 128 bits for each entry anyway, so maybe just pick
  72. // a hash function, H, and use an array indexed by H(key):
  73. // void Put(K key, V value) { a_[H(key)] = pair<K, V>(key, value); }
  74. // V GetOrDefault(K key, V default) { const pair<K, V> &p = a_[H(key)]; ... }
  75. // etc.
  76. //
  77. // Further Details
  78. // ---------------
  79. //
  80. // For caches used only by one thread, the following is true:
  81. // 1. For a cache c,
  82. // (c.Put(key, value), c.GetOrDefault(key, 0)) == value
  83. // and
  84. // (c.Put(key, value), <...>, c.GetOrDefault(key, 0)) == value
  85. // if the elided code contains no c.Put calls.
  86. //
  87. // 2. Has(key) will return false if no <key, value> pair with that key
  88. // has ever been Put. However, a newly initialized cache will have
  89. // some <key, value> pairs already present. When you create a new
  90. // cache, you must specify an "initial value." The initialization
  91. // procedure is equivalent to Clear(initial_value), which is
  92. // equivalent to Put(k, initial_value) for all keys k from 0 to
  93. // 2^kHashbits - 1.
  94. //
  95. // 3. If key and key' differ then the only way Put(key, value) may
  96. // cause Has(key') to change is that Has(key') may change from true to
  97. // false. Furthermore, a Put() call that doesn't change Has(key')
  98. // doesn't change GetOrDefault(key', ...) either.
  99. //
  100. // Implementation details:
  101. //
  102. // This is a direct-mapped cache with 2^kHashbits entries; the hash
  103. // function simply takes the low bits of the key. We store whole keys
  104. // if a whole key plus a whole value fits in an entry. Otherwise, an
  105. // entry is the high bits of a key and a value, packed together.
  106. // E.g., a 20 bit key and a 7 bit value only require a uint16 for each
  107. // entry if kHashbits >= 11.
  108. //
  109. // Alternatives to this scheme will be added as needed.
  110. #ifndef TCMALLOC_PACKED_CACHE_INL_H_
  111. #define TCMALLOC_PACKED_CACHE_INL_H_
  112. #include "config.h"
  113. #include <stddef.h> // for size_t
  114. #ifdef HAVE_STDINT_H
  115. #include <stdint.h> // for uintptr_t
  116. #endif
  117. #include "base/basictypes.h"
  118. #include "internal_logging.h"
  119. // A safe way of doing "(1 << n) - 1" -- without worrying about overflow
  120. // Note this will all be resolved to a constant expression at compile-time
  121. #define N_ONES_(IntType, N) \
  122. ( (N) == 0 ? 0 : ((static_cast<IntType>(1) << ((N)-1))-1 + \
  123. (static_cast<IntType>(1) << ((N)-1))) )
  124. // The types K and V provide upper bounds on the number of valid keys
  125. // and values, but we explicitly require the keys to be less than
  126. // 2^kKeybits and the values to be less than 2^kValuebits. The size of
  127. // the table is controlled by kHashbits, and the type of each entry in
  128. // the cache is T. See also the big comment at the top of the file.
  129. template <int kKeybits, typename T>
  130. class PackedCache {
  131. public:
  132. typedef uintptr_t K;
  133. typedef size_t V;
  134. #ifdef TCMALLOC_SMALL_BUT_SLOW
  135. // Decrease the size map cache if running in the small memory mode.
  136. static const int kHashbits = 12;
  137. #else
  138. static const int kHashbits = 16;
  139. #endif
  140. static const int kValuebits = 7;
  141. static const bool kUseWholeKeys = kKeybits + kValuebits <= 8 * sizeof(T);
  142. explicit PackedCache(V initial_value) {
  143. COMPILE_ASSERT(kKeybits <= sizeof(K) * 8, key_size);
  144. COMPILE_ASSERT(kValuebits <= sizeof(V) * 8, value_size);
  145. COMPILE_ASSERT(kHashbits <= kKeybits, hash_function);
  146. COMPILE_ASSERT(kKeybits - kHashbits + kValuebits <= kTbits,
  147. entry_size_must_be_big_enough);
  148. Clear(initial_value);
  149. }
  150. void Put(K key, V value) {
  151. ASSERT(key == (key & kKeyMask));
  152. ASSERT(value == (value & kValueMask));
  153. array_[Hash(key)] = KeyToUpper(key) | value;
  154. }
  155. bool Has(K key) const {
  156. ASSERT(key == (key & kKeyMask));
  157. return KeyMatch(array_[Hash(key)], key);
  158. }
  159. V GetOrDefault(K key, V default_value) const {
  160. // As with other code in this class, we touch array_ as few times
  161. // as we can. Assuming entries are read atomically (e.g., their
  162. // type is uintptr_t on most hardware) then certain races are
  163. // harmless.
  164. ASSERT(key == (key & kKeyMask));
  165. T entry = array_[Hash(key)];
  166. return KeyMatch(entry, key) ? EntryToValue(entry) : default_value;
  167. }
  168. void Clear(V value) {
  169. ASSERT(value == (value & kValueMask));
  170. for (int i = 0; i < 1 << kHashbits; i++) {
  171. ASSERT(kUseWholeKeys || KeyToUpper(i) == 0);
  172. array_[i] = kUseWholeKeys ? (value | KeyToUpper(i)) : value;
  173. }
  174. }
  175. private:
  176. // We are going to pack a value and the upper part of a key (or a
  177. // whole key) into an entry of type T. The UPPER type is for the
  178. // upper part of a key, after the key has been masked and shifted
  179. // for inclusion in an entry.
  180. typedef T UPPER;
  181. static V EntryToValue(T t) { return t & kValueMask; }
  182. // If we have space for a whole key, we just shift it left.
  183. // Otherwise kHashbits determines where in a K to find the upper
  184. // part of the key, and kValuebits determines where in the entry to
  185. // put it.
  186. static UPPER KeyToUpper(K k) {
  187. if (kUseWholeKeys) {
  188. return static_cast<T>(k) << kValuebits;
  189. } else {
  190. const int shift = kHashbits - kValuebits;
  191. // Assume kHashbits >= kValuebits. It'd be easy to lift this assumption.
  192. return static_cast<T>(k >> shift) & kUpperMask;
  193. }
  194. }
  195. static size_t Hash(K key) {
  196. return static_cast<size_t>(key) & N_ONES_(size_t, kHashbits);
  197. }
  198. // Does the entry match the relevant part of the given key?
  199. static bool KeyMatch(T entry, K key) {
  200. return kUseWholeKeys ?
  201. (entry >> kValuebits == key) :
  202. ((KeyToUpper(key) ^ entry) & kUpperMask) == 0;
  203. }
  204. static const int kTbits = 8 * sizeof(T);
  205. static const int kUpperbits = kUseWholeKeys ? kKeybits : kKeybits - kHashbits;
  206. // For masking a K.
  207. static const K kKeyMask = N_ONES_(K, kKeybits);
  208. // For masking a T.
  209. static const T kUpperMask = N_ONES_(T, kUpperbits) << kValuebits;
  210. // For masking a V or a T.
  211. static const V kValueMask = N_ONES_(V, kValuebits);
  212. // array_ is the cache. Its elements are volatile because any
  213. // thread can write any array element at any time.
  214. volatile T array_[1 << kHashbits];
  215. };
  216. #undef N_ONES_
  217. #endif // TCMALLOC_PACKED_CACHE_INL_H_