Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 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 | // --- |
| 32 | // Author: Geoff Pike |
| 33 | // |
| 34 | // This file provides a minimal cache that can hold a <key, value> pair |
| 35 | // with little if any wasted space. The types of the key and value |
| 36 | // must be unsigned integral types or at least have unsigned semantics |
| 37 | // for >>, casting, and similar operations. |
| 38 | // |
| 39 | // Synchronization is not provided. However, the cache is implemented |
| 40 | // as an array of cache entries whose type is chosen at compile time. |
| 41 | // If a[i] is atomic on your hardware for the chosen array type then |
| 42 | // raciness will not necessarily lead to bugginess. The cache entries |
| 43 | // must be large enough to hold a partial key and a value packed |
| 44 | // together. The partial keys are bit strings of length |
| 45 | // kKeybits - kHashbits, and the values are bit strings of length kValuebits. |
| 46 | // |
| 47 | // In an effort to use minimal space, every cache entry represents |
| 48 | // some <key, value> pair; the class provides no way to mark a cache |
| 49 | // entry as empty or uninitialized. In practice, you may want to have |
| 50 | // reserved keys or values to get around this limitation. For example, in |
| 51 | // tcmalloc's PageID-to-sizeclass cache, a value of 0 is used as |
| 52 | // "unknown sizeclass." |
| 53 | // |
| 54 | // Usage Considerations |
| 55 | // -------------------- |
| 56 | // |
| 57 | // kHashbits controls the size of the cache. The best value for |
| 58 | // kHashbits will of course depend on the application. Perhaps try |
| 59 | // tuning the value of kHashbits by measuring different values on your |
| 60 | // favorite benchmark. Also remember not to be a pig; other |
| 61 | // programs that need resources may suffer if you are. |
| 62 | // |
| 63 | // The main uses for this class will be when performance is |
| 64 | // critical and there's a convenient type to hold the cache's |
| 65 | // entries. As described above, the number of bits required |
| 66 | // for a cache entry is (kKeybits - kHashbits) + kValuebits. Suppose |
| 67 | // kKeybits + kValuebits is 43. Then it probably makes sense to |
| 68 | // chose kHashbits >= 11 so that cache entries fit in a uint32. |
| 69 | // |
| 70 | // On the other hand, suppose kKeybits = kValuebits = 64. Then |
| 71 | // using this class may be less worthwhile. You'll probably |
| 72 | // be using 128 bits for each entry anyway, so maybe just pick |
| 73 | // a hash function, H, and use an array indexed by H(key): |
| 74 | // void Put(K key, V value) { a_[H(key)] = pair<K, V>(key, value); } |
| 75 | // V GetOrDefault(K key, V default) { const pair<K, V> &p = a_[H(key)]; ... } |
| 76 | // etc. |
| 77 | // |
| 78 | // Further Details |
| 79 | // --------------- |
| 80 | // |
| 81 | // For caches used only by one thread, the following is true: |
| 82 | // 1. For a cache c, |
| 83 | // (c.Put(key, value), c.GetOrDefault(key, 0)) == value |
| 84 | // and |
| 85 | // (c.Put(key, value), <...>, c.GetOrDefault(key, 0)) == value |
| 86 | // if the elided code contains no c.Put calls. |
| 87 | // |
| 88 | // 2. Has(key) will return false if no <key, value> pair with that key |
| 89 | // has ever been Put. However, a newly initialized cache will have |
| 90 | // some <key, value> pairs already present. When you create a new |
| 91 | // cache, you must specify an "initial value." The initialization |
| 92 | // procedure is equivalent to Clear(initial_value), which is |
| 93 | // equivalent to Put(k, initial_value) for all keys k from 0 to |
| 94 | // 2^kHashbits - 1. |
| 95 | // |
| 96 | // 3. If key and key' differ then the only way Put(key, value) may |
| 97 | // cause Has(key') to change is that Has(key') may change from true to |
| 98 | // false. Furthermore, a Put() call that doesn't change Has(key') |
| 99 | // doesn't change GetOrDefault(key', ...) either. |
| 100 | // |
| 101 | // Implementation details: |
| 102 | // |
| 103 | // This is a direct-mapped cache with 2^kHashbits entries; the hash |
| 104 | // function simply takes the low bits of the key. We store whole keys |
| 105 | // if a whole key plus a whole value fits in an entry. Otherwise, an |
| 106 | // entry is the high bits of a key and a value, packed together. |
| 107 | // E.g., a 20 bit key and a 7 bit value only require a uint16 for each |
| 108 | // entry if kHashbits >= 11. |
| 109 | // |
| 110 | // Alternatives to this scheme will be added as needed. |
| 111 | |
| 112 | #ifndef TCMALLOC_PACKED_CACHE_INL_H_ |
| 113 | #define TCMALLOC_PACKED_CACHE_INL_H_ |
| 114 | |
| 115 | #include "config.h" |
| 116 | #include <stddef.h> // for size_t |
| 117 | #ifdef HAVE_STDINT_H |
| 118 | #include <stdint.h> // for uintptr_t |
| 119 | #endif |
| 120 | #include "base/basictypes.h" |
Brian Silverman | 20350ac | 2021-11-17 18:19:55 -0800 | [diff] [blame] | 121 | #include "common.h" |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 122 | #include "internal_logging.h" |
| 123 | |
| 124 | // A safe way of doing "(1 << n) - 1" -- without worrying about overflow |
| 125 | // Note this will all be resolved to a constant expression at compile-time |
| 126 | #define N_ONES_(IntType, N) \ |
| 127 | ( (N) == 0 ? 0 : ((static_cast<IntType>(1) << ((N)-1))-1 + \ |
| 128 | (static_cast<IntType>(1) << ((N)-1))) ) |
| 129 | |
| 130 | // The types K and V provide upper bounds on the number of valid keys |
| 131 | // and values, but we explicitly require the keys to be less than |
Brian Silverman | 20350ac | 2021-11-17 18:19:55 -0800 | [diff] [blame] | 132 | // 2^kKeybits and the values to be less than 2^kValuebits. The size |
| 133 | // of the table is controlled by kHashbits, and the type of each entry |
| 134 | // in the cache is uintptr_t (native machine word). See also the big |
| 135 | // comment at the top of the file. |
| 136 | template <int kKeybits> |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 137 | class PackedCache { |
| 138 | public: |
Brian Silverman | 20350ac | 2021-11-17 18:19:55 -0800 | [diff] [blame] | 139 | typedef uintptr_t T; |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 140 | typedef uintptr_t K; |
Brian Silverman | 20350ac | 2021-11-17 18:19:55 -0800 | [diff] [blame] | 141 | typedef uint32 V; |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 142 | #ifdef TCMALLOC_SMALL_BUT_SLOW |
| 143 | // Decrease the size map cache if running in the small memory mode. |
| 144 | static const int kHashbits = 12; |
| 145 | #else |
| 146 | static const int kHashbits = 16; |
| 147 | #endif |
| 148 | static const int kValuebits = 7; |
Brian Silverman | 20350ac | 2021-11-17 18:19:55 -0800 | [diff] [blame] | 149 | // one bit after value bits |
| 150 | static const int kInvalidMask = 0x80; |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 151 | |
Brian Silverman | 20350ac | 2021-11-17 18:19:55 -0800 | [diff] [blame] | 152 | explicit PackedCache() { |
| 153 | COMPILE_ASSERT(kKeybits + kValuebits + 1 <= 8 * sizeof(T), use_whole_keys); |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 154 | COMPILE_ASSERT(kHashbits <= kKeybits, hash_function); |
Brian Silverman | 20350ac | 2021-11-17 18:19:55 -0800 | [diff] [blame] | 155 | COMPILE_ASSERT(kHashbits >= kValuebits + 1, small_values_space); |
| 156 | Clear(); |
| 157 | } |
| 158 | |
| 159 | bool TryGet(K key, V* out) const { |
| 160 | // As with other code in this class, we touch array_ as few times |
| 161 | // as we can. Assuming entries are read atomically then certain |
| 162 | // races are harmless. |
| 163 | ASSERT(key == (key & kKeyMask)); |
| 164 | T hash = Hash(key); |
| 165 | T expected_entry = key; |
| 166 | expected_entry &= ~N_ONES_(T, kHashbits); |
| 167 | T entry = array_[hash]; |
| 168 | entry ^= expected_entry; |
| 169 | if (PREDICT_FALSE(entry >= (1 << kValuebits))) { |
| 170 | return false; |
| 171 | } |
| 172 | *out = static_cast<V>(entry); |
| 173 | return true; |
| 174 | } |
| 175 | |
| 176 | void Clear() { |
| 177 | // sets 'invalid' bit in every byte, include value byte |
| 178 | memset(const_cast<T* >(array_), kInvalidMask, sizeof(array_)); |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | void Put(K key, V value) { |
| 182 | ASSERT(key == (key & kKeyMask)); |
| 183 | ASSERT(value == (value & kValueMask)); |
| 184 | array_[Hash(key)] = KeyToUpper(key) | value; |
| 185 | } |
| 186 | |
Brian Silverman | 20350ac | 2021-11-17 18:19:55 -0800 | [diff] [blame] | 187 | void Invalidate(K key) { |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 188 | ASSERT(key == (key & kKeyMask)); |
Brian Silverman | 20350ac | 2021-11-17 18:19:55 -0800 | [diff] [blame] | 189 | array_[Hash(key)] = KeyToUpper(key) | kInvalidMask; |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | private: |
Brian Silverman | 20350ac | 2021-11-17 18:19:55 -0800 | [diff] [blame] | 193 | // we just wipe all hash bits out of key. I.e. clear lower |
| 194 | // kHashbits. We rely on compiler knowing value of Hash(k). |
| 195 | static T KeyToUpper(K k) { |
| 196 | return static_cast<T>(k) ^ Hash(k); |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Brian Silverman | 20350ac | 2021-11-17 18:19:55 -0800 | [diff] [blame] | 199 | static T Hash(K key) { |
| 200 | return static_cast<T>(key) & N_ONES_(size_t, kHashbits); |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 203 | // For masking a K. |
| 204 | static const K kKeyMask = N_ONES_(K, kKeybits); |
| 205 | |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 206 | // For masking a V or a T. |
| 207 | static const V kValueMask = N_ONES_(V, kValuebits); |
| 208 | |
| 209 | // array_ is the cache. Its elements are volatile because any |
| 210 | // thread can write any array element at any time. |
| 211 | volatile T array_[1 << kHashbits]; |
| 212 | }; |
| 213 | |
| 214 | #undef N_ONES_ |
| 215 | |
| 216 | #endif // TCMALLOC_PACKED_CACHE_INL_H_ |