raw_hash_set.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. #include "y_absl/container/internal/raw_hash_set.h"
  15. #include <atomic>
  16. #include <cassert>
  17. #include <cstddef>
  18. #include <cstdint>
  19. #include <cstring>
  20. #include "y_absl/base/attributes.h"
  21. #include "y_absl/base/config.h"
  22. #include "y_absl/base/dynamic_annotations.h"
  23. #include "y_absl/container/internal/container_memory.h"
  24. #include "y_absl/hash/hash.h"
  25. namespace y_absl {
  26. Y_ABSL_NAMESPACE_BEGIN
  27. namespace container_internal {
  28. // We have space for `growth_left` before a single block of control bytes. A
  29. // single block of empty control bytes for tables without any slots allocated.
  30. // This enables removing a branch in the hot path of find(). In order to ensure
  31. // that the control bytes are aligned to 16, we have 16 bytes before the control
  32. // bytes even though growth_left only needs 8.
  33. constexpr ctrl_t ZeroCtrlT() { return static_cast<ctrl_t>(0); }
  34. alignas(16) Y_ABSL_CONST_INIT Y_ABSL_DLL const ctrl_t kEmptyGroup[32] = {
  35. ZeroCtrlT(), ZeroCtrlT(), ZeroCtrlT(), ZeroCtrlT(),
  36. ZeroCtrlT(), ZeroCtrlT(), ZeroCtrlT(), ZeroCtrlT(),
  37. ZeroCtrlT(), ZeroCtrlT(), ZeroCtrlT(), ZeroCtrlT(),
  38. ZeroCtrlT(), ZeroCtrlT(), ZeroCtrlT(), ZeroCtrlT(),
  39. ctrl_t::kSentinel, ctrl_t::kEmpty, ctrl_t::kEmpty, ctrl_t::kEmpty,
  40. ctrl_t::kEmpty, ctrl_t::kEmpty, ctrl_t::kEmpty, ctrl_t::kEmpty,
  41. ctrl_t::kEmpty, ctrl_t::kEmpty, ctrl_t::kEmpty, ctrl_t::kEmpty,
  42. ctrl_t::kEmpty, ctrl_t::kEmpty, ctrl_t::kEmpty, ctrl_t::kEmpty};
  43. #ifdef Y_ABSL_INTERNAL_NEED_REDUNDANT_CONSTEXPR_DECL
  44. constexpr size_t Group::kWidth;
  45. #endif
  46. namespace {
  47. // Returns "random" seed.
  48. inline size_t RandomSeed() {
  49. #ifdef Y_ABSL_HAVE_THREAD_LOCAL
  50. static thread_local size_t counter = 0;
  51. // On Linux kernels >= 5.4 the MSAN runtime has a false-positive when
  52. // accessing thread local storage data from loaded libraries
  53. // (https://github.com/google/sanitizers/issues/1265), for this reason counter
  54. // needs to be annotated as initialized.
  55. Y_ABSL_ANNOTATE_MEMORY_IS_INITIALIZED(&counter, sizeof(size_t));
  56. size_t value = ++counter;
  57. #else // Y_ABSL_HAVE_THREAD_LOCAL
  58. static std::atomic<size_t> counter(0);
  59. size_t value = counter.fetch_add(1, std::memory_order_relaxed);
  60. #endif // Y_ABSL_HAVE_THREAD_LOCAL
  61. return value ^ static_cast<size_t>(reinterpret_cast<uintptr_t>(&counter));
  62. }
  63. bool ShouldRehashForBugDetection(const ctrl_t* ctrl, size_t capacity) {
  64. // Note: we can't use the abseil-random library because abseil-random
  65. // depends on swisstable. We want to return true with probability
  66. // `min(1, RehashProbabilityConstant() / capacity())`. In order to do this,
  67. // we probe based on a random hash and see if the offset is less than
  68. // RehashProbabilityConstant().
  69. return probe(ctrl, capacity, y_absl::HashOf(RandomSeed())).offset() <
  70. RehashProbabilityConstant();
  71. }
  72. } // namespace
  73. GenerationType* EmptyGeneration() {
  74. if (SwisstableGenerationsEnabled()) {
  75. constexpr size_t kNumEmptyGenerations = 1024;
  76. static constexpr GenerationType kEmptyGenerations[kNumEmptyGenerations]{};
  77. return const_cast<GenerationType*>(
  78. &kEmptyGenerations[RandomSeed() % kNumEmptyGenerations]);
  79. }
  80. return nullptr;
  81. }
  82. bool CommonFieldsGenerationInfoEnabled::
  83. should_rehash_for_bug_detection_on_insert(const ctrl_t* ctrl,
  84. size_t capacity) const {
  85. if (reserved_growth_ == kReservedGrowthJustRanOut) return true;
  86. if (reserved_growth_ > 0) return false;
  87. return ShouldRehashForBugDetection(ctrl, capacity);
  88. }
  89. bool CommonFieldsGenerationInfoEnabled::should_rehash_for_bug_detection_on_move(
  90. const ctrl_t* ctrl, size_t capacity) const {
  91. return ShouldRehashForBugDetection(ctrl, capacity);
  92. }
  93. bool ShouldInsertBackwards(size_t hash, const ctrl_t* ctrl) {
  94. // To avoid problems with weak hashes and single bit tests, we use % 13.
  95. // TODO(kfm,sbenza): revisit after we do unconditional mixing
  96. return (H1(hash, ctrl) ^ RandomSeed()) % 13 > 6;
  97. }
  98. void ConvertDeletedToEmptyAndFullToDeleted(ctrl_t* ctrl, size_t capacity) {
  99. assert(ctrl[capacity] == ctrl_t::kSentinel);
  100. assert(IsValidCapacity(capacity));
  101. for (ctrl_t* pos = ctrl; pos < ctrl + capacity; pos += Group::kWidth) {
  102. Group{pos}.ConvertSpecialToEmptyAndFullToDeleted(pos);
  103. }
  104. // Copy the cloned ctrl bytes.
  105. std::memcpy(ctrl + capacity + 1, ctrl, NumClonedBytes());
  106. ctrl[capacity] = ctrl_t::kSentinel;
  107. }
  108. // Extern template instantiation for inline function.
  109. template FindInfo find_first_non_full(const CommonFields&, size_t);
  110. FindInfo find_first_non_full_outofline(const CommonFields& common,
  111. size_t hash) {
  112. return find_first_non_full(common, hash);
  113. }
  114. // Returns the address of the slot just after slot assuming each slot has the
  115. // specified size.
  116. static inline void* NextSlot(void* slot, size_t slot_size) {
  117. return reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(slot) + slot_size);
  118. }
  119. // Returns the address of the slot just before slot assuming each slot has the
  120. // specified size.
  121. static inline void* PrevSlot(void* slot, size_t slot_size) {
  122. return reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(slot) - slot_size);
  123. }
  124. void DropDeletesWithoutResize(CommonFields& common,
  125. const PolicyFunctions& policy, void* tmp_space) {
  126. void* set = &common;
  127. void* slot_array = common.slot_array();
  128. const size_t capacity = common.capacity();
  129. assert(IsValidCapacity(capacity));
  130. assert(!is_small(capacity));
  131. // Algorithm:
  132. // - mark all DELETED slots as EMPTY
  133. // - mark all FULL slots as DELETED
  134. // - for each slot marked as DELETED
  135. // hash = Hash(element)
  136. // target = find_first_non_full(hash)
  137. // if target is in the same group
  138. // mark slot as FULL
  139. // else if target is EMPTY
  140. // transfer element to target
  141. // mark slot as EMPTY
  142. // mark target as FULL
  143. // else if target is DELETED
  144. // swap current element with target element
  145. // mark target as FULL
  146. // repeat procedure for current slot with moved from element (target)
  147. ctrl_t* ctrl = common.control();
  148. ConvertDeletedToEmptyAndFullToDeleted(ctrl, capacity);
  149. auto hasher = policy.hash_slot;
  150. auto transfer = policy.transfer;
  151. const size_t slot_size = policy.slot_size;
  152. size_t total_probe_length = 0;
  153. void* slot_ptr = SlotAddress(slot_array, 0, slot_size);
  154. for (size_t i = 0; i != capacity;
  155. ++i, slot_ptr = NextSlot(slot_ptr, slot_size)) {
  156. assert(slot_ptr == SlotAddress(slot_array, i, slot_size));
  157. if (!IsDeleted(ctrl[i])) continue;
  158. const size_t hash = (*hasher)(set, slot_ptr);
  159. const FindInfo target = find_first_non_full(common, hash);
  160. const size_t new_i = target.offset;
  161. total_probe_length += target.probe_length;
  162. // Verify if the old and new i fall within the same group wrt the hash.
  163. // If they do, we don't need to move the object as it falls already in the
  164. // best probe we can.
  165. const size_t probe_offset = probe(common, hash).offset();
  166. const auto probe_index = [probe_offset, capacity](size_t pos) {
  167. return ((pos - probe_offset) & capacity) / Group::kWidth;
  168. };
  169. // Element doesn't move.
  170. if (Y_ABSL_PREDICT_TRUE(probe_index(new_i) == probe_index(i))) {
  171. SetCtrl(common, i, H2(hash), slot_size);
  172. continue;
  173. }
  174. void* new_slot_ptr = SlotAddress(slot_array, new_i, slot_size);
  175. if (IsEmpty(ctrl[new_i])) {
  176. // Transfer element to the empty spot.
  177. // SetCtrl poisons/unpoisons the slots so we have to call it at the
  178. // right time.
  179. SetCtrl(common, new_i, H2(hash), slot_size);
  180. (*transfer)(set, new_slot_ptr, slot_ptr);
  181. SetCtrl(common, i, ctrl_t::kEmpty, slot_size);
  182. } else {
  183. assert(IsDeleted(ctrl[new_i]));
  184. SetCtrl(common, new_i, H2(hash), slot_size);
  185. // Until we are done rehashing, DELETED marks previously FULL slots.
  186. // Swap i and new_i elements.
  187. (*transfer)(set, tmp_space, new_slot_ptr);
  188. (*transfer)(set, new_slot_ptr, slot_ptr);
  189. (*transfer)(set, slot_ptr, tmp_space);
  190. // repeat the processing of the ith slot
  191. --i;
  192. slot_ptr = PrevSlot(slot_ptr, slot_size);
  193. }
  194. }
  195. ResetGrowthLeft(common);
  196. common.infoz().RecordRehash(total_probe_length);
  197. }
  198. static bool WasNeverFull(CommonFields& c, size_t index) {
  199. if (is_single_group(c.capacity())) {
  200. return true;
  201. }
  202. const size_t index_before = (index - Group::kWidth) & c.capacity();
  203. const auto empty_after = Group(c.control() + index).MaskEmpty();
  204. const auto empty_before = Group(c.control() + index_before).MaskEmpty();
  205. // We count how many consecutive non empties we have to the right and to the
  206. // left of `it`. If the sum is >= kWidth then there is at least one probe
  207. // window that might have seen a full group.
  208. return empty_before && empty_after &&
  209. static_cast<size_t>(empty_after.TrailingZeros()) +
  210. empty_before.LeadingZeros() <
  211. Group::kWidth;
  212. }
  213. void EraseMetaOnly(CommonFields& c, size_t index, size_t slot_size) {
  214. assert(IsFull(c.control()[index]) && "erasing a dangling iterator");
  215. c.decrement_size();
  216. c.infoz().RecordErase();
  217. if (WasNeverFull(c, index)) {
  218. SetCtrl(c, index, ctrl_t::kEmpty, slot_size);
  219. c.set_growth_left(c.growth_left() + 1);
  220. return;
  221. }
  222. SetCtrl(c, index, ctrl_t::kDeleted, slot_size);
  223. }
  224. void ClearBackingArray(CommonFields& c, const PolicyFunctions& policy,
  225. bool reuse) {
  226. c.set_size(0);
  227. if (reuse) {
  228. ResetCtrl(c, policy.slot_size);
  229. ResetGrowthLeft(c);
  230. c.infoz().RecordStorageChanged(0, c.capacity());
  231. } else {
  232. // We need to record infoz before calling dealloc, which will unregister
  233. // infoz.
  234. c.infoz().RecordClearedReservation();
  235. c.infoz().RecordStorageChanged(0, 0);
  236. (*policy.dealloc)(c, policy);
  237. c.set_control(EmptyGroup());
  238. c.set_generation_ptr(EmptyGeneration());
  239. c.set_slots(nullptr);
  240. c.set_capacity(0);
  241. }
  242. }
  243. void HashSetResizeHelper::GrowIntoSingleGroupShuffleControlBytes(
  244. ctrl_t* new_ctrl, size_t new_capacity) const {
  245. assert(is_single_group(new_capacity));
  246. constexpr size_t kHalfWidth = Group::kWidth / 2;
  247. assert(old_capacity_ < kHalfWidth);
  248. const size_t half_old_capacity = old_capacity_ / 2;
  249. // NOTE: operations are done with compile time known size = kHalfWidth.
  250. // Compiler optimizes that into single ASM operation.
  251. // Copy second half of bytes to the beginning.
  252. // We potentially copy more bytes in order to have compile time known size.
  253. // Mirrored bytes from the old_ctrl_ will also be copied.
  254. // In case of old_capacity_ == 3, we will copy 1st element twice.
  255. // Examples:
  256. // old_ctrl = 0S0EEEEEEE...
  257. // new_ctrl = S0EEEEEEEE...
  258. //
  259. // old_ctrl = 01S01EEEEE...
  260. // new_ctrl = 1S01EEEEEE...
  261. //
  262. // old_ctrl = 0123456S0123456EE...
  263. // new_ctrl = 456S0123?????????...
  264. std::memcpy(new_ctrl, old_ctrl_ + half_old_capacity + 1, kHalfWidth);
  265. // Clean up copied kSentinel from old_ctrl.
  266. new_ctrl[half_old_capacity] = ctrl_t::kEmpty;
  267. // Clean up damaged or uninitialized bytes.
  268. // Clean bytes after the intended size of the copy.
  269. // Example:
  270. // new_ctrl = 1E01EEEEEEE????
  271. // *new_ctrl= 1E0EEEEEEEE????
  272. // position /
  273. std::memset(new_ctrl + old_capacity_ + 1, static_cast<int8_t>(ctrl_t::kEmpty),
  274. kHalfWidth);
  275. // Clean non-mirrored bytes that are not initialized.
  276. // For small old_capacity that may be inside of mirrored bytes zone.
  277. // Examples:
  278. // new_ctrl = 1E0EEEEEEEE??????????....
  279. // *new_ctrl= 1E0EEEEEEEEEEEEE?????....
  280. // position /
  281. //
  282. // new_ctrl = 456E0123???????????...
  283. // *new_ctrl= 456E0123EEEEEEEE???...
  284. // position /
  285. std::memset(new_ctrl + kHalfWidth, static_cast<int8_t>(ctrl_t::kEmpty),
  286. kHalfWidth);
  287. // Clean last mirrored bytes that are not initialized
  288. // and will not be overwritten by mirroring.
  289. // Examples:
  290. // new_ctrl = 1E0EEEEEEEEEEEEE????????
  291. // *new_ctrl= 1E0EEEEEEEEEEEEEEEEEEEEE
  292. // position S /
  293. //
  294. // new_ctrl = 456E0123EEEEEEEE???????????????
  295. // *new_ctrl= 456E0123EEEEEEEE???????EEEEEEEE
  296. // position S /
  297. std::memset(new_ctrl + new_capacity + kHalfWidth,
  298. static_cast<int8_t>(ctrl_t::kEmpty), kHalfWidth);
  299. // Create mirrored bytes. old_capacity_ < kHalfWidth
  300. // Example:
  301. // new_ctrl = 456E0123EEEEEEEE???????EEEEEEEE
  302. // *new_ctrl= 456E0123EEEEEEEE456E0123EEEEEEE
  303. // position S/
  304. ctrl_t g[kHalfWidth];
  305. std::memcpy(g, new_ctrl, kHalfWidth);
  306. std::memcpy(new_ctrl + new_capacity + 1, g, kHalfWidth);
  307. // Finally set sentinel to its place.
  308. new_ctrl[new_capacity] = ctrl_t::kSentinel;
  309. }
  310. void HashSetResizeHelper::GrowIntoSingleGroupShuffleTransferableSlots(
  311. void* old_slots, void* new_slots, size_t slot_size) const {
  312. assert(old_capacity_ > 0);
  313. const size_t half_old_capacity = old_capacity_ / 2;
  314. SanitizerUnpoisonMemoryRegion(old_slots, slot_size * old_capacity_);
  315. std::memcpy(new_slots,
  316. SlotAddress(old_slots, half_old_capacity + 1, slot_size),
  317. slot_size * half_old_capacity);
  318. std::memcpy(SlotAddress(new_slots, half_old_capacity + 1, slot_size),
  319. old_slots, slot_size * (half_old_capacity + 1));
  320. }
  321. void HashSetResizeHelper::GrowSizeIntoSingleGroupTransferable(
  322. CommonFields& c, void* old_slots, size_t slot_size) {
  323. assert(old_capacity_ < Group::kWidth / 2);
  324. assert(is_single_group(c.capacity()));
  325. assert(IsGrowingIntoSingleGroupApplicable(old_capacity_, c.capacity()));
  326. GrowIntoSingleGroupShuffleControlBytes(c.control(), c.capacity());
  327. GrowIntoSingleGroupShuffleTransferableSlots(old_slots, c.slot_array(),
  328. slot_size);
  329. // We poison since GrowIntoSingleGroupShuffleTransferableSlots
  330. // may leave empty slots unpoisoned.
  331. PoisonSingleGroupEmptySlots(c, slot_size);
  332. }
  333. } // namespace container_internal
  334. Y_ABSL_NAMESPACE_END
  335. } // namespace y_absl