• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 The Abseil Authors.
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 //      https://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 
15 #include "absl/container/internal/raw_hash_set.h"
16 
17 #include <atomic>
18 #include <cassert>
19 #include <cstddef>
20 #include <cstring>
21 
22 #include "absl/base/attributes.h"
23 #include "absl/base/config.h"
24 #include "absl/base/dynamic_annotations.h"
25 #include "absl/hash/hash.h"
26 
27 namespace absl {
28 ABSL_NAMESPACE_BEGIN
29 namespace container_internal {
30 
31 // We have space for `growth_left` before a single block of control bytes. A
32 // single block of empty control bytes for tables without any slots allocated.
33 // This enables removing a branch in the hot path of find(). In order to ensure
34 // that the control bytes are aligned to 16, we have 16 bytes before the control
35 // bytes even though growth_left only needs 8.
ZeroCtrlT()36 constexpr ctrl_t ZeroCtrlT() { return static_cast<ctrl_t>(0); }
37 alignas(16) ABSL_CONST_INIT ABSL_DLL const ctrl_t kEmptyGroup[32] = {
38     ZeroCtrlT(),       ZeroCtrlT(),    ZeroCtrlT(),    ZeroCtrlT(),
39     ZeroCtrlT(),       ZeroCtrlT(),    ZeroCtrlT(),    ZeroCtrlT(),
40     ZeroCtrlT(),       ZeroCtrlT(),    ZeroCtrlT(),    ZeroCtrlT(),
41     ZeroCtrlT(),       ZeroCtrlT(),    ZeroCtrlT(),    ZeroCtrlT(),
42     ctrl_t::kSentinel, ctrl_t::kEmpty, ctrl_t::kEmpty, ctrl_t::kEmpty,
43     ctrl_t::kEmpty,    ctrl_t::kEmpty, ctrl_t::kEmpty, ctrl_t::kEmpty,
44     ctrl_t::kEmpty,    ctrl_t::kEmpty, ctrl_t::kEmpty, ctrl_t::kEmpty,
45     ctrl_t::kEmpty,    ctrl_t::kEmpty, ctrl_t::kEmpty, ctrl_t::kEmpty};
46 
47 #ifdef ABSL_INTERNAL_NEED_REDUNDANT_CONSTEXPR_DECL
48 constexpr size_t Group::kWidth;
49 #endif
50 
51 namespace {
52 
53 // Returns "random" seed.
RandomSeed()54 inline size_t RandomSeed() {
55 #ifdef ABSL_HAVE_THREAD_LOCAL
56   static thread_local size_t counter = 0;
57   // On Linux kernels >= 5.4 the MSAN runtime has a false-positive when
58   // accessing thread local storage data from loaded libraries
59   // (https://github.com/google/sanitizers/issues/1265), for this reason counter
60   // needs to be annotated as initialized.
61   ABSL_ANNOTATE_MEMORY_IS_INITIALIZED(&counter, sizeof(size_t));
62   size_t value = ++counter;
63 #else   // ABSL_HAVE_THREAD_LOCAL
64   static std::atomic<size_t> counter(0);
65   size_t value = counter.fetch_add(1, std::memory_order_relaxed);
66 #endif  // ABSL_HAVE_THREAD_LOCAL
67   return value ^ static_cast<size_t>(reinterpret_cast<uintptr_t>(&counter));
68 }
69 
ShouldRehashForBugDetection(const ctrl_t * ctrl,size_t capacity)70 bool ShouldRehashForBugDetection(const ctrl_t* ctrl, size_t capacity) {
71   // Note: we can't use the abseil-random library because abseil-random
72   // depends on swisstable. We want to return true with probability
73   // `min(1, RehashProbabilityConstant() / capacity())`. In order to do this,
74   // we probe based on a random hash and see if the offset is less than
75   // RehashProbabilityConstant().
76   return probe(ctrl, capacity, absl::HashOf(RandomSeed())).offset() <
77          RehashProbabilityConstant();
78 }
79 
80 }  // namespace
81 
EmptyGeneration()82 GenerationType* EmptyGeneration() {
83   if (SwisstableGenerationsEnabled()) {
84     constexpr size_t kNumEmptyGenerations = 1024;
85     static constexpr GenerationType kEmptyGenerations[kNumEmptyGenerations]{};
86     return const_cast<GenerationType*>(
87         &kEmptyGenerations[RandomSeed() % kNumEmptyGenerations]);
88   }
89   return nullptr;
90 }
91 
92 bool CommonFieldsGenerationInfoEnabled::
should_rehash_for_bug_detection_on_insert(const ctrl_t * ctrl,size_t capacity) const93     should_rehash_for_bug_detection_on_insert(const ctrl_t* ctrl,
94                                               size_t capacity) const {
95   if (reserved_growth_ == kReservedGrowthJustRanOut) return true;
96   if (reserved_growth_ > 0) return false;
97   return ShouldRehashForBugDetection(ctrl, capacity);
98 }
99 
should_rehash_for_bug_detection_on_move(const ctrl_t * ctrl,size_t capacity) const100 bool CommonFieldsGenerationInfoEnabled::should_rehash_for_bug_detection_on_move(
101     const ctrl_t* ctrl, size_t capacity) const {
102   return ShouldRehashForBugDetection(ctrl, capacity);
103 }
104 
ShouldInsertBackwards(size_t hash,const ctrl_t * ctrl)105 bool ShouldInsertBackwards(size_t hash, const ctrl_t* ctrl) {
106   // To avoid problems with weak hashes and single bit tests, we use % 13.
107   // TODO(kfm,sbenza): revisit after we do unconditional mixing
108   return (H1(hash, ctrl) ^ RandomSeed()) % 13 > 6;
109 }
110 
ConvertDeletedToEmptyAndFullToDeleted(ctrl_t * ctrl,size_t capacity)111 void ConvertDeletedToEmptyAndFullToDeleted(ctrl_t* ctrl, size_t capacity) {
112   assert(ctrl[capacity] == ctrl_t::kSentinel);
113   assert(IsValidCapacity(capacity));
114   for (ctrl_t* pos = ctrl; pos < ctrl + capacity; pos += Group::kWidth) {
115     Group{pos}.ConvertSpecialToEmptyAndFullToDeleted(pos);
116   }
117   // Copy the cloned ctrl bytes.
118   std::memcpy(ctrl + capacity + 1, ctrl, NumClonedBytes());
119   ctrl[capacity] = ctrl_t::kSentinel;
120 }
121 // Extern template instantiation for inline function.
122 template FindInfo find_first_non_full(const CommonFields&, size_t);
123 
find_first_non_full_outofline(const CommonFields & common,size_t hash)124 FindInfo find_first_non_full_outofline(const CommonFields& common,
125                                        size_t hash) {
126   return find_first_non_full(common, hash);
127 }
128 
129 // Returns the address of the ith slot in slots where each slot occupies
130 // slot_size.
SlotAddress(void * slot_array,size_t slot,size_t slot_size)131 static inline void* SlotAddress(void* slot_array, size_t slot,
132                                 size_t slot_size) {
133   return reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(slot_array) +
134                                  (slot * slot_size));
135 }
136 
137 // Returns the address of the slot just after slot assuming each slot has the
138 // specified size.
NextSlot(void * slot,size_t slot_size)139 static inline void* NextSlot(void* slot, size_t slot_size) {
140   return reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(slot) + slot_size);
141 }
142 
143 // Returns the address of the slot just before slot assuming each slot has the
144 // specified size.
PrevSlot(void * slot,size_t slot_size)145 static inline void* PrevSlot(void* slot, size_t slot_size) {
146   return reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(slot) - slot_size);
147 }
148 
DropDeletesWithoutResize(CommonFields & common,const PolicyFunctions & policy,void * tmp_space)149 void DropDeletesWithoutResize(CommonFields& common,
150                               const PolicyFunctions& policy, void* tmp_space) {
151   void* set = &common;
152   void* slot_array = common.slot_array();
153   const size_t capacity = common.capacity();
154   assert(IsValidCapacity(capacity));
155   assert(!is_small(capacity));
156   // Algorithm:
157   // - mark all DELETED slots as EMPTY
158   // - mark all FULL slots as DELETED
159   // - for each slot marked as DELETED
160   //     hash = Hash(element)
161   //     target = find_first_non_full(hash)
162   //     if target is in the same group
163   //       mark slot as FULL
164   //     else if target is EMPTY
165   //       transfer element to target
166   //       mark slot as EMPTY
167   //       mark target as FULL
168   //     else if target is DELETED
169   //       swap current element with target element
170   //       mark target as FULL
171   //       repeat procedure for current slot with moved from element (target)
172   ctrl_t* ctrl = common.control();
173   ConvertDeletedToEmptyAndFullToDeleted(ctrl, capacity);
174   auto hasher = policy.hash_slot;
175   auto transfer = policy.transfer;
176   const size_t slot_size = policy.slot_size;
177 
178   size_t total_probe_length = 0;
179   void* slot_ptr = SlotAddress(slot_array, 0, slot_size);
180   for (size_t i = 0; i != capacity;
181        ++i, slot_ptr = NextSlot(slot_ptr, slot_size)) {
182     assert(slot_ptr == SlotAddress(slot_array, i, slot_size));
183     if (!IsDeleted(ctrl[i])) continue;
184     const size_t hash = (*hasher)(set, slot_ptr);
185     const FindInfo target = find_first_non_full(common, hash);
186     const size_t new_i = target.offset;
187     total_probe_length += target.probe_length;
188 
189     // Verify if the old and new i fall within the same group wrt the hash.
190     // If they do, we don't need to move the object as it falls already in the
191     // best probe we can.
192     const size_t probe_offset = probe(common, hash).offset();
193     const auto probe_index = [probe_offset, capacity](size_t pos) {
194       return ((pos - probe_offset) & capacity) / Group::kWidth;
195     };
196 
197     // Element doesn't move.
198     if (ABSL_PREDICT_TRUE(probe_index(new_i) == probe_index(i))) {
199       SetCtrl(common, i, H2(hash), slot_size);
200       continue;
201     }
202 
203     void* new_slot_ptr = SlotAddress(slot_array, new_i, slot_size);
204     if (IsEmpty(ctrl[new_i])) {
205       // Transfer element to the empty spot.
206       // SetCtrl poisons/unpoisons the slots so we have to call it at the
207       // right time.
208       SetCtrl(common, new_i, H2(hash), slot_size);
209       (*transfer)(set, new_slot_ptr, slot_ptr);
210       SetCtrl(common, i, ctrl_t::kEmpty, slot_size);
211     } else {
212       assert(IsDeleted(ctrl[new_i]));
213       SetCtrl(common, new_i, H2(hash), slot_size);
214       // Until we are done rehashing, DELETED marks previously FULL slots.
215 
216       // Swap i and new_i elements.
217       (*transfer)(set, tmp_space, new_slot_ptr);
218       (*transfer)(set, new_slot_ptr, slot_ptr);
219       (*transfer)(set, slot_ptr, tmp_space);
220 
221       // repeat the processing of the ith slot
222       --i;
223       slot_ptr = PrevSlot(slot_ptr, slot_size);
224     }
225   }
226   ResetGrowthLeft(common);
227   common.infoz().RecordRehash(total_probe_length);
228 }
229 
EraseMetaOnly(CommonFields & c,ctrl_t * it,size_t slot_size)230 void EraseMetaOnly(CommonFields& c, ctrl_t* it, size_t slot_size) {
231   assert(IsFull(*it) && "erasing a dangling iterator");
232   c.decrement_size();
233   const auto index = static_cast<size_t>(it - c.control());
234   const size_t index_before = (index - Group::kWidth) & c.capacity();
235   const auto empty_after = Group(it).MaskEmpty();
236   const auto empty_before = Group(c.control() + index_before).MaskEmpty();
237 
238   // We count how many consecutive non empties we have to the right and to the
239   // left of `it`. If the sum is >= kWidth then there is at least one probe
240   // window that might have seen a full group.
241   bool was_never_full = empty_before && empty_after &&
242                         static_cast<size_t>(empty_after.TrailingZeros()) +
243                                 empty_before.LeadingZeros() <
244                             Group::kWidth;
245 
246   SetCtrl(c, index, was_never_full ? ctrl_t::kEmpty : ctrl_t::kDeleted,
247           slot_size);
248   c.set_growth_left(c.growth_left() + (was_never_full ? 1 : 0));
249   c.infoz().RecordErase();
250 }
251 
ClearBackingArray(CommonFields & c,const PolicyFunctions & policy,bool reuse)252 void ClearBackingArray(CommonFields& c, const PolicyFunctions& policy,
253                        bool reuse) {
254   c.set_size(0);
255   if (reuse) {
256     ResetCtrl(c, policy.slot_size);
257     c.infoz().RecordStorageChanged(0, c.capacity());
258   } else {
259     // We need to record infoz before calling dealloc, which will unregister
260     // infoz.
261     c.infoz().RecordClearedReservation();
262     c.infoz().RecordStorageChanged(0, 0);
263     (*policy.dealloc)(c, policy);
264     c.set_control(EmptyGroup());
265     c.set_generation_ptr(EmptyGeneration());
266     c.set_slots(nullptr);
267     c.set_capacity(0);
268   }
269 }
270 
271 }  // namespace container_internal
272 ABSL_NAMESPACE_END
273 }  // namespace absl
274