Coverage Report

Created: 2026-09-14 06:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/abseil-cpp/absl/container/internal/raw_hash_set.cc
Line
Count
Source
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 <algorithm>
18
#include <atomic>
19
#include <cassert>
20
#include <cstddef>
21
#include <cstdint>
22
#include <cstring>
23
#include <memory>
24
#include <tuple>
25
#include <type_traits>
26
#include <utility>
27
28
#include "absl/base/attributes.h"
29
#include "absl/base/config.h"
30
#include "absl/base/dynamic_annotations.h"
31
#include "absl/base/internal/endian.h"
32
#include "absl/base/internal/raw_logging.h"
33
#include "absl/base/optimization.h"
34
#include "absl/container/internal/container_memory.h"
35
#include "absl/container/internal/hashtable_control_bytes.h"
36
#include "absl/container/internal/hashtablez_sampler.h"
37
#include "absl/container/internal/raw_hash_set_resize_impl.h"
38
#include "absl/functional/function_ref.h"
39
#include "absl/hash/hash.h"
40
41
namespace absl {
42
ABSL_NAMESPACE_BEGIN
43
namespace container_internal {
44
45
// Represents a control byte corresponding to a full slot with arbitrary hash.
46
0
constexpr ctrl_t ZeroCtrlT() { return static_cast<ctrl_t>(0); }
47
48
// A single byte for default-constructed iterators. We leave it uninitialized
49
// because reading this memory is a bug.
50
ABSL_DLL char kDefaultIterSlot;
51
52
// We need one full byte followed by a sentinel byte for iterator::operator++.
53
ABSL_CONST_INIT ABSL_DLL const ctrl_t kSooControl[2] = {ZeroCtrlT(),
54
                                                        ctrl_t::kSentinel};
55
// We need one full byte followed by a sentinel byte for iterator::operator++.
56
ABSL_CONST_INIT ABSL_DLL const ctrl_t kInsertIteratorControl[2] = {
57
    ZeroCtrlT(), ctrl_t::kSentinel};
58
59
namespace {
60
61
#ifdef ABSL_SWISSTABLE_ASSERT
62
#error ABSL_SWISSTABLE_ASSERT cannot be directly set
63
#else
64
// We use this macro for assertions that users may see when the table is in an
65
// invalid state that sanitizers may help diagnose.
66
#define ABSL_SWISSTABLE_ASSERT(CONDITION) \
67
57.9M
  assert((CONDITION) && "Try enabling sanitizers.")
68
#endif
69
70
void ValidateMaxSize([[maybe_unused]] size_t size,
71
                     [[maybe_unused]] size_t key_size,
72
0
                     [[maybe_unused]] size_t slot_size) {
73
0
  ABSL_SWISSTABLE_ASSERT(size <= MaxValidSize(key_size, slot_size));
74
0
}
75
0
void ValidateMaxCapacity(size_t capacity, size_t key_size, size_t slot_size) {
76
0
  if (capacity <= 1) return;
77
0
  ValidateMaxSize(CapacityToGrowth(PreviousCapacity(capacity)), key_size,
78
0
                  slot_size);
79
0
}
80
81
// Returns "random" seed.
82
56.6k
inline size_t RandomSeed() {
83
56.6k
  constexpr size_t kIncrement = 0xad53;
84
56.6k
#ifdef ABSL_HAVE_THREAD_LOCAL
85
56.6k
  static thread_local size_t counter = 0;
86
56.6k
  counter += kIncrement;
87
56.6k
  size_t value = counter;
88
#else   // ABSL_HAVE_THREAD_LOCAL
89
  static std::atomic<size_t> counter(0);
90
  size_t value = counter.fetch_add(kIncrement, std::memory_order_relaxed);
91
#endif  // ABSL_HAVE_THREAD_LOCAL
92
56.6k
  return value ^ static_cast<size_t>(reinterpret_cast<uintptr_t>(&counter));
93
56.6k
}
94
95
0
bool ShouldRehashForBugDetection(size_t capacity) {
96
  // Note: we can't use the abseil-random library because abseil-random
97
  // depends on swisstable. We want to return true with probability
98
  // `min(1, RehashProbabilityConstant() / capacity())`. In order to do this,
99
  // we probe based on a random hash and see if the offset is less than
100
  // RehashProbabilityConstant().
101
0
  return probe(ProbeCapacity{capacity}, absl::HashOf(RandomSeed()))
102
0
             .offset() < RehashProbabilityConstant();
103
0
}
104
105
// Returns the offset of the new element after resize from capacity 1 to 3.
106
56.6k
size_t Resize1To3NewOffset(size_t hash) {
107
  // After resize from capacity 1 to 3, we always have exactly the slot with
108
  // index 1 occupied, so we need to insert either at index 0 or index 2.
109
56.6k
  static_assert(SooSlotIndex() == 1);
110
56.6k
  return hash & 2;
111
56.6k
}
112
113
// Returns the address of the ith slot in slots where each slot occupies
114
// slot_size.
115
11.1M
inline void* SlotAddress(void* slot_array, size_t slot, size_t slot_size) {
116
11.1M
  return static_cast<void*>(static_cast<char*>(slot_array) +
117
11.1M
                            (slot * slot_size));
118
11.1M
}
119
120
// Returns the address of the slot `i` iterations after `slot` assuming each
121
// slot has the specified size.
122
100k
inline void* NextSlot(void* slot, size_t slot_size, size_t i = 1) {
123
100k
  return reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(slot) +
124
100k
                                 slot_size * i);
125
100k
}
126
127
// Returns the address of the slot just before `slot` assuming each slot has the
128
// specified size.
129
0
inline void* PrevSlot(void* slot, size_t slot_size) {
130
0
  return reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(slot) - slot_size);
131
0
}
132
133
}  // namespace
134
135
// Must be defined out-of-line to avoid MSVC error C2482 on some platforms,
136
// which is caused by non-constexpr initialization.
137
56.6k
uint8_t NextHashTableSeed() {
138
56.6k
  return static_cast<uint8_t>(RandomSeed());
139
56.6k
}
140
141
0
GenerationType* EmptyGeneration() {
142
0
  if (SwisstableGenerationsEnabled()) {
143
0
    constexpr size_t kNumEmptyGenerations = 1024;
144
0
    static constexpr GenerationType kEmptyGenerations[kNumEmptyGenerations]{};
145
0
    return const_cast<GenerationType*>(
146
0
        &kEmptyGenerations[RandomSeed() % kNumEmptyGenerations]);
147
0
  }
148
0
  return nullptr;
149
0
}
150
151
bool CommonFieldsGenerationInfoEnabled::
152
0
    should_rehash_for_bug_detection_on_insert(size_t capacity) const {
153
0
  if (reserved_growth_ == kReservedGrowthJustRanOut) return true;
154
0
  if (reserved_growth_ > 0) return false;
155
0
  return ShouldRehashForBugDetection(capacity);
156
0
}
157
158
bool CommonFieldsGenerationInfoEnabled::should_rehash_for_bug_detection_on_move(
159
0
    size_t capacity) const {
160
0
  return ShouldRehashForBugDetection(capacity);
161
0
}
162
163
namespace {
164
165
// Probes an array of control bits using a probe sequence,
166
// and returns the mask corresponding to the first group with a deleted or empty
167
// slot.
168
inline Group::NonIterableBitMaskType probe_till_first_non_full_group(
169
    const ctrl_t* ctrl, probe_seq<Group::kWidth>& seq,
170
117k
    [[maybe_unused]] size_t capacity) {
171
121k
  while (true) {
172
121k
    GroupFullEmptyOrDeleted g{ctrl + seq.offset()};
173
121k
    auto mask = g.MaskEmptyOrDeleted();
174
121k
    if (mask) {
175
117k
      return mask;
176
117k
    }
177
3.74k
    seq.next();
178
3.74k
    ABSL_SWISSTABLE_ASSERT(seq.index() <= capacity && "full table!");
179
3.74k
  }
180
117k
}
181
182
FindInfo find_first_non_full_from_h1(const ctrl_t* ctrl, size_t h1,
183
226k
                                     HashtableCapacity capacity) {
184
226k
  const size_t cap = capacity.capacity();
185
226k
  auto seq = probe_h1(ProbeCapacity{cap}, h1);
186
226k
  if (IsEmptyOrDeleted(ctrl[seq.offset()])) {
187
108k
    return {seq.offset(), /*probe_length=*/0};
188
108k
  }
189
117k
  auto mask = probe_till_first_non_full_group(ctrl, seq, cap);
190
117k
  return {seq.offset(mask.LowestBitSet()), seq.index()};
191
226k
}
192
193
// Probes an array of control bits using a probe sequence derived from `hash`,
194
// and returns the offset corresponding to the first deleted or empty slot.
195
//
196
// Behavior when the entire table is full is undefined.
197
//
198
// NOTE: this function must work with tables having both empty and deleted
199
// slots in the same group. Such tables appear during `erase()`.
200
113k
FindInfo find_first_non_full(const CommonFields& common, size_t hash) {
201
113k
  return find_first_non_full_from_h1(common.control(), H1(hash),
202
113k
                                     common.capacity_impl());
203
113k
}
204
205
// Same as `find_first_non_full`, but returns the mask corresponding to the
206
// first group with a deleted or empty slot.
207
std::pair<FindInfo, Group::NonIterableBitMaskType> find_first_non_full_group(
208
0
    const CommonFields& common, size_t hash) {
209
0
  auto seq = probe(common, hash);
210
0
  auto mask =
211
0
      probe_till_first_non_full_group(common.control(), seq, common.capacity());
212
0
  return {{seq.offset(), seq.index()}, mask};
213
0
}
214
215
// Whether a table fits in half a group. A half-group table fits entirely into a
216
// probing group, i.e., has a capacity < `Group::kWidth`.
217
//
218
// In half-group mode we are able to use the whole capacity. The extra control
219
// bytes give us at least one "empty" control byte to stop the iteration.
220
// This is important to make 1 a valid capacity.
221
//
222
// In half-group mode only the first `capacity` control bytes after the sentinel
223
// are valid. The rest contain dummy ctrl_t::kEmpty values that do not
224
// represent a real slot.
225
0
constexpr bool is_half_group(size_t capacity) {
226
0
  return capacity < Group::kWidth - 1;
227
0
}
228
229
template <class Fn>
230
0
void IterateOverFullSlotsImpl(const CommonFields& c, size_t slot_size, Fn cb) {
231
0
  const size_t cap = c.capacity();
232
0
  ABSL_ASSUME(cap > kMaxSmallCapacity);
233
0
  const ctrl_t* ctrl = c.control();
234
0
  void* slot = c.slot_array(cap);
235
0
  if (is_half_group(cap)) {
236
    // Mirrored/cloned control bytes in half-group table are also located in the
237
    // first group (starting from position 0). We are taking group from position
238
    // `capacity` in order to avoid duplicates.
239
240
    // Half-group tables capacity fits into portable group, where
241
    // GroupPortableImpl::MaskFull is more efficient for the
242
    // capacity <= GroupPortableImpl::kWidth.
243
0
    ABSL_SWISSTABLE_ASSERT(cap <= GroupPortableImpl::kWidth &&
244
0
                           "unexpectedly large half-group capacity");
245
0
    static_assert(Group::kWidth >= GroupPortableImpl::kWidth,
246
0
                  "unexpected group width");
247
    // Group starts from kSentinel slot, so indices in the mask will
248
    // be increased by 1.
249
0
    const auto mask = GroupPortableImpl(ctrl + cap).MaskFull();
250
0
    --ctrl;
251
0
    slot = PrevSlot(slot, slot_size);
252
0
    for (uint32_t i : mask) {
253
0
      cb(ctrl + i, SlotAddress(slot, i, slot_size));
254
0
    }
255
0
    return;
256
0
  }
257
0
  size_t remaining = c.size();
258
0
  [[maybe_unused]] const size_t original_size_for_assert = remaining;
259
0
  while (remaining != 0) {
260
0
    for (uint32_t i : GroupFullEmptyOrDeleted(ctrl).MaskFull()) {
261
0
      ABSL_SWISSTABLE_ASSERT(IsFull(ctrl[i]) &&
262
0
                             "hash table was modified unexpectedly");
263
0
      cb(ctrl + i, SlotAddress(slot, i, slot_size));
264
0
      --remaining;
265
0
    }
266
0
    ctrl += Group::kWidth;
267
0
    slot = NextSlot(slot, slot_size, Group::kWidth);
268
0
    ABSL_SWISSTABLE_ASSERT(
269
0
        (remaining == 0 || *(ctrl - 1) != ctrl_t::kSentinel) &&
270
0
        "hash table was modified unexpectedly");
271
0
  }
272
  // NOTE: erasure of the current element is allowed in callback for
273
  // absl::erase_if specialization. So we use `>=`.
274
0
  ABSL_SWISSTABLE_ASSERT(original_size_for_assert >= c.size() &&
275
0
                         "hash table was modified unexpectedly");
276
0
}
Unexecuted instantiation: raw_hash_set.cc:void absl::container_internal::(anonymous namespace)::IterateOverFullSlotsImpl<absl::FunctionRef<void (absl::container_internal::ctrl_t const*, void*)> >(absl::container_internal::CommonFields const&, unsigned long, absl::FunctionRef<void (absl::container_internal::ctrl_t const*, void*)>)
Unexecuted instantiation: raw_hash_set.cc:void absl::container_internal::(anonymous namespace)::IterateOverFullSlotsImpl<absl::container_internal::DestroySlots(absl::container_internal::CommonFields&, unsigned long, void (*)(void*, void*))::$_0>(absl::container_internal::CommonFields const&, unsigned long, absl::container_internal::DestroySlots(absl::container_internal::CommonFields&, unsigned long, void (*)(void*, void*))::$_0)
Unexecuted instantiation: raw_hash_set.cc:void absl::container_internal::(anonymous namespace)::IterateOverFullSlotsImpl<absl::container_internal::Copy(absl::container_internal::CommonFields&, absl::container_internal::PolicyFunctions const&, absl::container_internal::CommonFields const&, absl::FunctionRef<void (void*, void const*)>)::$_0>(absl::container_internal::CommonFields const&, unsigned long, absl::container_internal::Copy(absl::container_internal::CommonFields&, absl::container_internal::PolicyFunctions const&, absl::container_internal::CommonFields const&, absl::FunctionRef<void (void*, void const*)>)::$_0)
277
278
}  // namespace
279
280
613k
void CommonFields::AssertNotDebugCapacityImpl() const {
281
613k
  const HashtableCapacity cap = maybe_invalid_capacity();
282
613k
  if (ABSL_PREDICT_TRUE(cap.IsValid())) {
283
613k
    return;
284
613k
  }
285
613k
  assert(!cap.IsReentrance() &&
286
0
         "Reentrant container access during element construction/destruction "
287
0
         "is not allowed.");
288
0
  if (cap.IsDestroyed()) {
289
0
    ABSL_RAW_LOG(FATAL, "Use of destroyed hash table.");
290
0
  }
291
0
  if (SwisstableGenerationsEnabled() && ABSL_PREDICT_FALSE(cap.IsMovedFrom())) {
292
0
    if (cap.IsSelfMovedFrom()) {
293
      // If this log triggers, then a hash table was move-assigned to itself
294
      // and then used again later without being reinitialized.
295
0
      ABSL_RAW_LOG(FATAL, "Use of self-move-assigned hash table.");
296
0
    }
297
0
    ABSL_RAW_LOG(FATAL, "Use of moved-from hash table.");
298
0
  }
299
0
}
300
301
0
void ConvertDeletedToEmptyAndFullToDeleted(ctrl_t* ctrl, size_t capacity) {
302
0
  ABSL_SWISSTABLE_ASSERT(ctrl[capacity] == ctrl_t::kSentinel);
303
0
  ABSL_SWISSTABLE_ASSERT(IsValidCapacity(capacity));
304
0
  for (ctrl_t* pos = ctrl; pos < ctrl + capacity; pos += Group::kWidth) {
305
0
    Group{pos}.ConvertSpecialToEmptyAndFullToDeleted(pos);
306
0
  }
307
  // Copy the cloned ctrl bytes.
308
0
  std::memcpy(ctrl + capacity + 1, ctrl, NumClonedBytes());
309
0
  ctrl[capacity] = ctrl_t::kSentinel;
310
0
}
311
312
void IterateOverFullSlots(const CommonFields& c, size_t slot_size,
313
0
                          absl::FunctionRef<void(const ctrl_t*, void*)> cb) {
314
0
  IterateOverFullSlotsImpl(c, slot_size, cb);
315
0
}
316
317
void CommonFields::InitGrowthLeftNoDeleted(size_t growth_left,
318
502k
                                           size_t capacity) {
319
502k
  if (capacity <= GrowthInfoLowerBound::kMaxGrowthLeftLowerBound) {
320
463k
    inline_data_.set_growth_info_lower_bound(
321
463k
        GrowthInfoLowerBound(static_cast<uint8_t>(growth_left)));
322
463k
  } else {
323
38.9k
    size_t lower_bound = (std::min)(
324
38.9k
        growth_left,
325
38.9k
        GrowthInfoLowerBound::kMaxGrowthLeftLowerBound);
326
38.9k
    inline_data_.set_growth_info_lower_bound(
327
38.9k
        GrowthInfoLowerBound(static_cast<uint8_t>(lower_bound)));
328
38.9k
    SetGrowthInfoOverflow(growth_left - lower_bound);
329
38.9k
  }
330
502k
}
331
332
177k
size_t CommonFields::GetGrowthLeftTotalSlow(size_t capacity) const {
333
177k
  size_t result = inline_data_.growth_info_lower_bound().GetGrowthLeft();
334
177k
  if (capacity > GrowthInfoLowerBound::kMaxGrowthLeftLowerBound) {
335
17.2k
    result += GetOverflowGrowthLeft();
336
17.2k
  }
337
177k
  return result;
338
177k
}
339
340
0
void CommonFields::OverwriteFullAsEmpty() {
341
0
  if (inline_data_.growth_info_lower_bound().GetGrowthLeft() <
342
0
      GrowthInfoLowerBound::kMaxGrowthLeftLowerBound) {
343
0
    inline_data_.overwrite_full_as_empty_in_lower_bound();
344
0
  } else {
345
0
    SetGrowthInfoOverflow(GetOverflowGrowthLeft() + 1);
346
0
  }
347
0
}
348
349
GrowthInfoLowerBound CommonFields::RebalanceGrowthLeftLowerBound(
350
183k
    size_t capacity) {
351
183k
  auto growth_left_lower_bound = GetGrowthInfoLowerBound();
352
183k
  if (capacity <= GrowthInfoLowerBound::kMaxGrowthLeftLowerBound ||
353
      // For tables with deleted slots, we often call rebalance even if
354
      // we have growth left in the lower bound.
355
159k
      growth_left_lower_bound.HasDeletedAndGrowthLeft()) {
356
159k
    return growth_left_lower_bound;
357
159k
  } else {
358
23.2k
    return RebalanceGrowthLeftLowerBoundLargeCapacity();
359
23.2k
  }
360
183k
}
361
362
ABSL_ATTRIBUTE_NOINLINE GrowthInfoLowerBound
363
23.2k
CommonFields::RebalanceGrowthLeftLowerBoundLargeCapacity() {
364
23.2k
  size_t overflow_growth_left = GetOverflowGrowthLeft();
365
23.2k
  size_t lower_bound_growth_left = GetGrowthLeftLowerBound();
366
23.2k
  size_t overflow_to_lower_bound_size =
367
23.2k
      (std::min)(overflow_growth_left,
368
23.2k
                 GrowthInfoLowerBound::kMaxGrowthLeftLowerBound -
369
23.2k
                     lower_bound_growth_left);
370
23.2k
  SetGrowthInfoOverflow(overflow_growth_left - overflow_to_lower_bound_size);
371
23.2k
  inline_data_.increment_growth_info_lower_bound(overflow_to_lower_bound_size);
372
23.2k
  auto result = GetGrowthInfoLowerBound();
373
23.2k
  ABSL_SWISSTABLE_ASSERT(result.HasNoDeleted() ==
374
23.2k
                         GetGrowthInfoLowerBound().HasNoDeleted());
375
23.2k
  ABSL_SWISSTABLE_ASSERT(
376
23.2k
      (result.GetGrowthLeft() > 0 ||
377
23.2k
       (GetGrowthLeftLowerBound() + GetOverflowGrowthLeft() == 0)) &&
378
23.2k
      "rebalance may return 0 only if we have absolutely no growth left");
379
23.2k
  return result;
380
23.2k
}
381
382
0
HashtablezInfoHandle CommonFields::infoz_ptr() const {
383
  // growth_info is stored before control bytes.
384
0
  ABSL_SWISSTABLE_ASSERT(has_infoz());
385
0
  HashtablezInfoHandle res;
386
0
  void* src = reinterpret_cast<char*>(control()) -
387
0
              MetadataBeforeControlSize(/*has_infoz=*/true, capacity());
388
0
  std::memcpy(&res, src, sizeof(HashtablezInfoHandle));
389
0
  return res;
390
0
}
391
392
0
void CommonFields::set_infoz(HashtablezInfoHandle infoz) {
393
0
  ABSL_SWISSTABLE_ASSERT(has_infoz());
394
0
  void* dst = reinterpret_cast<char*>(control()) -
395
0
              MetadataBeforeControlSize(/*has_infoz=*/true, capacity());
396
0
  std::memcpy(dst, &infoz, sizeof(HashtablezInfoHandle));
397
0
}
398
399
namespace {
400
void DeallocBackingArrayImpl(void* alloc, size_t capacity, ctrl_t* ctrl,
401
                             size_t slot_size, size_t slot_align,
402
                             bool has_infoz, size_t blocked_element_count,
403
267k
                             DeallocBackingArrayFn dealloc) {
404
267k
  RawHashSetLayout layout(capacity, slot_size, slot_align, has_infoz,
405
267k
                          blocked_element_count);
406
267k
  void* backing_array = ctrl - layout.control_offset();
407
  // Unpoison before returning the memory to the allocator.
408
267k
  SanitizerUnpoisonMemoryRegion(backing_array, layout.alloc_size());
409
267k
  dealloc(alloc, backing_array, layout.alloc_size());
410
267k
}
411
412
void DeallocBackingArrayImpl(CommonFields& c,
413
                             const PolicyFunctions& __restrict policy,
414
17.3k
                             void* alloc) {
415
17.3k
  DeallocBackingArrayImpl(alloc, c.capacity(), c.control(), policy.slot_size,
416
17.3k
                          policy.slot_align, c.has_infoz(),
417
17.3k
                          c.blocked_element_count(), policy.dealloc);
418
17.3k
}
419
420
}  // namespace
421
422
void UnregisterAndDeallocBackingArray(CommonFields& c,
423
                                      const DtorPolicy& __restrict policy,
424
                                      DeallocBackingArrayFn dealloc,
425
41.8k
                                      void* alloc) {
426
41.8k
  size_t cap = c.capacity();  // capacity is already in register, so storing it
427
                              // in a local variable before Unregister().
428
41.8k
  c.infoz().Unregister();
429
41.8k
  DeallocBackingArrayImpl(alloc, cap, c.control(), policy.slot_size,
430
41.8k
                          policy.slot_align, c.has_infoz(),
431
41.8k
                          c.blocked_element_count(), dealloc);
432
41.8k
}
433
434
namespace {
435
436
void ResetGrowthLeft(size_t capacity, size_t occupied_elements,
437
445k
                     CommonFields& common) {
438
445k
  common.InitGrowthLeftNoDeleted(CapacityToGrowth(capacity) - occupied_elements,
439
445k
                                 capacity);
440
445k
}
441
442
// Finds guaranteed to exists empty slot from the given position.
443
// NOTE: this function is almost never triggered inside of the
444
// DropDeletesWithoutResize, so we keep it simple.
445
// The table is rather sparse, so empty slot will be found very quickly.
446
0
size_t FindEmptySlot(size_t start, size_t end, const ctrl_t* ctrl) {
447
0
  for (size_t i = start; i < end; ++i) {
448
0
    if (IsEmpty(ctrl[i])) {
449
0
      return i;
450
0
    }
451
0
  }
452
0
  ABSL_UNREACHABLE();
453
0
}
454
455
// Finds guaranteed to exist full slot starting from the given position.
456
// NOTE: this function is only triggered for rehash(0), when we need to
457
// go back to SOO state, so we keep it simple.
458
0
size_t FindFirstFullSlot(size_t start, size_t end, const ctrl_t* ctrl) {
459
0
  for (size_t i = start; i < end; ++i) {
460
0
    if (IsFull(ctrl[i])) {
461
0
      return i;
462
0
    }
463
0
  }
464
0
  ABSL_UNREACHABLE();
465
0
}
466
467
10.9M
void PrepareInsertCommon(CommonFields& common) {
468
10.9M
  common.increment_size();
469
10.9M
  common.maybe_increment_generation_on_insert();
470
10.9M
}
471
472
// Sets sanitizer poisoning for slot corresponding to control byte being set.
473
inline void DoSanitizeOnSetCtrl(const CommonFields& c, size_t i, ctrl_t h,
474
10.9M
                                size_t slot_size) {
475
10.9M
  const size_t cap = c.capacity();
476
10.9M
  ABSL_ASSUME(cap > kMaxSmallCapacity);
477
10.9M
  ABSL_SWISSTABLE_ASSERT(i < cap);
478
10.9M
  auto* slot_i = static_cast<const char*>(c.slot_array(cap)) + i * slot_size;
479
10.9M
  if (IsFull(h)) {
480
10.9M
    SanitizerUnpoisonMemoryRegion(slot_i, slot_size);
481
10.9M
  } else {
482
0
    SanitizerPoisonMemoryRegion(slot_i, slot_size);
483
0
  }
484
10.9M
}
485
486
// Sets `ctrl[i]` to `h`.
487
//
488
// Unlike setting it directly, this function will perform bounds checks and
489
// mirror the value to the cloned tail if necessary.
490
10.6M
inline void SetCtrlNoSanitizeImpl(const CommonFields& c, size_t i, ctrl_t h) {
491
10.6M
  ABSL_SWISSTABLE_ASSERT(i < c.capacity());
492
10.6M
  ctrl_t* ctrl = c.control();
493
10.6M
  const size_t cap = c.capacity();
494
10.6M
  ctrl[i] = h;
495
10.6M
  ctrl[((i - NumClonedBytes()) & cap) + (NumClonedBytes() & cap)] = h;
496
10.6M
}
497
498
inline void SetCtrl(const CommonFields& c, size_t i, ctrl_t h,
499
10.6M
                    size_t slot_size) {
500
10.6M
  ABSL_SWISSTABLE_ASSERT(!c.is_small());
501
10.6M
  DoSanitizeOnSetCtrl(c, i, h, slot_size);
502
10.6M
  SetCtrlNoSanitizeImpl(c, i, h);
503
10.6M
}
504
// Overload for setting to an occupied `h2_t` rather than a special `ctrl_t`.
505
10.6M
inline void SetCtrl(const CommonFields& c, size_t i, h2_t h, size_t slot_size) {
506
10.6M
  SetCtrl(c, i, static_cast<ctrl_t>(h), slot_size);
507
10.6M
}
508
509
// Sets `ctrl[i]` to `ctrl_t::kSentinel`.
510
//
511
// Unlike setting it directly, this function will perform bounds checks and
512
// mirror the value to the cloned tail if necessary.
513
0
inline void BlockCtrl(const CommonFields& c, size_t i) {
514
0
  ABSL_SWISSTABLE_ASSERT(!c.is_small());
515
0
  SetCtrlNoSanitizeImpl(c, i, ctrl_t::kSentinel);
516
0
}
517
518
// Like SetCtrl, but in a single group table, we can save some operations when
519
// setting the cloned control byte.
520
inline void SetCtrlInSingleGroupTable(const CommonFields& c, size_t i, ctrl_t h,
521
69.2k
                                      size_t slot_size) {
522
69.2k
  const size_t cap = c.capacity();
523
69.2k
  ABSL_SWISSTABLE_ASSERT(!c.is_small());
524
69.2k
  ABSL_SWISSTABLE_ASSERT(is_single_group(cap));
525
69.2k
  DoSanitizeOnSetCtrl(c, i, h, slot_size);
526
69.2k
  ctrl_t* ctrl = c.control();
527
69.2k
  ctrl[i] = h;
528
69.2k
  ctrl[i + cap + 1] = h;
529
69.2k
}
530
// Overload for setting to an occupied `h2_t` rather than a special `ctrl_t`.
531
inline void SetCtrlInSingleGroupTable(const CommonFields& c, size_t i, h2_t h,
532
69.2k
                                      size_t slot_size) {
533
69.2k
  SetCtrlInSingleGroupTable(c, i, static_cast<ctrl_t>(h), slot_size);
534
69.2k
}
535
536
// Like SetCtrl, but in a table with capacity >= Group::kWidth - 1,
537
// we can save some operations when setting the cloned control byte.
538
inline void SetCtrlInLargeTable(const CommonFields& c, size_t i, ctrl_t h,
539
226k
                                size_t slot_size) {
540
226k
  ABSL_SWISSTABLE_ASSERT(c.capacity() >= Group::kWidth - 1);
541
226k
  DoSanitizeOnSetCtrl(c, i, h, slot_size);
542
226k
  ctrl_t* ctrl = c.control();
543
226k
  ctrl[i] = h;
544
226k
  ctrl[((i - NumClonedBytes()) & c.capacity()) + NumClonedBytes()] = h;
545
226k
}
546
// Overload for setting to an occupied `h2_t` rather than a special `ctrl_t`.
547
inline void SetCtrlInLargeTable(const CommonFields& c, size_t i, h2_t h,
548
226k
                                size_t slot_size) {
549
226k
  SetCtrlInLargeTable(c, i, static_cast<ctrl_t>(h), slot_size);
550
226k
}
551
552
268k
void BlockControlBytes(CommonFields& common, size_t blocked_element_count) {
553
268k
  const size_t capacity = common.capacity();
554
268k
  while (blocked_element_count > 0) {
555
0
    BlockCtrl(common, capacity - blocked_element_count);
556
0
    --blocked_element_count;
557
0
  }
558
268k
}
559
560
void* DropDeletesWithoutResizeAndPrepareInsert(
561
    CommonFields& common, const PolicyFunctions& __restrict policy,
562
0
    size_t new_hash) {
563
0
  void* set = &common;
564
0
  const size_t capacity = common.capacity();
565
0
  ABSL_SWISSTABLE_ASSERT(IsValidCapacity(capacity));
566
0
  ABSL_SWISSTABLE_ASSERT(!is_single_group(capacity));
567
0
  ABSL_ASSUME(capacity > kMaxSmallCapacity);
568
569
0
  ctrl_t* ctrl = common.control();
570
0
  void* slot_array = common.slot_array(capacity);
571
  // Algorithm:
572
  // - mark all DELETED slots as EMPTY
573
  // - mark all FULL slots as DELETED
574
  // - for each slot marked as DELETED
575
  //     hash = Hash(element)
576
  //     target = find_first_non_full(hash)
577
  //     if target is in the same group
578
  //       mark slot as FULL
579
  //     else if target is EMPTY
580
  //       transfer element to target
581
  //       mark slot as EMPTY
582
  //       mark target as FULL
583
  //     else if target is DELETED
584
  //       swap current element with target element
585
  //       mark target as FULL
586
  //       repeat procedure for current slot with moved from element (target)
587
0
  const size_t blocked_element_count = common.blocked_element_count();
588
0
  ConvertDeletedToEmptyAndFullToDeleted(ctrl, capacity);
589
0
  BlockControlBytes(common, blocked_element_count);
590
0
  const void* hash_fn = policy.hash_fn(common);
591
0
  auto hasher = policy.hash_slot;
592
0
  auto transfer_n = policy.transfer_n;
593
0
  const size_t slot_size = policy.slot_size;
594
595
0
  size_t total_probe_length = 0;
596
0
  void* slot_ptr = SlotAddress(slot_array, 0, slot_size);
597
598
  // The index of an empty slot that can be used as temporary memory for
599
  // the swap operation.
600
0
  constexpr size_t kUnknownId = ~size_t{};
601
0
  size_t tmp_space_id = kUnknownId;
602
603
0
  for (size_t i = 0; i != capacity;
604
0
       ++i, slot_ptr = NextSlot(slot_ptr, slot_size)) {
605
0
    ABSL_SWISSTABLE_ASSERT(slot_ptr == SlotAddress(slot_array, i, slot_size));
606
0
    if (IsEmpty(ctrl[i])) {
607
0
      tmp_space_id = i;
608
0
      continue;
609
0
    }
610
0
    if (!IsDeleted(ctrl[i])) continue;
611
0
    const size_t hash = (*hasher)(hash_fn, slot_ptr, common.seed().seed());
612
0
    const FindInfo target = find_first_non_full(common, hash);
613
0
    const size_t new_i = target.offset;
614
0
    total_probe_length += target.probe_length;
615
616
    // Verify if the old and new i fall within the same group wrt the hash.
617
    // If they do, we don't need to move the object as it falls already in the
618
    // best probe we can.
619
0
    const size_t probe_offset = probe(common, hash).offset();
620
0
    const h2_t h2 = H2(hash);
621
0
    const auto probe_index = [probe_offset, capacity](size_t pos) {
622
0
      return ((pos - probe_offset) & capacity) / Group::kWidth;
623
0
    };
624
625
    // Element doesn't move.
626
0
    if (ABSL_PREDICT_TRUE(probe_index(new_i) == probe_index(i))) {
627
0
      SetCtrlInLargeTable(common, i, h2, slot_size);
628
0
      continue;
629
0
    }
630
631
0
    void* new_slot_ptr = SlotAddress(slot_array, new_i, slot_size);
632
0
    if (IsEmpty(ctrl[new_i])) {
633
      // Transfer element to the empty spot.
634
      // SetCtrl poisons/unpoisons the slots so we have to call it at the
635
      // right time.
636
0
      SetCtrlInLargeTable(common, new_i, h2, slot_size);
637
0
      (*transfer_n)(set, new_slot_ptr, slot_ptr, 1);
638
0
      SetCtrlInLargeTable(common, i, ctrl_t::kEmpty, slot_size);
639
      // Initialize or change empty space id.
640
0
      tmp_space_id = i;
641
0
    } else {
642
0
      ABSL_SWISSTABLE_ASSERT(IsDeleted(ctrl[new_i]));
643
0
      SetCtrlInLargeTable(common, new_i, h2, slot_size);
644
      // Until we are done rehashing, DELETED marks previously FULL slots.
645
646
0
      if (tmp_space_id == kUnknownId) {
647
0
        tmp_space_id = FindEmptySlot(i + 1, capacity, ctrl);
648
0
      }
649
0
      void* tmp_space = SlotAddress(slot_array, tmp_space_id, slot_size);
650
0
      SanitizerUnpoisonMemoryRegion(tmp_space, slot_size);
651
652
      // Swap i and new_i elements.
653
0
      (*transfer_n)(set, tmp_space, new_slot_ptr, 1);
654
0
      (*transfer_n)(set, new_slot_ptr, slot_ptr, 1);
655
0
      (*transfer_n)(set, slot_ptr, tmp_space, 1);
656
657
0
      SanitizerPoisonMemoryRegion(tmp_space, slot_size);
658
659
      // repeat the processing of the ith slot
660
0
      --i;
661
0
      slot_ptr = PrevSlot(slot_ptr, slot_size);
662
0
    }
663
0
  }
664
  // Prepare insert for the new element.
665
0
  PrepareInsertCommon(common);
666
0
  ResetGrowthLeft(capacity, common.size() + blocked_element_count, common);
667
0
  FindInfo find_info = find_first_non_full(common, new_hash);
668
0
  SetCtrlInLargeTable(common, find_info.offset, H2(new_hash), slot_size);
669
0
  common.infoz().RecordInsertMiss(new_hash, find_info.probe_length);
670
0
  common.infoz().RecordRehash(total_probe_length);
671
0
  return SlotAddress(slot_array, find_info.offset, slot_size);
672
0
}
673
674
0
bool WasNeverFull(CommonFields& c, size_t index) {
675
0
  if (is_single_group(c.capacity())) {
676
0
    return true;
677
0
  }
678
0
  const size_t index_before = (index - Group::kWidth) & c.capacity();
679
0
  const auto empty_after = Group(c.control() + index).MaskEmpty();
680
0
  const auto empty_before = Group(c.control() + index_before).MaskEmpty();
681
682
  // We count how many consecutive non empties we have to the right and to the
683
  // left of `it`. If the sum is >= kWidth then there is at least one probe
684
  // window that might have seen a full group.
685
0
  return empty_before && empty_after &&
686
0
         static_cast<size_t>(empty_after.TrailingZeros()) +
687
0
                 empty_before.LeadingZeros() <
688
0
             Group::kWidth;
689
0
}
690
691
// Updates the control bytes to indicate a completely empty table such that all
692
// control bytes are kEmpty except for the kSentinel bytes.
693
// If the table has blocked elements, last `blocked_element_count` are set to
694
// kSentinel.
695
void ResetCtrl(CommonFields& common, size_t slot_size,
696
268k
               size_t blocked_element_count) {
697
268k
  ABSL_SWISSTABLE_ASSERT(IsCapacityValidForBlockedElements(common.capacity()) ||
698
268k
                         blocked_element_count == 0);
699
268k
  const size_t capacity = common.capacity();
700
268k
  ctrl_t* ctrl = common.control();
701
268k
  static constexpr size_t kTwoGroupCapacity = 2 * Group::kWidth - 1;
702
268k
  if (ABSL_PREDICT_TRUE(capacity <= kTwoGroupCapacity)) {
703
162k
    if (IsSmallCapacity(capacity)) return;
704
162k
    std::memset(ctrl, static_cast<int8_t>(ctrl_t::kEmpty), Group::kWidth);
705
162k
    std::memset(ctrl + capacity, static_cast<int8_t>(ctrl_t::kEmpty),
706
162k
                Group::kWidth);
707
162k
    if (capacity == kTwoGroupCapacity) {
708
37.7k
      std::memset(ctrl + Group::kWidth, static_cast<int8_t>(ctrl_t::kEmpty),
709
37.7k
                  Group::kWidth);
710
37.7k
    }
711
162k
  } else {
712
106k
    std::memset(ctrl, static_cast<int8_t>(ctrl_t::kEmpty),
713
106k
                capacity + 1 + NumClonedBytes());
714
106k
  }
715
268k
  ctrl[capacity] = ctrl_t::kSentinel;
716
268k
  SanitizerPoisonMemoryRegion(common.slot_array(capacity),
717
268k
                              slot_size * (capacity - blocked_element_count));
718
268k
  BlockControlBytes(common, blocked_element_count);
719
268k
}
720
721
// Initializes control bytes for growing from capacity 1 to 3.
722
// `orig_h2` is placed in the position `SooSlotIndex()`.
723
// `new_h2` is placed in the position `new_offset`.
724
ABSL_ATTRIBUTE_ALWAYS_INLINE inline void InitializeThreeElementsControlBytes(
725
56.6k
    h2_t orig_h2, h2_t new_h2, size_t new_offset, ctrl_t* new_ctrl) {
726
56.6k
  static constexpr size_t kNewCapacity = NextCapacity(SooCapacity());
727
56.6k
  static_assert(kNewCapacity == 3);
728
56.6k
  static_assert(is_single_group(kNewCapacity));
729
56.6k
  static_assert(SooSlotIndex() == 1);
730
56.6k
  ABSL_SWISSTABLE_ASSERT(new_offset == 0 || new_offset == 2);
731
732
56.6k
  static constexpr uint64_t kEmptyXorSentinel =
733
56.6k
      static_cast<uint8_t>(ctrl_t::kEmpty) ^
734
56.6k
      static_cast<uint8_t>(ctrl_t::kSentinel);
735
56.6k
  static constexpr uint64_t kEmpty64 = static_cast<uint8_t>(ctrl_t::kEmpty);
736
56.6k
  static constexpr size_t kMirroredSooSlotIndex =
737
56.6k
      SooSlotIndex() + kNewCapacity + 1;
738
  // The first 8 bytes, where SOO slot original and mirrored positions are
739
  // replaced with 0.
740
  // Result will look like: E0ESE0EE
741
56.6k
  static constexpr uint64_t kFirstCtrlBytesWithZeroes =
742
56.6k
      k8EmptyBytes ^ (kEmpty64 << (8 * SooSlotIndex())) ^
743
56.6k
      (kEmptyXorSentinel << (8 * kNewCapacity)) ^
744
56.6k
      (kEmpty64 << (8 * kMirroredSooSlotIndex));
745
746
56.6k
  const uint64_t soo_h2 = static_cast<uint64_t>(orig_h2);
747
56.6k
  const uint64_t new_h2_xor_empty =
748
56.6k
      static_cast<uint64_t>(new_h2 ^ static_cast<uint8_t>(ctrl_t::kEmpty));
749
  // Fill the original and mirrored bytes for SOO slot.
750
  // Result will look like:
751
  // EHESEHEE
752
  // Where H = soo_h2, E = kEmpty, S = kSentinel.
753
56.6k
  uint64_t first_ctrl_bytes =
754
56.6k
      ((soo_h2 << (8 * SooSlotIndex())) | kFirstCtrlBytesWithZeroes) |
755
56.6k
      (soo_h2 << (8 * kMirroredSooSlotIndex));
756
  // Replace original and mirrored empty bytes for the new position.
757
  // Result for new_offset 0 will look like:
758
  // NHESNHEE
759
  // Where H = soo_h2, N = H2(new_hash), E = kEmpty, S = kSentinel.
760
  // Result for new_offset 2 will look like:
761
  // EHNSEHNE
762
56.6k
  first_ctrl_bytes ^= (new_h2_xor_empty << (8 * new_offset));
763
56.6k
  size_t new_mirrored_offset = new_offset + kNewCapacity + 1;
764
56.6k
  first_ctrl_bytes ^= (new_h2_xor_empty << (8 * new_mirrored_offset));
765
766
  // Fill last bytes with kEmpty.
767
56.6k
  std::memset(new_ctrl + kNewCapacity, static_cast<int8_t>(ctrl_t::kEmpty),
768
56.6k
              Group::kWidth);
769
  // Overwrite the first 8 bytes with first_ctrl_bytes.
770
56.6k
  absl::little_endian::Store64(new_ctrl, first_ctrl_bytes);
771
772
  // Example for group size 16:
773
  // new_ctrl after 1st memset =      ???EEEEEEEEEEEEEEEE
774
  // new_offset 0:
775
  // new_ctrl after 2nd store  =      NHESNHEEEEEEEEEEEEE
776
  // new_offset 2:
777
  // new_ctrl after 2nd store  =      EHNSEHNEEEEEEEEEEEE
778
779
  // Example for group size 8:
780
  // new_ctrl after 1st memset =      ???EEEEEEEE
781
  // new_offset 0:
782
  // new_ctrl after 2nd store  =      NHESNHEEEEE
783
  // new_offset 2:
784
  // new_ctrl after 2nd store  =      EHNSEHNEEEE
785
56.6k
}
786
787
// ClearBackingArrayNoReuse clears the backing array and sets the common
788
// fields to the default values for empty non-allocated tables.
789
// REQUIRES: c.capacity > policy.soo_capacity.
790
void ClearBackingArrayNoReuse(CommonFields& c,
791
                              const PolicyFunctions& __restrict policy,
792
17.3k
                              void* alloc) {
793
17.3k
  ABSL_SWISSTABLE_ASSERT(c.capacity() > policy.soo_capacity());
794
  // We need to record infoz before calling dealloc, which will unregister
795
  // infoz.
796
17.3k
  c.infoz().RecordClearedReservation();
797
17.3k
  c.infoz().RecordStorageChanged(0, policy.soo_capacity());
798
17.3k
  c.infoz().Unregister();
799
17.3k
  DeallocBackingArrayImpl(c, policy, alloc);
800
17.3k
  c = policy.soo_enabled ? CommonFields{soo_tag_t{}}
801
17.3k
                         : CommonFields{non_soo_tag_t{}};
802
17.3k
}
803
804
template <bool kSooEnabled>
805
135k
void* SingleSlotAddress(CommonFields& c) {
806
135k
  return kSooEnabled ? c.soo_data() : c.slot_array(/*capacity=*/1);
807
135k
}
raw_hash_set.cc:void* absl::container_internal::(anonymous namespace)::SingleSlotAddress<false>(absl::container_internal::CommonFields&)
Line
Count
Source
805
135k
void* SingleSlotAddress(CommonFields& c) {
806
135k
  return kSooEnabled ? c.soo_data() : c.slot_array(/*capacity=*/1);
807
135k
}
Unexecuted instantiation: raw_hash_set.cc:void* absl::container_internal::(anonymous namespace)::SingleSlotAddress<true>(absl::container_internal::CommonFields&)
808
809
template <bool kSooEnabled>
810
148k
void DecrementSmallSize(CommonFields& c) {
811
148k
  if constexpr (kSooEnabled) {
812
13.1k
    c.set_empty_soo();
813
135k
  } else {
814
135k
    c.decrement_size();
815
135k
  }
816
148k
}
raw_hash_set.cc:void absl::container_internal::(anonymous namespace)::DecrementSmallSize<true>(absl::container_internal::CommonFields&)
Line
Count
Source
810
13.1k
void DecrementSmallSize(CommonFields& c) {
811
13.1k
  if constexpr (kSooEnabled) {
812
13.1k
    c.set_empty_soo();
813
  } else {
814
    c.decrement_size();
815
  }
816
13.1k
}
raw_hash_set.cc:void absl::container_internal::(anonymous namespace)::DecrementSmallSize<false>(absl::container_internal::CommonFields&)
Line
Count
Source
810
135k
void DecrementSmallSize(CommonFields& c) {
811
  if constexpr (kSooEnabled) {
812
    c.set_empty_soo();
813
135k
  } else {
814
135k
    c.decrement_size();
815
135k
  }
816
135k
}
817
818
void DestructSoo(CommonFields& c, const DtorPolicy& __restrict policy,
819
24.9k
                 DeallocBackingArrayFn dealloc, void* alloc) {
820
24.9k
  ABSL_SWISSTABLE_ASSERT(!c.is_small() || !c.empty());
821
24.9k
  if (c.is_small()) {
822
0
    ABSL_SWISSTABLE_ASSERT(policy.destroy_slot != nullptr);
823
0
    policy.destroy_slot(&c, c.soo_data());
824
0
    return;
825
0
  }
826
24.9k
  if (policy.destroy_slot != nullptr) {
827
0
    DestroySlots(c, policy.slot_size, policy.destroy_slot);
828
0
  }
829
24.9k
  UnregisterAndDeallocBackingArray(c, policy, dealloc, alloc);
830
24.9k
}
831
832
void DestructNonSoo(CommonFields& c, const DtorPolicy& __restrict policy,
833
16.9k
                    DeallocBackingArrayFn dealloc, void* alloc) {
834
16.9k
  ABSL_SWISSTABLE_ASSERT(c.capacity() > 0);
835
16.9k
  if (policy.destroy_slot != nullptr) {
836
0
    if (c.is_small()) {
837
0
      if (!c.empty()) {
838
0
        static_assert(kMaxSmallCapacity == 1);
839
0
        policy.destroy_slot(&c, c.slot_array(/*capacity=*/1));
840
0
      }
841
0
    } else {
842
0
      DestroySlots(c, policy.slot_size, policy.destroy_slot);
843
0
    }
844
0
  }
845
16.9k
  UnregisterAndDeallocBackingArray(c, policy, dealloc, alloc);
846
16.9k
}
847
848
}  // namespace
849
850
0
void EraseMetaOnlySmall(CommonFields& c, bool soo_enabled, size_t slot_size) {
851
0
  ABSL_SWISSTABLE_ASSERT(c.is_small());
852
0
  if (soo_enabled) {
853
0
    c.set_empty_soo();
854
0
    return;
855
0
  }
856
0
  c.decrement_size();
857
0
  c.infoz().RecordErase();
858
0
  SanitizerPoisonMemoryRegion(SingleSlotAddress</*kSooEnabled=*/false>(c),
859
0
                              slot_size);
860
0
}
861
862
0
void EraseMetaOnlyLarge(CommonFields& c, size_t index, size_t slot_size) {
863
0
  ABSL_SWISSTABLE_ASSERT(!c.is_small());
864
0
  ABSL_SWISSTABLE_ASSERT(IsFull(c.control()[index]) &&
865
0
                         "erasing a dangling iterator");
866
0
  c.decrement_size();
867
0
  c.infoz().RecordErase();
868
869
0
  if (WasNeverFull(c, index)) {
870
0
    SetCtrl(c, index, ctrl_t::kEmpty, slot_size);
871
0
    c.OverwriteFullAsEmpty();
872
0
    return;
873
0
  }
874
875
0
  c.OverwriteFullAsDeleted();
876
0
  SetCtrlInLargeTable(c, index, ctrl_t::kDeleted, slot_size);
877
0
}
878
879
void ClearBackingArray(CommonFields& c,
880
                       const PolicyFunctions& __restrict policy, void* alloc,
881
285k
                       bool reuse) {
882
285k
  ABSL_SWISSTABLE_ASSERT(c.capacity() > kMaxSmallCapacity);
883
285k
  if (reuse) {
884
268k
    const size_t blocked_element_count = c.blocked_element_count();
885
268k
    c.set_size_to_zero();
886
268k
    ABSL_SWISSTABLE_ASSERT(c.capacity() > policy.soo_capacity());
887
268k
    ResetCtrl(c, policy.slot_size, blocked_element_count);
888
268k
    ResetGrowthLeft(c.capacity(), blocked_element_count, c);
889
268k
    ABSL_SWISSTABLE_ASSERT(c.blocked_element_count() == blocked_element_count);
890
268k
    c.infoz().RecordStorageChanged(0, c.capacity());
891
268k
  } else {
892
17.3k
    ClearBackingArrayNoReuse(c, policy, alloc);
893
17.3k
  }
894
285k
}
895
896
void DestroySlots(CommonFields& c, size_t slot_size,
897
0
                  DestroySlotFn destroy_slot) {
898
0
  ABSL_SWISSTABLE_ASSERT(!c.is_small());
899
0
  ABSL_SWISSTABLE_ASSERT(destroy_slot != nullptr);
900
0
  auto destroy_slot_wrapper = [&](const ctrl_t*, void* slot) {
901
0
    destroy_slot(&c, slot);
902
0
  };
903
0
  if constexpr (SwisstableGenerationsOrDebugEnabled()) {
904
0
    CommonFields common_copy(non_soo_tag_t{}, c);
905
0
    c.set_capacity(HashtableCapacity::CreateDestroyed());
906
0
    IterateOverFullSlotsImpl(common_copy, slot_size, destroy_slot_wrapper);
907
0
    c.set_capacity(common_copy.capacity());
908
  } else {
909
    IterateOverFullSlotsImpl(c, slot_size, destroy_slot_wrapper);
910
  }
911
0
}
912
913
template <bool kSooEnabled>
914
void Clear(CommonFields& c, const PolicyFunctions& __restrict policy,
915
613k
           DestroySlotFn destroy_slot, void* alloc) {
916
613k
  if (SwisstableGenerationsEnabled() &&
917
0
      c.maybe_invalid_capacity().IsMovedFrom()) {
918
0
    c.set_capacity(policy.soo_capacity());
919
0
  }
920
613k
  c.AssertNotDebugCapacity();
921
613k
  const size_t cap = c.capacity();
922
613k
  if constexpr (kSooEnabled) {
923
37.6k
    ABSL_ASSUME(cap > 0);
924
37.6k
  }
925
613k
  if (c.is_small()) {
926
327k
    if (!c.empty()) {
927
148k
      if (destroy_slot != nullptr) {
928
0
        destroy_slot(&c, SingleSlotAddress<kSooEnabled>(c));
929
0
      }
930
148k
      DecrementSmallSize<kSooEnabled>(c);
931
148k
      c.infoz().RecordStorageChanged(0, cap);
932
148k
    }
933
327k
  } else {
934
285k
    if (destroy_slot != nullptr) {
935
0
      DestroySlots(c, policy.slot_size, destroy_slot);
936
0
    }
937
    // Iterating over this container is O(bucket_count()). When bucket_count()
938
    // is much greater than size(), iteration becomes prohibitively expensive.
939
    // For clear() it is more important to reuse the allocated array when the
940
    // container is small because allocation takes comparatively long time
941
    // compared to destruction of the elements of the container. So we pick the
942
    // largest bucket_count() threshold for which iteration is still fast and
943
    // past that we simply deallocate the array.
944
285k
    ClearBackingArray(c, policy, alloc, /*reuse=*/cap < 128);
945
285k
  }
946
613k
  c.set_reserved_growth(0);
947
613k
  c.set_reservation_size(0);
948
613k
}
void absl::container_internal::Clear<true>(absl::container_internal::CommonFields&, absl::container_internal::PolicyFunctions const&, void (*)(void*, void*), void*)
Line
Count
Source
915
37.6k
           DestroySlotFn destroy_slot, void* alloc) {
916
37.6k
  if (SwisstableGenerationsEnabled() &&
917
0
      c.maybe_invalid_capacity().IsMovedFrom()) {
918
0
    c.set_capacity(policy.soo_capacity());
919
0
  }
920
37.6k
  c.AssertNotDebugCapacity();
921
37.6k
  const size_t cap = c.capacity();
922
37.6k
  if constexpr (kSooEnabled) {
923
37.6k
    ABSL_ASSUME(cap > 0);
924
37.6k
  }
925
37.6k
  if (c.is_small()) {
926
13.7k
    if (!c.empty()) {
927
13.1k
      if (destroy_slot != nullptr) {
928
0
        destroy_slot(&c, SingleSlotAddress<kSooEnabled>(c));
929
0
      }
930
13.1k
      DecrementSmallSize<kSooEnabled>(c);
931
13.1k
      c.infoz().RecordStorageChanged(0, cap);
932
13.1k
    }
933
23.8k
  } else {
934
23.8k
    if (destroy_slot != nullptr) {
935
0
      DestroySlots(c, policy.slot_size, destroy_slot);
936
0
    }
937
    // Iterating over this container is O(bucket_count()). When bucket_count()
938
    // is much greater than size(), iteration becomes prohibitively expensive.
939
    // For clear() it is more important to reuse the allocated array when the
940
    // container is small because allocation takes comparatively long time
941
    // compared to destruction of the elements of the container. So we pick the
942
    // largest bucket_count() threshold for which iteration is still fast and
943
    // past that we simply deallocate the array.
944
23.8k
    ClearBackingArray(c, policy, alloc, /*reuse=*/cap < 128);
945
23.8k
  }
946
37.6k
  c.set_reserved_growth(0);
947
37.6k
  c.set_reservation_size(0);
948
37.6k
}
void absl::container_internal::Clear<false>(absl::container_internal::CommonFields&, absl::container_internal::PolicyFunctions const&, void (*)(void*, void*), void*)
Line
Count
Source
915
576k
           DestroySlotFn destroy_slot, void* alloc) {
916
576k
  if (SwisstableGenerationsEnabled() &&
917
0
      c.maybe_invalid_capacity().IsMovedFrom()) {
918
0
    c.set_capacity(policy.soo_capacity());
919
0
  }
920
576k
  c.AssertNotDebugCapacity();
921
576k
  const size_t cap = c.capacity();
922
  if constexpr (kSooEnabled) {
923
    ABSL_ASSUME(cap > 0);
924
  }
925
576k
  if (c.is_small()) {
926
313k
    if (!c.empty()) {
927
135k
      if (destroy_slot != nullptr) {
928
0
        destroy_slot(&c, SingleSlotAddress<kSooEnabled>(c));
929
0
      }
930
135k
      DecrementSmallSize<kSooEnabled>(c);
931
135k
      c.infoz().RecordStorageChanged(0, cap);
932
135k
    }
933
313k
  } else {
934
262k
    if (destroy_slot != nullptr) {
935
0
      DestroySlots(c, policy.slot_size, destroy_slot);
936
0
    }
937
    // Iterating over this container is O(bucket_count()). When bucket_count()
938
    // is much greater than size(), iteration becomes prohibitively expensive.
939
    // For clear() it is more important to reuse the allocated array when the
940
    // container is small because allocation takes comparatively long time
941
    // compared to destruction of the elements of the container. So we pick the
942
    // largest bucket_count() threshold for which iteration is still fast and
943
    // past that we simply deallocate the array.
944
262k
    ClearBackingArray(c, policy, alloc, /*reuse=*/cap < 128);
945
262k
  }
946
576k
  c.set_reserved_growth(0);
947
576k
  c.set_reservation_size(0);
948
576k
}
949
950
template <bool kSooEnabled>
951
void Destruct(CommonFields& c, const DtorPolicy& __restrict policy,
952
41.8k
              DeallocBackingArrayFn dealloc, void* alloc) {
953
41.8k
  if constexpr (kSooEnabled) {
954
24.9k
    DestructSoo(c, policy, dealloc, alloc);
955
24.9k
  } else {
956
16.9k
    DestructNonSoo(c, policy, dealloc, alloc);
957
16.9k
  }
958
41.8k
}
void absl::container_internal::Destruct<true>(absl::container_internal::CommonFields&, absl::container_internal::DtorPolicy const&, void (*)(void*, void*, unsigned long), void*)
Line
Count
Source
952
24.9k
              DeallocBackingArrayFn dealloc, void* alloc) {
953
24.9k
  if constexpr (kSooEnabled) {
954
24.9k
    DestructSoo(c, policy, dealloc, alloc);
955
  } else {
956
    DestructNonSoo(c, policy, dealloc, alloc);
957
  }
958
24.9k
}
void absl::container_internal::Destruct<false>(absl::container_internal::CommonFields&, absl::container_internal::DtorPolicy const&, void (*)(void*, void*, unsigned long), void*)
Line
Count
Source
952
16.9k
              DeallocBackingArrayFn dealloc, void* alloc) {
953
  if constexpr (kSooEnabled) {
954
    DestructSoo(c, policy, dealloc, alloc);
955
16.9k
  } else {
956
16.9k
    DestructNonSoo(c, policy, dealloc, alloc);
957
16.9k
  }
958
16.9k
}
959
template <bool kSooEnabled>
960
void Destruct(CommonFields& c, const DtorPolicy& __restrict policy,
961
41.8k
              DeallocBackingArrayFn dealloc) {
962
41.8k
  Destruct<kSooEnabled>(c, policy, dealloc, /*alloc=*/&c);
963
41.8k
}
void absl::container_internal::Destruct<true>(absl::container_internal::CommonFields&, absl::container_internal::DtorPolicy const&, void (*)(void*, void*, unsigned long))
Line
Count
Source
961
24.9k
              DeallocBackingArrayFn dealloc) {
962
24.9k
  Destruct<kSooEnabled>(c, policy, dealloc, /*alloc=*/&c);
963
24.9k
}
void absl::container_internal::Destruct<false>(absl::container_internal::CommonFields&, absl::container_internal::DtorPolicy const&, void (*)(void*, void*, unsigned long))
Line
Count
Source
961
16.9k
              DeallocBackingArrayFn dealloc) {
962
16.9k
  Destruct<kSooEnabled>(c, policy, dealloc, /*alloc=*/&c);
963
16.9k
}
964
template <bool kSooEnabled>
965
41.8k
void Destruct(CommonFields& c, const DtorPolicy& __restrict policy) {
966
41.8k
  Destruct<kSooEnabled>(c, policy, kStandardDeallocBackingArrayFn);
967
41.8k
}
void absl::container_internal::Destruct<true>(absl::container_internal::CommonFields&, absl::container_internal::DtorPolicy const&)
Line
Count
Source
965
24.9k
void Destruct(CommonFields& c, const DtorPolicy& __restrict policy) {
966
24.9k
  Destruct<kSooEnabled>(c, policy, kStandardDeallocBackingArrayFn);
967
24.9k
}
void absl::container_internal::Destruct<false>(absl::container_internal::CommonFields&, absl::container_internal::DtorPolicy const&)
Line
Count
Source
965
16.9k
void Destruct(CommonFields& c, const DtorPolicy& __restrict policy) {
966
16.9k
  Destruct<kSooEnabled>(c, policy, kStandardDeallocBackingArrayFn);
967
16.9k
}
968
969
namespace {
970
971
// Iterates over full slots in old table, finds new positions for them and
972
// transfers the slots.
973
// This function is used for reserving or rehashing non-empty tables.
974
// This use case is rare so the function is type erased.
975
// Returns the total probe length.
976
size_t FindNewPositionsAndTransferSlots(
977
    CommonFields& common, const PolicyFunctions& __restrict policy,
978
0
    ctrl_t* old_ctrl, void* old_slots, size_t old_capacity) {
979
0
  void* new_slots = common.slot_array(common.capacity());
980
0
  const void* hash_fn = policy.hash_fn(common);
981
0
  const size_t slot_size = policy.slot_size;
982
0
  const size_t seed = common.seed().seed();
983
984
0
  const auto insert_slot = [&](void* slot) {
985
0
    size_t hash = policy.hash_slot(hash_fn, slot, seed);
986
0
    FindInfo target;
987
0
    if (common.is_small()) {
988
0
      target = FindInfo{0, 0};
989
0
    } else {
990
0
      target = find_first_non_full(common, hash);
991
0
      SetCtrl(common, target.offset, H2(hash), slot_size);
992
0
    }
993
0
    policy.transfer_n(&common, SlotAddress(new_slots, target.offset, slot_size),
994
0
                      slot, 1);
995
0
    return target.probe_length;
996
0
  };
997
0
  if (IsSmallCapacity(old_capacity)) {
998
0
    if (common.size() == 1) insert_slot(old_slots);
999
0
    return 0;
1000
0
  }
1001
0
  size_t total_probe_length = 0;
1002
0
  for (size_t i = 0; i < old_capacity; ++i) {
1003
0
    if (IsFull(old_ctrl[i])) {
1004
0
      total_probe_length += insert_slot(old_slots);
1005
0
    }
1006
0
    old_slots = NextSlot(old_slots, slot_size);
1007
0
  }
1008
0
  return total_probe_length;
1009
0
}
1010
1011
void ReportGrowthToInfozImpl(CommonFields& common, HashtablezInfoHandle infoz,
1012
                             size_t hash, size_t total_probe_length,
1013
0
                             size_t distance_from_desired) {
1014
0
  ABSL_SWISSTABLE_ASSERT(infoz.IsSampled());
1015
0
  infoz.RecordStorageChanged(common.size() - 1, common.capacity());
1016
0
  infoz.RecordRehash(total_probe_length);
1017
0
  infoz.RecordInsertMiss(hash, distance_from_desired);
1018
0
  common.set_has_infoz();
1019
0
  common.set_infoz(infoz);
1020
0
}
1021
1022
// Specialization to avoid passing two 0s from hot function.
1023
ABSL_ATTRIBUTE_NOINLINE void ReportSingleGroupTableGrowthToInfoz(
1024
0
    CommonFields& common, HashtablezInfoHandle infoz, size_t hash) {
1025
0
  ReportGrowthToInfozImpl(common, infoz, hash, /*total_probe_length=*/0,
1026
0
                          /*distance_from_desired=*/0);
1027
0
}
1028
1029
ABSL_ATTRIBUTE_NOINLINE void ReportGrowthToInfoz(CommonFields& common,
1030
                                                 HashtablezInfoHandle infoz,
1031
                                                 size_t hash,
1032
                                                 size_t total_probe_length,
1033
0
                                                 size_t distance_from_desired) {
1034
0
  ReportGrowthToInfozImpl(common, infoz, hash, total_probe_length,
1035
0
                          distance_from_desired);
1036
0
}
1037
1038
ABSL_ATTRIBUTE_NOINLINE void ReportResizeToInfoz(CommonFields& common,
1039
                                                 HashtablezInfoHandle infoz,
1040
0
                                                 size_t total_probe_length) {
1041
0
  ABSL_SWISSTABLE_ASSERT(infoz.IsSampled());
1042
0
  infoz.RecordStorageChanged(common.size(), common.capacity());
1043
0
  infoz.RecordRehash(total_probe_length);
1044
0
  common.set_has_infoz();
1045
0
  common.set_infoz(infoz);
1046
0
}
1047
1048
struct BackingArrayPtrs {
1049
  ctrl_t* ctrl;
1050
  void* slots;
1051
};
1052
1053
BackingArrayPtrs AllocBackingArray(CommonFields& common,
1054
                                   const PolicyFunctions& __restrict policy,
1055
                                   size_t new_capacity, bool has_infoz,
1056
267k
                                   void* alloc, size_t blocked_element_count) {
1057
267k
  RawHashSetLayout layout(new_capacity, policy.slot_size, policy.slot_align,
1058
267k
                          has_infoz, blocked_element_count);
1059
  // Perform a direct call in the common case to allow for profile-guided
1060
  // heap optimization (PGHO) to understand which allocation function is used.
1061
267k
  constexpr size_t kDefaultAlignment = BackingArrayAlignment(alignof(size_t));
1062
267k
  char* mem = static_cast<char*>(
1063
267k
      ABSL_PREDICT_TRUE(
1064
267k
          policy.alloc ==
1065
267k
          (&AllocateBackingArray<kDefaultAlignment, std::allocator<char>>))
1066
267k
          ? AllocateBackingArray<kDefaultAlignment, std::allocator<char>>(
1067
267k
                alloc, layout.alloc_size())
1068
267k
          : policy.alloc(alloc, layout.alloc_size()));
1069
267k
  const GenerationType old_generation = common.generation();
1070
267k
  common.set_generation_ptr(
1071
267k
      reinterpret_cast<GenerationType*>(mem + layout.generation_offset()));
1072
267k
  common.set_generation(NextGeneration(old_generation));
1073
1074
267k
  return {reinterpret_cast<ctrl_t*>(mem + layout.control_offset()),
1075
267k
          mem + layout.slot_offset()};
1076
267k
}
1077
1078
void ResizeEmptyNonAllocatedTableImpl(CommonFields& common,
1079
                                      const PolicyFunctions& __restrict policy,
1080
                                      size_t new_capacity,
1081
                                      size_t blocked_element_count,
1082
0
                                      bool force_infoz) {
1083
0
  ABSL_SWISSTABLE_ASSERT(IsValidCapacity(new_capacity));
1084
0
  ABSL_SWISSTABLE_ASSERT(new_capacity > policy.soo_capacity());
1085
0
  ABSL_SWISSTABLE_ASSERT(!force_infoz || policy.soo_enabled);
1086
0
  ABSL_SWISSTABLE_ASSERT(common.capacity() == policy.soo_capacity());
1087
0
  ABSL_SWISSTABLE_ASSERT(common.empty());
1088
0
  const size_t slot_size = policy.slot_size;
1089
0
  HashtablezInfoHandle infoz;
1090
0
  const bool should_sample =
1091
0
      policy.is_hashtablez_eligible && (force_infoz || ShouldSampleNextTable());
1092
0
  if (ABSL_PREDICT_FALSE(should_sample)) {
1093
0
    infoz = ForcedTrySample(slot_size, policy.key_size, policy.value_size,
1094
0
                            policy.soo_capacity());
1095
0
  }
1096
0
  const bool has_infoz = infoz.IsSampled();
1097
0
  void* alloc = policy.get_char_alloc(common);
1098
1099
0
  common.set_capacity(new_capacity);
1100
0
  common.init_blocked_element_count(blocked_element_count);
1101
0
  const auto [new_ctrl, new_slots] = AllocBackingArray(
1102
0
      common, policy, new_capacity, has_infoz, alloc, blocked_element_count);
1103
0
  common.set_control(new_ctrl);
1104
0
  common.generate_new_seed(has_infoz);
1105
1106
0
  ResetCtrl(common, slot_size, blocked_element_count);
1107
0
  ResetGrowthLeft(new_capacity, blocked_element_count, common);
1108
1109
0
  if (ABSL_PREDICT_FALSE(has_infoz)) {
1110
0
    ReportResizeToInfoz(common, infoz, 0);
1111
0
  }
1112
0
}
1113
1114
// If the table was SOO, initializes new control bytes and transfers slot.
1115
// After transferring the slot, sets control and slots in CommonFields.
1116
// It is rare to resize an SOO table with one element to a large size.
1117
// Requires: `c` contains SOO data.
1118
void InsertOldSooSlotAndInitializeControlBytes(
1119
    CommonFields& c, const PolicyFunctions& __restrict policy, ctrl_t* new_ctrl,
1120
0
    void* new_slots, bool has_infoz) {
1121
0
  ABSL_SWISSTABLE_ASSERT(c.size() == policy.soo_capacity());
1122
0
  ABSL_SWISSTABLE_ASSERT(policy.soo_enabled);
1123
0
  const size_t new_capacity = c.capacity();
1124
1125
0
  c.generate_new_seed(has_infoz);
1126
1127
0
  const size_t soo_slot_hash =
1128
0
      policy.hash_slot(policy.hash_fn(c), c.soo_data(), c.seed().seed());
1129
0
  size_t offset = probe(ProbeCapacity{new_capacity}, soo_slot_hash).offset();
1130
0
  offset = offset == new_capacity ? 0 : offset;
1131
0
  SanitizerPoisonMemoryRegion(new_slots, policy.slot_size * new_capacity);
1132
0
  void* target_slot = SlotAddress(new_slots, offset, policy.slot_size);
1133
0
  SanitizerUnpoisonMemoryRegion(target_slot, policy.slot_size);
1134
0
  policy.transfer_n(&c, target_slot, c.soo_data(), 1);
1135
0
  c.set_control(new_ctrl);
1136
0
  ResetCtrl(c, policy.slot_size, /*blocked_element_count=*/0);
1137
0
  SetCtrl(c, offset, H2(soo_slot_hash), policy.slot_size);
1138
0
}
1139
1140
enum class ResizeFullSooTableSamplingMode {
1141
  kNoSampling,
1142
  // Force sampling. If the table was still not sampled, do not resize.
1143
  kForceSampleNoResizeIfUnsampled,
1144
};
1145
1146
void AssertSoo([[maybe_unused]] CommonFields& common,
1147
25.2k
               [[maybe_unused]] const PolicyFunctions& __restrict policy) {
1148
25.2k
  ABSL_SWISSTABLE_ASSERT(policy.soo_enabled);
1149
25.2k
  ABSL_SWISSTABLE_ASSERT(common.capacity() == policy.soo_capacity());
1150
25.2k
}
1151
void AssertFullSoo([[maybe_unused]] CommonFields& common,
1152
0
                   [[maybe_unused]] const PolicyFunctions& __restrict policy) {
1153
0
  AssertSoo(common, policy);
1154
0
  ABSL_SWISSTABLE_ASSERT(common.size() == policy.soo_capacity());
1155
0
}
1156
1157
void ResizeFullSooTable(CommonFields& common,
1158
                        const PolicyFunctions& __restrict policy,
1159
                        size_t new_capacity,
1160
0
                        ResizeFullSooTableSamplingMode sampling_mode) {
1161
0
  AssertFullSoo(common, policy);
1162
0
  const size_t slot_size = policy.slot_size;
1163
0
  void* alloc = policy.get_char_alloc(common);
1164
0
  constexpr size_t kTableSize = 1;
1165
1166
0
  HashtablezInfoHandle infoz;
1167
0
  bool has_infoz = false;
1168
0
  if (sampling_mode ==
1169
0
      ResizeFullSooTableSamplingMode::kForceSampleNoResizeIfUnsampled) {
1170
0
    if (ABSL_PREDICT_FALSE(policy.is_hashtablez_eligible)) {
1171
0
      infoz = ForcedTrySample(slot_size, policy.key_size, policy.value_size,
1172
0
                              policy.soo_capacity());
1173
0
    }
1174
1175
0
    if (!infoz.IsSampled()) return;
1176
0
    has_infoz = true;
1177
0
  }
1178
1179
0
  common.set_capacity(new_capacity);
1180
1181
  // We do not set control and slots in CommonFields yet to avoid overriding
1182
  // SOO data.
1183
0
  const auto [new_ctrl, new_slots] =
1184
0
      AllocBackingArray(common, policy, new_capacity, has_infoz, alloc,
1185
0
                        /*blocked_element_count=*/0);
1186
1187
0
  InsertOldSooSlotAndInitializeControlBytes(common, policy, new_ctrl, new_slots,
1188
0
                                            has_infoz);
1189
0
  ResetGrowthLeft(new_capacity, kTableSize, common);
1190
0
  if (has_infoz) {
1191
0
    common.set_has_infoz();
1192
0
    common.set_infoz(infoz);
1193
0
    infoz.RecordStorageChanged(kTableSize, new_capacity);
1194
0
  }
1195
0
}
1196
1197
void GrowIntoSingleGroupShuffleControlBytes(ctrl_t* __restrict old_ctrl,
1198
                                            size_t old_capacity,
1199
                                            size_t old_blocked_element_count,
1200
                                            ctrl_t* __restrict new_ctrl,
1201
69.2k
                                            size_t new_capacity) {
1202
69.2k
  ABSL_SWISSTABLE_ASSERT(is_single_group(new_capacity));
1203
69.2k
  constexpr size_t kHalfWidth = Group::kWidth / 2;
1204
69.2k
  ABSL_ASSUME(old_capacity < kHalfWidth);
1205
69.2k
  ABSL_ASSUME(old_capacity > 0);
1206
69.2k
  static_assert(Group::kWidth == 8 || Group::kWidth == 16,
1207
69.2k
                "Group size is not supported.");
1208
1209
  // NOTE: operations are done with compile time known size = 8.
1210
  // Compiler optimizes that into single ASM operation.
1211
1212
  // Load the bytes from old_capacity. This contains
1213
  // - the sentinel byte
1214
  // - all the old control bytes
1215
  // - the rest is filled with kEmpty bytes
1216
  // Example:
1217
  // old_ctrl =     012S012EEEEEEEEE...
1218
  // copied_bytes = S012EEEE
1219
  // Example with blocked elements:
1220
  // old_ctrl =     01SS01SEEEEEEEEE...
1221
  // copied_bytes = S01SEEEE
1222
69.2k
  uint64_t copied_bytes = absl::little_endian::Load64(old_ctrl + old_capacity);
1223
1224
  // We change the sentinel byte to kEmpty before storing to both the start of
1225
  // the new_ctrl, and past the end of the new_ctrl later for the new cloned
1226
  // bytes. Note that this is faster than setting the sentinel byte to kEmpty
1227
  // after the copy directly in new_ctrl because we are limited on store
1228
  // bandwidth.
1229
69.2k
  static constexpr uint64_t kEmptyXorSentinel =
1230
69.2k
      static_cast<uint8_t>(ctrl_t::kEmpty) ^
1231
69.2k
      static_cast<uint8_t>(ctrl_t::kSentinel);
1232
1233
  // Replace the first byte kSentinel with kEmpty.
1234
  // Resulting bytes will be shifted by one byte old control blocks.
1235
  // Example:
1236
  // old_ctrl = 012S012EEEEEEEEE...
1237
  // before =   S012EEEE
1238
  // after  =   E012EEEE
1239
69.2k
  copied_bytes ^= kEmptyXorSentinel;
1240
1241
69.2k
  if (ABSL_PREDICT_FALSE(old_blocked_element_count > 0)) {
1242
    // Replacing blocked sentinel elements with kEmpty.
1243
0
    static constexpr uint64_t kAllBytesEmptyXorSentinel =
1244
0
        kEmptyXorSentinel * uint64_t{0x0101010101010101};
1245
0
    uint64_t blocked_mask = kAllBytesEmptyXorSentinel;
1246
    // Keep old_blocked_element_count bytes in the mask.
1247
0
    blocked_mask >>= 64 - old_blocked_element_count * 8;
1248
    // Shift the mask to the start of the blocked elements bytes.
1249
0
    blocked_mask <<= (old_capacity - old_blocked_element_count + 1) * 8;
1250
    // Example with blocked elements:
1251
    // old_ctrl = 0SSS0SSEEEEEEEEE...
1252
    // before =   E0SSEEEE
1253
    // after  =   E0EEEEEE
1254
0
    copied_bytes ^= blocked_mask;
1255
0
  }
1256
1257
69.2k
  if (Group::kWidth == 8) {
1258
    // With group size 8, we can grow with two write operations.
1259
0
    ABSL_SWISSTABLE_ASSERT(old_capacity < 8 &&
1260
0
                           "old_capacity is too large for group size 8");
1261
0
    absl::little_endian::Store64(new_ctrl, copied_bytes);
1262
1263
0
    static constexpr uint64_t kSentinal64 =
1264
0
        static_cast<uint8_t>(ctrl_t::kSentinel);
1265
1266
    // Prepend kSentinel byte to the beginning of copied_bytes.
1267
    // We have maximum 3 non-empty bytes at the beginning of copied_bytes for
1268
    // group size 8.
1269
    // Example:
1270
    // old_ctrl = 012S012EEEE
1271
    // before =   E012EEEE
1272
    // after  =   SE012EEE
1273
0
    copied_bytes = (copied_bytes << 8) ^ kSentinal64;
1274
0
    absl::little_endian::Store64(new_ctrl + new_capacity, copied_bytes);
1275
    // Example for capacity 3:
1276
    // old_ctrl = 012S012EEEE
1277
    // After the first store:
1278
    //           >!
1279
    // new_ctrl = E012EEEE???????
1280
    // After the second store:
1281
    //                  >!
1282
    // new_ctrl = E012EEESE012EEE
1283
0
    return;
1284
0
  }
1285
1286
69.2k
  ABSL_SWISSTABLE_ASSERT(Group::kWidth == 16);  // NOLINT(misc-static-assert)
1287
1288
  // Fill the second half of the main control bytes with kEmpty.
1289
  // For small capacity that may write into mirrored control bytes.
1290
  // It is fine as we will overwrite all the bytes later.
1291
69.2k
  std::memset(new_ctrl + kHalfWidth, static_cast<int8_t>(ctrl_t::kEmpty),
1292
69.2k
              kHalfWidth);
1293
  // Fill the second half of the mirrored control bytes with kEmpty.
1294
69.2k
  std::memset(new_ctrl + new_capacity + kHalfWidth,
1295
69.2k
              static_cast<int8_t>(ctrl_t::kEmpty), kHalfWidth);
1296
  // Copy the first half of the non-mirrored control bytes.
1297
69.2k
  absl::little_endian::Store64(new_ctrl, copied_bytes);
1298
69.2k
  new_ctrl[new_capacity] = ctrl_t::kSentinel;
1299
  // Copy the first half of the mirrored control bytes.
1300
69.2k
  absl::little_endian::Store64(new_ctrl + new_capacity + 1, copied_bytes);
1301
1302
  // Example for growth capacity 1->3:
1303
  // old_ctrl =                  0S0EEEEEEEEEEEEEE
1304
  // new_ctrl at the end =       E0ESE0EEEEEEEEEEEEE
1305
  //                                    >!
1306
  // new_ctrl after 1st memset = ????????EEEEEEEE???
1307
  //                                       >!
1308
  // new_ctrl after 2nd memset = ????????EEEEEEEEEEE
1309
  //                            >!
1310
  // new_ctrl after 1st store =  E0EEEEEEEEEEEEEEEEE
1311
  // new_ctrl after kSentinel =  E0ESEEEEEEEEEEEEEEE
1312
  //                                >!
1313
  // new_ctrl after 2nd store =  E0ESE0EEEEEEEEEEEEE
1314
1315
  // Example for growth capacity 3->7:
1316
  // old_ctrl =                  012S012EEEEEEEEEEEE
1317
  // new_ctrl at the end =       E012EEESE012EEEEEEEEEEE
1318
  //                                    >!
1319
  // new_ctrl after 1st memset = ????????EEEEEEEE???????
1320
  //                                           >!
1321
  // new_ctrl after 2nd memset = ????????EEEEEEEEEEEEEEE
1322
  //                            >!
1323
  // new_ctrl after 1st store =  E012EEEEEEEEEEEEEEEEEEE
1324
  // new_ctrl after kSentinel =  E012EEESEEEEEEEEEEEEEEE
1325
  //                                >!
1326
  // new_ctrl after 2nd store =  E012EEESE012EEEEEEEEEEE
1327
1328
  // Example for growth capacity 7->15:
1329
  // old_ctrl =                  0123456S0123456EEEEEEEE
1330
  // new_ctrl at the end =       E0123456EEEEEEESE0123456EEEEEEE
1331
  //                                    >!
1332
  // new_ctrl after 1st memset = ????????EEEEEEEE???????????????
1333
  //                                                   >!
1334
  // new_ctrl after 2nd memset = ????????EEEEEEEE???????EEEEEEEE
1335
  //                            >!
1336
  // new_ctrl after 1st store =  E0123456EEEEEEEE???????EEEEEEEE
1337
  // new_ctrl after kSentinel =  E0123456EEEEEEES???????EEEEEEEE
1338
  //                                            >!
1339
  // new_ctrl after 2nd store =  E0123456EEEEEEESE0123456EEEEEEE
1340
69.2k
}
1341
1342
// Size of the buffer we allocate on stack for storing probed elements in
1343
// GrowToNextCapacity algorithm.
1344
constexpr size_t kProbedElementsBufferSize = 512;
1345
1346
// Decodes information about probed elements from contiguous memory.
1347
// Finds new position for each element and transfers it to the new slots.
1348
// Returns the total probe length.
1349
template <typename ProbedItem>
1350
ABSL_ATTRIBUTE_NOINLINE size_t DecodeAndInsertImpl(
1351
    CommonFields& c, const PolicyFunctions& __restrict policy,
1352
48.1k
    const ProbedItem* start, const ProbedItem* end, void* old_slots) {
1353
48.1k
  const HashtableCapacity new_capacity = c.capacity_impl();
1354
1355
48.1k
  void* new_slots = c.slot_array(new_capacity.capacity());
1356
48.1k
  ctrl_t* new_ctrl = c.control();
1357
48.1k
  size_t total_probe_length = 0;
1358
1359
48.1k
  const size_t slot_size = policy.slot_size;
1360
48.1k
  auto transfer_n = policy.transfer_n;
1361
1362
160k
  for (; start < end; ++start) {
1363
112k
    const FindInfo target = find_first_non_full_from_h1(
1364
112k
        new_ctrl, static_cast<size_t>(start->h1), new_capacity);
1365
112k
    total_probe_length += target.probe_length;
1366
112k
    const size_t old_index = static_cast<size_t>(start->source_offset);
1367
112k
    const size_t new_i = target.offset;
1368
112k
    ABSL_SWISSTABLE_ASSERT(old_index < new_capacity.capacity() / 2);
1369
112k
    ABSL_SWISSTABLE_ASSERT(new_i < new_capacity.capacity());
1370
112k
    ABSL_SWISSTABLE_ASSERT(IsEmpty(new_ctrl[new_i]));
1371
112k
    void* src_slot = SlotAddress(old_slots, old_index, slot_size);
1372
112k
    void* dst_slot = SlotAddress(new_slots, new_i, slot_size);
1373
112k
    SanitizerUnpoisonMemoryRegion(dst_slot, slot_size);
1374
112k
    transfer_n(&c, dst_slot, src_slot, 1);
1375
112k
    SetCtrlInLargeTable(c, new_i, static_cast<h2_t>(start->h2), slot_size);
1376
112k
  }
1377
48.1k
  return total_probe_length;
1378
48.1k
}
raw_hash_set.cc:unsigned long absl::container_internal::(anonymous namespace)::DecodeAndInsertImpl<absl::container_internal::ProbedItemImpl<unsigned int, 32ul> >(absl::container_internal::CommonFields&, absl::container_internal::PolicyFunctions const&, absl::container_internal::ProbedItemImpl<unsigned int, 32ul> const*, absl::container_internal::ProbedItemImpl<unsigned int, 32ul> const*, void*)
Line
Count
Source
1352
48.1k
    const ProbedItem* start, const ProbedItem* end, void* old_slots) {
1353
48.1k
  const HashtableCapacity new_capacity = c.capacity_impl();
1354
1355
48.1k
  void* new_slots = c.slot_array(new_capacity.capacity());
1356
48.1k
  ctrl_t* new_ctrl = c.control();
1357
48.1k
  size_t total_probe_length = 0;
1358
1359
48.1k
  const size_t slot_size = policy.slot_size;
1360
48.1k
  auto transfer_n = policy.transfer_n;
1361
1362
160k
  for (; start < end; ++start) {
1363
112k
    const FindInfo target = find_first_non_full_from_h1(
1364
112k
        new_ctrl, static_cast<size_t>(start->h1), new_capacity);
1365
112k
    total_probe_length += target.probe_length;
1366
112k
    const size_t old_index = static_cast<size_t>(start->source_offset);
1367
112k
    const size_t new_i = target.offset;
1368
112k
    ABSL_SWISSTABLE_ASSERT(old_index < new_capacity.capacity() / 2);
1369
112k
    ABSL_SWISSTABLE_ASSERT(new_i < new_capacity.capacity());
1370
112k
    ABSL_SWISSTABLE_ASSERT(IsEmpty(new_ctrl[new_i]));
1371
112k
    void* src_slot = SlotAddress(old_slots, old_index, slot_size);
1372
112k
    void* dst_slot = SlotAddress(new_slots, new_i, slot_size);
1373
112k
    SanitizerUnpoisonMemoryRegion(dst_slot, slot_size);
1374
112k
    transfer_n(&c, dst_slot, src_slot, 1);
1375
112k
    SetCtrlInLargeTable(c, new_i, static_cast<h2_t>(start->h2), slot_size);
1376
112k
  }
1377
48.1k
  return total_probe_length;
1378
48.1k
}
Unexecuted instantiation: raw_hash_set.cc:unsigned long absl::container_internal::(anonymous namespace)::DecodeAndInsertImpl<absl::container_internal::ProbedItemImpl<unsigned long, 64ul> >(absl::container_internal::CommonFields&, absl::container_internal::PolicyFunctions const&, absl::container_internal::ProbedItemImpl<unsigned long, 64ul> const*, absl::container_internal::ProbedItemImpl<unsigned long, 64ul> const*, void*)
Unexecuted instantiation: raw_hash_set.cc:unsigned long absl::container_internal::(anonymous namespace)::DecodeAndInsertImpl<absl::container_internal::ProbedItemImpl<unsigned long, 122ul> >(absl::container_internal::CommonFields&, absl::container_internal::PolicyFunctions const&, absl::container_internal::ProbedItemImpl<unsigned long, 122ul> const*, absl::container_internal::ProbedItemImpl<unsigned long, 122ul> const*, void*)
1379
1380
// Sentinel value for the start of marked elements.
1381
// Signals that there are no marked elements.
1382
constexpr size_t kNoMarkedElementsSentinel = ~size_t{};
1383
1384
// Process probed elements that did not fit into available buffers.
1385
// We marked them in control bytes as kMarkedForSlowTransfer.
1386
// Hash recomputation and full probing is done here.
1387
// This use case should be extremely rare.
1388
ABSL_ATTRIBUTE_NOINLINE size_t ProcessProbedMarkedElements(
1389
    CommonFields& c, const PolicyFunctions& __restrict policy, ctrl_t* old_ctrl,
1390
0
    void* old_slots, size_t start) {
1391
0
  size_t old_capacity = PreviousCapacity(c.capacity());
1392
0
  const size_t slot_size = policy.slot_size;
1393
0
  void* new_slots = c.slot_array(c.capacity());
1394
0
  size_t total_probe_length = 0;
1395
0
  const void* hash_fn = policy.hash_fn(c);
1396
0
  auto hash_slot = policy.hash_slot;
1397
0
  auto transfer_n = policy.transfer_n;
1398
0
  const size_t seed = c.seed().seed();
1399
0
  for (size_t old_index = start; old_index < old_capacity; ++old_index) {
1400
0
    if (old_ctrl[old_index] != ctrl_t::kMarkedForSlowTransfer) {
1401
0
      continue;
1402
0
    }
1403
0
    void* src_slot = SlotAddress(old_slots, old_index, slot_size);
1404
0
    const size_t hash = hash_slot(hash_fn, src_slot, seed);
1405
0
    const FindInfo target = find_first_non_full(c, hash);
1406
0
    total_probe_length += target.probe_length;
1407
0
    const size_t new_i = target.offset;
1408
0
    void* dst_slot = SlotAddress(new_slots, new_i, slot_size);
1409
0
    SetCtrlInLargeTable(c, new_i, H2(hash), slot_size);
1410
0
    transfer_n(&c, dst_slot, src_slot, 1);
1411
0
  }
1412
0
  return total_probe_length;
1413
0
}
1414
1415
// The largest old capacity for which it is guaranteed that all probed elements
1416
// fit in ProbedItemEncoder's local buffer.
1417
// For such tables, `encode_probed_element` is trivial.
1418
constexpr size_t kMaxLocalBufferOldCapacity =
1419
    kProbedElementsBufferSize / sizeof(ProbedItem4Bytes) - 1;
1420
static_assert(IsValidCapacity(kMaxLocalBufferOldCapacity));
1421
constexpr size_t kMaxLocalBufferNewCapacity =
1422
    NextCapacity(kMaxLocalBufferOldCapacity);
1423
static_assert(kMaxLocalBufferNewCapacity <= ProbedItem4Bytes::kMaxNewCapacity);
1424
static_assert(NextCapacity(kMaxLocalBufferNewCapacity) <=
1425
              ProbedItem4Bytes::kMaxNewCapacity);
1426
1427
// Initializes mirrored control bytes after
1428
// transfer_unprobed_elements_to_next_capacity.
1429
107k
void InitializeMirroredControlBytes(ctrl_t* new_ctrl, size_t new_capacity) {
1430
107k
  std::memcpy(new_ctrl + new_capacity + 1, new_ctrl, Group::kWidth - 1);
1431
107k
  new_ctrl[new_capacity] = ctrl_t::kSentinel;
1432
107k
}
1433
1434
// Encodes probed elements into available memory.
1435
// At first, a local (on stack) buffer is used. The size of the buffer is
1436
// kProbedElementsBufferSize bytes.
1437
// When the local buffer is full, we switch to `control_` buffer. We are allowed
1438
// to overwrite `control_` buffer till the `source_offset` byte. In case we have
1439
// no space in `control_` buffer, we fallback to a naive algorithm for all the
1440
// rest of the probed elements. We mark elements as kSentinel in control bytes
1441
// and later process them fully. See ProcessMarkedElements for details. It
1442
// should be extremely rare.
1443
template <typename ProbedItemType,
1444
          // If true, we only use the local buffer and never switch to the
1445
          // control buffer.
1446
          bool kGuaranteedFitToBuffer = false>
1447
class ProbedItemEncoder {
1448
 public:
1449
  using ProbedItem = ProbedItemType;
1450
107k
  explicit ProbedItemEncoder(ctrl_t* control) : control_(control) {}
raw_hash_set.cc:absl::container_internal::(anonymous namespace)::ProbedItemEncoder<absl::container_internal::ProbedItemImpl<unsigned int, 32ul>, true>::ProbedItemEncoder(absl::container_internal::ctrl_t*)
Line
Count
Source
1450
90.6k
  explicit ProbedItemEncoder(ctrl_t* control) : control_(control) {}
raw_hash_set.cc:absl::container_internal::(anonymous namespace)::ProbedItemEncoder<absl::container_internal::ProbedItemImpl<unsigned int, 32ul>, false>::ProbedItemEncoder(absl::container_internal::ctrl_t*)
Line
Count
Source
1450
17.2k
  explicit ProbedItemEncoder(ctrl_t* control) : control_(control) {}
Unexecuted instantiation: raw_hash_set.cc:absl::container_internal::(anonymous namespace)::ProbedItemEncoder<absl::container_internal::ProbedItemImpl<unsigned long, 64ul>, false>::ProbedItemEncoder(absl::container_internal::ctrl_t*)
Unexecuted instantiation: raw_hash_set.cc:absl::container_internal::(anonymous namespace)::ProbedItemEncoder<absl::container_internal::ProbedItemImpl<unsigned long, 122ul>, false>::ProbedItemEncoder(absl::container_internal::ctrl_t*)
1451
1452
  // Encode item into the best available location.
1453
112k
  void EncodeItem(ProbedItem item) {
1454
112k
    if (ABSL_PREDICT_FALSE(!kGuaranteedFitToBuffer && pos_ >= end_)) {
1455
0
      return ProcessEncodeWithOverflow(item);
1456
0
    }
1457
112k
    ABSL_SWISSTABLE_ASSERT(pos_ < end_);
1458
112k
    *pos_ = item;
1459
112k
    ++pos_;
1460
112k
  }
raw_hash_set.cc:absl::container_internal::(anonymous namespace)::ProbedItemEncoder<absl::container_internal::ProbedItemImpl<unsigned int, 32ul>, true>::EncodeItem(absl::container_internal::ProbedItemImpl<unsigned int, 32ul>)
Line
Count
Source
1453
88.7k
  void EncodeItem(ProbedItem item) {
1454
88.7k
    if (ABSL_PREDICT_FALSE(!kGuaranteedFitToBuffer && pos_ >= end_)) {
1455
0
      return ProcessEncodeWithOverflow(item);
1456
0
    }
1457
88.7k
    ABSL_SWISSTABLE_ASSERT(pos_ < end_);
1458
88.7k
    *pos_ = item;
1459
88.7k
    ++pos_;
1460
88.7k
  }
raw_hash_set.cc:absl::container_internal::(anonymous namespace)::ProbedItemEncoder<absl::container_internal::ProbedItemImpl<unsigned int, 32ul>, false>::EncodeItem(absl::container_internal::ProbedItemImpl<unsigned int, 32ul>)
Line
Count
Source
1453
23.4k
  void EncodeItem(ProbedItem item) {
1454
23.4k
    if (ABSL_PREDICT_FALSE(!kGuaranteedFitToBuffer && pos_ >= end_)) {
1455
0
      return ProcessEncodeWithOverflow(item);
1456
0
    }
1457
23.4k
    ABSL_SWISSTABLE_ASSERT(pos_ < end_);
1458
23.4k
    *pos_ = item;
1459
23.4k
    ++pos_;
1460
23.4k
  }
Unexecuted instantiation: raw_hash_set.cc:absl::container_internal::(anonymous namespace)::ProbedItemEncoder<absl::container_internal::ProbedItemImpl<unsigned long, 64ul>, false>::EncodeItem(absl::container_internal::ProbedItemImpl<unsigned long, 64ul>)
Unexecuted instantiation: raw_hash_set.cc:absl::container_internal::(anonymous namespace)::ProbedItemEncoder<absl::container_internal::ProbedItemImpl<unsigned long, 122ul>, false>::EncodeItem(absl::container_internal::ProbedItemImpl<unsigned long, 122ul>)
1461
1462
  // Decodes information about probed elements from all available sources.
1463
  // Finds new position for each element and transfers it to the new slots.
1464
  // Returns the total probe length.
1465
  size_t DecodeAndInsertToTable(CommonFields& common,
1466
                                const PolicyFunctions& __restrict policy,
1467
107k
                                void* old_slots) const {
1468
107k
    if (pos_ == buffer_) {
1469
59.7k
      return 0;
1470
59.7k
    }
1471
48.1k
    if constexpr (kGuaranteedFitToBuffer) {
1472
39.7k
      return DecodeAndInsertImpl(common, policy, buffer_, pos_, old_slots);
1473
39.7k
    }
1474
0
    size_t total_probe_length = DecodeAndInsertImpl(
1475
48.1k
        common, policy, buffer_,
1476
48.1k
        local_buffer_full_ ? buffer_ + kBufferSize : pos_, old_slots);
1477
48.1k
    if (!local_buffer_full_) {
1478
8.37k
      return total_probe_length;
1479
8.37k
    }
1480
39.7k
    total_probe_length +=
1481
39.7k
        DecodeAndInsertToTableOverflow(common, policy, old_slots);
1482
39.7k
    return total_probe_length;
1483
48.1k
  }
raw_hash_set.cc:absl::container_internal::(anonymous namespace)::ProbedItemEncoder<absl::container_internal::ProbedItemImpl<unsigned int, 32ul>, true>::DecodeAndInsertToTable(absl::container_internal::CommonFields&, absl::container_internal::PolicyFunctions const&, void*) const
Line
Count
Source
1467
90.6k
                                void* old_slots) const {
1468
90.6k
    if (pos_ == buffer_) {
1469
50.8k
      return 0;
1470
50.8k
    }
1471
39.7k
    if constexpr (kGuaranteedFitToBuffer) {
1472
39.7k
      return DecodeAndInsertImpl(common, policy, buffer_, pos_, old_slots);
1473
39.7k
    }
1474
0
    size_t total_probe_length = DecodeAndInsertImpl(
1475
39.7k
        common, policy, buffer_,
1476
39.7k
        local_buffer_full_ ? buffer_ + kBufferSize : pos_, old_slots);
1477
39.7k
    if (!local_buffer_full_) {
1478
0
      return total_probe_length;
1479
0
    }
1480
39.7k
    total_probe_length +=
1481
39.7k
        DecodeAndInsertToTableOverflow(common, policy, old_slots);
1482
39.7k
    return total_probe_length;
1483
39.7k
  }
raw_hash_set.cc:absl::container_internal::(anonymous namespace)::ProbedItemEncoder<absl::container_internal::ProbedItemImpl<unsigned int, 32ul>, false>::DecodeAndInsertToTable(absl::container_internal::CommonFields&, absl::container_internal::PolicyFunctions const&, void*) const
Line
Count
Source
1467
17.2k
                                void* old_slots) const {
1468
17.2k
    if (pos_ == buffer_) {
1469
8.89k
      return 0;
1470
8.89k
    }
1471
    if constexpr (kGuaranteedFitToBuffer) {
1472
      return DecodeAndInsertImpl(common, policy, buffer_, pos_, old_slots);
1473
    }
1474
8.37k
    size_t total_probe_length = DecodeAndInsertImpl(
1475
8.37k
        common, policy, buffer_,
1476
8.37k
        local_buffer_full_ ? buffer_ + kBufferSize : pos_, old_slots);
1477
8.37k
    if (!local_buffer_full_) {
1478
8.37k
      return total_probe_length;
1479
8.37k
    }
1480
0
    total_probe_length +=
1481
0
        DecodeAndInsertToTableOverflow(common, policy, old_slots);
1482
0
    return total_probe_length;
1483
8.37k
  }
Unexecuted instantiation: raw_hash_set.cc:absl::container_internal::(anonymous namespace)::ProbedItemEncoder<absl::container_internal::ProbedItemImpl<unsigned long, 64ul>, false>::DecodeAndInsertToTable(absl::container_internal::CommonFields&, absl::container_internal::PolicyFunctions const&, void*) const
Unexecuted instantiation: raw_hash_set.cc:absl::container_internal::(anonymous namespace)::ProbedItemEncoder<absl::container_internal::ProbedItemImpl<unsigned long, 122ul>, false>::DecodeAndInsertToTable(absl::container_internal::CommonFields&, absl::container_internal::PolicyFunctions const&, void*) const
1484
1485
 private:
1486
0
  static ProbedItem* AlignToNextItem(void* ptr) {
1487
0
    return reinterpret_cast<ProbedItem*>(AlignUpTo(
1488
0
        reinterpret_cast<uintptr_t>(ptr), alignof(ProbedItem)));
1489
0
  }
Unexecuted instantiation: raw_hash_set.cc:absl::container_internal::(anonymous namespace)::ProbedItemEncoder<absl::container_internal::ProbedItemImpl<unsigned int, 32ul>, false>::AlignToNextItem(void*)
Unexecuted instantiation: raw_hash_set.cc:absl::container_internal::(anonymous namespace)::ProbedItemEncoder<absl::container_internal::ProbedItemImpl<unsigned long, 64ul>, false>::AlignToNextItem(void*)
Unexecuted instantiation: raw_hash_set.cc:absl::container_internal::(anonymous namespace)::ProbedItemEncoder<absl::container_internal::ProbedItemImpl<unsigned long, 122ul>, false>::AlignToNextItem(void*)
Unexecuted instantiation: raw_hash_set.cc:absl::container_internal::(anonymous namespace)::ProbedItemEncoder<absl::container_internal::ProbedItemImpl<unsigned int, 32ul>, true>::AlignToNextItem(void*)
1490
1491
0
  ProbedItem* OverflowBufferStart() const {
1492
0
    ABSL_SWISSTABLE_ASSERT(!kGuaranteedFitToBuffer &&
1493
0
                           "OverflowBufferStart should not be called when "
1494
0
                           "kGuaranteedFitToBuffer is true.");
1495
    // We reuse GrowthInfo memory as well.
1496
0
    return AlignToNextItem(
1497
0
        control_ - MetadataBeforeControlSize(/*has_infoz=*/false,
1498
0
                                 NextCapacity(kMaxLocalBufferOldCapacity)));
1499
0
  }
Unexecuted instantiation: raw_hash_set.cc:absl::container_internal::(anonymous namespace)::ProbedItemEncoder<absl::container_internal::ProbedItemImpl<unsigned int, 32ul>, false>::OverflowBufferStart() const
Unexecuted instantiation: raw_hash_set.cc:absl::container_internal::(anonymous namespace)::ProbedItemEncoder<absl::container_internal::ProbedItemImpl<unsigned long, 64ul>, false>::OverflowBufferStart() const
Unexecuted instantiation: raw_hash_set.cc:absl::container_internal::(anonymous namespace)::ProbedItemEncoder<absl::container_internal::ProbedItemImpl<unsigned long, 122ul>, false>::OverflowBufferStart() const
Unexecuted instantiation: raw_hash_set.cc:absl::container_internal::(anonymous namespace)::ProbedItemEncoder<absl::container_internal::ProbedItemImpl<unsigned int, 32ul>, true>::OverflowBufferStart() const
1500
1501
  // Encodes item when previously allocated buffer is full.
1502
  // At first that happens when local buffer is full.
1503
  // We switch from the local buffer to the control buffer.
1504
  // Every time this function is called, the available buffer is extended till
1505
  // `item.source_offset` byte in the control buffer.
1506
  // After the buffer is extended, this function wouldn't be called till the
1507
  // buffer is exhausted.
1508
  //
1509
  // If there's no space in the control buffer, we fallback to naive algorithm
1510
  // and mark probed elements as kMarkedForSlowTransfer in the control buffer.
1511
  // In this case, we will call this function for every subsequent probed
1512
  // element.
1513
0
  ABSL_ATTRIBUTE_NOINLINE void ProcessEncodeWithOverflow(ProbedItem item) {
1514
0
    if (!local_buffer_full_) {
1515
0
      local_buffer_full_ = true;
1516
0
      pos_ = OverflowBufferStart();
1517
0
    }
1518
0
    const size_t source_offset = static_cast<size_t>(item.source_offset);
1519
    // We are in fallback mode so we can't reuse control buffer anymore.
1520
    // Probed elements are marked as kMarkedForSlowTransfer in the control
1521
    // buffer.
1522
0
    if (ABSL_PREDICT_FALSE(marked_elements_starting_position_ !=
1523
0
                           kNoMarkedElementsSentinel)) {
1524
0
      control_[source_offset] = ctrl_t::kMarkedForSlowTransfer;
1525
0
      return;
1526
0
    }
1527
    // Refresh the end pointer to the new available position.
1528
    // Invariant: if pos < end, then we have at least sizeof(ProbedItem) bytes
1529
    // to write.
1530
0
    end_ = control_ + source_offset + 1 - sizeof(ProbedItem);
1531
0
    if (ABSL_PREDICT_TRUE(pos_ < end_)) {
1532
0
      *pos_ = item;
1533
0
      ++pos_;
1534
0
      return;
1535
0
    }
1536
0
    control_[source_offset] = ctrl_t::kMarkedForSlowTransfer;
1537
0
    marked_elements_starting_position_ = source_offset;
1538
    // Now we will always fall down to `ProcessEncodeWithOverflow`.
1539
0
    ABSL_SWISSTABLE_ASSERT(pos_ >= end_);
1540
0
  }
Unexecuted instantiation: raw_hash_set.cc:absl::container_internal::(anonymous namespace)::ProbedItemEncoder<absl::container_internal::ProbedItemImpl<unsigned int, 32ul>, false>::ProcessEncodeWithOverflow(absl::container_internal::ProbedItemImpl<unsigned int, 32ul>)
Unexecuted instantiation: raw_hash_set.cc:absl::container_internal::(anonymous namespace)::ProbedItemEncoder<absl::container_internal::ProbedItemImpl<unsigned long, 64ul>, false>::ProcessEncodeWithOverflow(absl::container_internal::ProbedItemImpl<unsigned long, 64ul>)
Unexecuted instantiation: raw_hash_set.cc:absl::container_internal::(anonymous namespace)::ProbedItemEncoder<absl::container_internal::ProbedItemImpl<unsigned long, 122ul>, false>::ProcessEncodeWithOverflow(absl::container_internal::ProbedItemImpl<unsigned long, 122ul>)
Unexecuted instantiation: raw_hash_set.cc:absl::container_internal::(anonymous namespace)::ProbedItemEncoder<absl::container_internal::ProbedItemImpl<unsigned int, 32ul>, true>::ProcessEncodeWithOverflow(absl::container_internal::ProbedItemImpl<unsigned int, 32ul>)
1541
1542
  // Decodes information about probed elements from control buffer and processes
1543
  // marked elements.
1544
  // Finds new position for each element and transfers it to the new slots.
1545
  // Returns the total probe length.
1546
  ABSL_ATTRIBUTE_NOINLINE size_t DecodeAndInsertToTableOverflow(
1547
      CommonFields& common, const PolicyFunctions& __restrict policy,
1548
0
      void* old_slots) const {
1549
0
    ABSL_SWISSTABLE_ASSERT(local_buffer_full_ &&
1550
0
                           "must not be called when local buffer is not full");
1551
0
    size_t total_probe_length = DecodeAndInsertImpl(
1552
0
        common, policy, OverflowBufferStart(), pos_, old_slots);
1553
0
    if (ABSL_PREDICT_TRUE(marked_elements_starting_position_ ==
1554
0
                          kNoMarkedElementsSentinel)) {
1555
0
      return total_probe_length;
1556
0
    }
1557
0
    total_probe_length +=
1558
0
        ProcessProbedMarkedElements(common, policy, control_, old_slots,
1559
0
                                    marked_elements_starting_position_);
1560
0
    return total_probe_length;
1561
0
  }
Unexecuted instantiation: raw_hash_set.cc:absl::container_internal::(anonymous namespace)::ProbedItemEncoder<absl::container_internal::ProbedItemImpl<unsigned int, 32ul>, false>::DecodeAndInsertToTableOverflow(absl::container_internal::CommonFields&, absl::container_internal::PolicyFunctions const&, void*) const
Unexecuted instantiation: raw_hash_set.cc:absl::container_internal::(anonymous namespace)::ProbedItemEncoder<absl::container_internal::ProbedItemImpl<unsigned long, 64ul>, false>::DecodeAndInsertToTableOverflow(absl::container_internal::CommonFields&, absl::container_internal::PolicyFunctions const&, void*) const
Unexecuted instantiation: raw_hash_set.cc:absl::container_internal::(anonymous namespace)::ProbedItemEncoder<absl::container_internal::ProbedItemImpl<unsigned long, 122ul>, false>::DecodeAndInsertToTableOverflow(absl::container_internal::CommonFields&, absl::container_internal::PolicyFunctions const&, void*) const
Unexecuted instantiation: raw_hash_set.cc:absl::container_internal::(anonymous namespace)::ProbedItemEncoder<absl::container_internal::ProbedItemImpl<unsigned int, 32ul>, true>::DecodeAndInsertToTableOverflow(absl::container_internal::CommonFields&, absl::container_internal::PolicyFunctions const&, void*) const
1562
1563
  static constexpr size_t kBufferSize =
1564
      kProbedElementsBufferSize / sizeof(ProbedItem);
1565
  ProbedItem buffer_[kBufferSize];
1566
  // If local_buffer_full_ is false, then pos_/end_ are in the local buffer,
1567
  // otherwise, they're in the overflow buffer.
1568
  ProbedItem* pos_ = buffer_;
1569
  const void* end_ = buffer_ + kBufferSize;
1570
  ctrl_t* const control_;
1571
  size_t marked_elements_starting_position_ = kNoMarkedElementsSentinel;
1572
  bool local_buffer_full_ = false;
1573
};
1574
1575
// Grows to next capacity with specified encoder type.
1576
// Encoder is used to store probed elements that are processed later.
1577
// Different encoder is used depending on the capacity of the table.
1578
// Returns total probe length.
1579
template <typename Encoder>
1580
size_t GrowToNextCapacity(CommonFields& common,
1581
                          const PolicyFunctions& __restrict policy,
1582
107k
                          ctrl_t* old_ctrl, void* old_slots) {
1583
107k
  using ProbedItem = typename Encoder::ProbedItem;
1584
107k
  ABSL_SWISSTABLE_ASSERT(common.capacity() <= ProbedItem::kMaxNewCapacity);
1585
107k
  Encoder encoder(old_ctrl);
1586
107k
  policy.transfer_unprobed_elements_to_next_capacity(
1587
107k
      common, old_ctrl, old_slots, &encoder,
1588
112k
      [](void* probed_storage, h2_t h2, size_t source_offset, size_t h1) {
1589
112k
        auto encoder_ptr = static_cast<Encoder*>(probed_storage);
1590
112k
        encoder_ptr->EncodeItem(ProbedItem(h2, source_offset, h1));
1591
112k
      });
raw_hash_set.cc:absl::container_internal::(anonymous namespace)::GrowToNextCapacity<absl::container_internal::(anonymous namespace)::ProbedItemEncoder<absl::container_internal::ProbedItemImpl<unsigned int, 32ul>, true> >(absl::container_internal::CommonFields&, absl::container_internal::PolicyFunctions const&, absl::container_internal::ctrl_t*, void*)::{lambda(void*, unsigned char, unsigned long, unsigned long)#1}::operator()(void*, unsigned char, unsigned long, unsigned long) const
Line
Count
Source
1588
88.7k
      [](void* probed_storage, h2_t h2, size_t source_offset, size_t h1) {
1589
88.7k
        auto encoder_ptr = static_cast<Encoder*>(probed_storage);
1590
88.7k
        encoder_ptr->EncodeItem(ProbedItem(h2, source_offset, h1));
1591
88.7k
      });
raw_hash_set.cc:absl::container_internal::(anonymous namespace)::GrowToNextCapacity<absl::container_internal::(anonymous namespace)::ProbedItemEncoder<absl::container_internal::ProbedItemImpl<unsigned int, 32ul>, false> >(absl::container_internal::CommonFields&, absl::container_internal::PolicyFunctions const&, absl::container_internal::ctrl_t*, void*)::{lambda(void*, unsigned char, unsigned long, unsigned long)#1}::operator()(void*, unsigned char, unsigned long, unsigned long) const
Line
Count
Source
1588
23.4k
      [](void* probed_storage, h2_t h2, size_t source_offset, size_t h1) {
1589
23.4k
        auto encoder_ptr = static_cast<Encoder*>(probed_storage);
1590
23.4k
        encoder_ptr->EncodeItem(ProbedItem(h2, source_offset, h1));
1591
23.4k
      });
Unexecuted instantiation: raw_hash_set.cc:absl::container_internal::(anonymous namespace)::GrowToNextCapacity<absl::container_internal::(anonymous namespace)::ProbedItemEncoder<absl::container_internal::ProbedItemImpl<unsigned long, 64ul>, false> >(absl::container_internal::CommonFields&, absl::container_internal::PolicyFunctions const&, absl::container_internal::ctrl_t*, void*)::{lambda(void*, unsigned char, unsigned long, unsigned long)#1}::operator()(void*, unsigned char, unsigned long, unsigned long) const
Unexecuted instantiation: raw_hash_set.cc:absl::container_internal::(anonymous namespace)::GrowToNextCapacity<absl::container_internal::(anonymous namespace)::ProbedItemEncoder<absl::container_internal::ProbedItemImpl<unsigned long, 122ul>, false> >(absl::container_internal::CommonFields&, absl::container_internal::PolicyFunctions const&, absl::container_internal::ctrl_t*, void*)::{lambda(void*, unsigned char, unsigned long, unsigned long)#1}::operator()(void*, unsigned char, unsigned long, unsigned long) const
1592
107k
  InitializeMirroredControlBytes(common.control(), common.capacity());
1593
107k
  return encoder.DecodeAndInsertToTable(common, policy, old_slots);
1594
107k
}
raw_hash_set.cc:unsigned long absl::container_internal::(anonymous namespace)::GrowToNextCapacity<absl::container_internal::(anonymous namespace)::ProbedItemEncoder<absl::container_internal::ProbedItemImpl<unsigned int, 32ul>, true> >(absl::container_internal::CommonFields&, absl::container_internal::PolicyFunctions const&, absl::container_internal::ctrl_t*, void*)
Line
Count
Source
1582
90.6k
                          ctrl_t* old_ctrl, void* old_slots) {
1583
90.6k
  using ProbedItem = typename Encoder::ProbedItem;
1584
90.6k
  ABSL_SWISSTABLE_ASSERT(common.capacity() <= ProbedItem::kMaxNewCapacity);
1585
90.6k
  Encoder encoder(old_ctrl);
1586
90.6k
  policy.transfer_unprobed_elements_to_next_capacity(
1587
90.6k
      common, old_ctrl, old_slots, &encoder,
1588
90.6k
      [](void* probed_storage, h2_t h2, size_t source_offset, size_t h1) {
1589
90.6k
        auto encoder_ptr = static_cast<Encoder*>(probed_storage);
1590
90.6k
        encoder_ptr->EncodeItem(ProbedItem(h2, source_offset, h1));
1591
90.6k
      });
1592
90.6k
  InitializeMirroredControlBytes(common.control(), common.capacity());
1593
90.6k
  return encoder.DecodeAndInsertToTable(common, policy, old_slots);
1594
90.6k
}
raw_hash_set.cc:unsigned long absl::container_internal::(anonymous namespace)::GrowToNextCapacity<absl::container_internal::(anonymous namespace)::ProbedItemEncoder<absl::container_internal::ProbedItemImpl<unsigned int, 32ul>, false> >(absl::container_internal::CommonFields&, absl::container_internal::PolicyFunctions const&, absl::container_internal::ctrl_t*, void*)
Line
Count
Source
1582
17.2k
                          ctrl_t* old_ctrl, void* old_slots) {
1583
17.2k
  using ProbedItem = typename Encoder::ProbedItem;
1584
17.2k
  ABSL_SWISSTABLE_ASSERT(common.capacity() <= ProbedItem::kMaxNewCapacity);
1585
17.2k
  Encoder encoder(old_ctrl);
1586
17.2k
  policy.transfer_unprobed_elements_to_next_capacity(
1587
17.2k
      common, old_ctrl, old_slots, &encoder,
1588
17.2k
      [](void* probed_storage, h2_t h2, size_t source_offset, size_t h1) {
1589
17.2k
        auto encoder_ptr = static_cast<Encoder*>(probed_storage);
1590
17.2k
        encoder_ptr->EncodeItem(ProbedItem(h2, source_offset, h1));
1591
17.2k
      });
1592
17.2k
  InitializeMirroredControlBytes(common.control(), common.capacity());
1593
17.2k
  return encoder.DecodeAndInsertToTable(common, policy, old_slots);
1594
17.2k
}
Unexecuted instantiation: raw_hash_set.cc:unsigned long absl::container_internal::(anonymous namespace)::GrowToNextCapacity<absl::container_internal::(anonymous namespace)::ProbedItemEncoder<absl::container_internal::ProbedItemImpl<unsigned long, 64ul>, false> >(absl::container_internal::CommonFields&, absl::container_internal::PolicyFunctions const&, absl::container_internal::ctrl_t*, void*)
Unexecuted instantiation: raw_hash_set.cc:unsigned long absl::container_internal::(anonymous namespace)::GrowToNextCapacity<absl::container_internal::(anonymous namespace)::ProbedItemEncoder<absl::container_internal::ProbedItemImpl<unsigned long, 122ul>, false> >(absl::container_internal::CommonFields&, absl::container_internal::PolicyFunctions const&, absl::container_internal::ctrl_t*, void*)
1595
1596
// Grows to next capacity for relatively small tables so that even if all
1597
// elements are probed, we don't need to overflow the local buffer.
1598
// Returns total probe length.
1599
size_t GrowToNextCapacityThatFitsInLocalBuffer(
1600
    CommonFields& common, const PolicyFunctions& __restrict policy,
1601
90.6k
    ctrl_t* old_ctrl, void* old_slots) {
1602
90.6k
  ABSL_SWISSTABLE_ASSERT(common.capacity() <= kMaxLocalBufferNewCapacity);
1603
90.6k
  return GrowToNextCapacity<
1604
90.6k
      ProbedItemEncoder<ProbedItem4Bytes, /*kGuaranteedFitToBuffer=*/true>>(
1605
90.6k
      common, policy, old_ctrl, old_slots);
1606
90.6k
}
1607
1608
// Grows to next capacity with different encodings. Returns total probe length.
1609
// These functions are useful to simplify profile analysis.
1610
size_t GrowToNextCapacity4BytesEncoder(CommonFields& common,
1611
                                       const PolicyFunctions& __restrict policy,
1612
17.2k
                                       ctrl_t* old_ctrl, void* old_slots) {
1613
17.2k
  return GrowToNextCapacity<ProbedItemEncoder<ProbedItem4Bytes>>(
1614
17.2k
      common, policy, old_ctrl, old_slots);
1615
17.2k
}
1616
size_t GrowToNextCapacity8BytesEncoder(CommonFields& common,
1617
                                       const PolicyFunctions& __restrict policy,
1618
0
                                       ctrl_t* old_ctrl, void* old_slots) {
1619
0
  return GrowToNextCapacity<ProbedItemEncoder<ProbedItem8Bytes>>(
1620
0
      common, policy, old_ctrl, old_slots);
1621
0
}
1622
size_t GrowToNextCapacity16BytesEncoder(
1623
    CommonFields& common, const PolicyFunctions& __restrict policy,
1624
0
    ctrl_t* old_ctrl, void* old_slots) {
1625
0
  return GrowToNextCapacity<ProbedItemEncoder<ProbedItem16Bytes>>(
1626
0
      common, policy, old_ctrl, old_slots);
1627
0
}
1628
1629
// Grows to next capacity for tables with relatively large capacity so that we
1630
// can't guarantee that all probed elements fit in the local buffer. Returns
1631
// total probe length.
1632
size_t GrowToNextCapacityOverflowLocalBuffer(
1633
    CommonFields& common, const PolicyFunctions& __restrict policy,
1634
17.2k
    ctrl_t* old_ctrl, void* old_slots) {
1635
17.2k
  const size_t new_capacity = common.capacity();
1636
17.2k
  if (ABSL_PREDICT_TRUE(new_capacity <= ProbedItem4Bytes::kMaxNewCapacity)) {
1637
17.2k
    return GrowToNextCapacity4BytesEncoder(common, policy, old_ctrl, old_slots);
1638
17.2k
  }
1639
0
  if (ABSL_PREDICT_TRUE(new_capacity <= ProbedItem8Bytes::kMaxNewCapacity)) {
1640
0
    return GrowToNextCapacity8BytesEncoder(common, policy, old_ctrl, old_slots);
1641
0
  }
1642
  // 16 bytes encoding supports the maximum swisstable capacity.
1643
0
  return GrowToNextCapacity16BytesEncoder(common, policy, old_ctrl, old_slots);
1644
0
}
1645
1646
// Dispatches to the appropriate `GrowToNextCapacity*` function based on the
1647
// capacity of the table. Returns total probe length.
1648
ABSL_ATTRIBUTE_NOINLINE
1649
size_t GrowToNextCapacityDispatch(CommonFields& common,
1650
                                  const PolicyFunctions& __restrict policy,
1651
107k
                                  ctrl_t* old_ctrl, void* old_slots) {
1652
107k
  const size_t new_capacity = common.capacity();
1653
107k
  if (ABSL_PREDICT_TRUE(new_capacity <= kMaxLocalBufferNewCapacity)) {
1654
90.6k
    return GrowToNextCapacityThatFitsInLocalBuffer(common, policy, old_ctrl,
1655
90.6k
                                                   old_slots);
1656
90.6k
  } else {
1657
17.2k
    return GrowToNextCapacityOverflowLocalBuffer(common, policy, old_ctrl,
1658
17.2k
                                                 old_slots);
1659
17.2k
  }
1660
107k
}
1661
1662
void IncrementSmallSizeNonSoo(CommonFields& common,
1663
135k
                              const PolicyFunctions& __restrict policy) {
1664
135k
  ABSL_SWISSTABLE_ASSERT(common.is_small());
1665
135k
  common.increment_size();
1666
135k
  SanitizerUnpoisonMemoryRegion(
1667
135k
      SingleSlotAddress</*kSooEnabled=*/false>(common), policy.slot_size);
1668
135k
}
1669
1670
void IncrementSmallSize(CommonFields& common,
1671
0
                        const PolicyFunctions& __restrict policy) {
1672
0
  ABSL_SWISSTABLE_ASSERT(common.is_small());
1673
0
  if (policy.soo_enabled) {
1674
0
    common.set_full_soo();
1675
0
  } else {
1676
0
    IncrementSmallSizeNonSoo(common, policy);
1677
0
  }
1678
0
}
1679
1680
void* Grow1To3AndPrepareInsert(CommonFields& common,
1681
                               const PolicyFunctions& __restrict policy,
1682
31.3k
                               absl::FunctionRef<size_t(size_t)> get_hash) {
1683
  // TODO(b/413062340): Refactor to reuse more code with
1684
  // GrowSooTableToNextCapacityAndPrepareInsert.
1685
31.3k
  ABSL_SWISSTABLE_ASSERT(common.capacity() == 1);
1686
31.3k
  ABSL_SWISSTABLE_ASSERT(!common.empty());
1687
31.3k
  ABSL_SWISSTABLE_ASSERT(!policy.soo_enabled);
1688
  // 1-element tables can't have any blocked elements.
1689
31.3k
  ABSL_SWISSTABLE_ASSERT(common.blocked_element_count() == 0);
1690
31.3k
  constexpr size_t kOldCapacity = 1;
1691
31.3k
  constexpr size_t kNewCapacity = NextCapacity(kOldCapacity);
1692
31.3k
  void* old_slots = common.slot_array(kOldCapacity);
1693
1694
31.3k
  const size_t slot_size = policy.slot_size;
1695
31.3k
  const size_t slot_align = policy.slot_align;
1696
31.3k
  void* alloc = policy.get_char_alloc(common);
1697
31.3k
  HashtablezInfoHandle infoz = common.infoz();
1698
31.3k
  const bool has_infoz = infoz.IsSampled();
1699
31.3k
  common.set_capacity(kNewCapacity);
1700
1701
31.3k
  const auto [new_ctrl, new_slots] =
1702
31.3k
      AllocBackingArray(common, policy, kNewCapacity, has_infoz, alloc,
1703
31.3k
                        /*blocked_element_count=*/0);
1704
31.3k
  common.set_control(new_ctrl);
1705
31.3k
  SanitizerPoisonMemoryRegion(new_slots, kNewCapacity * slot_size);
1706
1707
31.3k
  if (ABSL_PREDICT_TRUE(!has_infoz)) {
1708
    // When we're sampled, we already have a seed.
1709
31.3k
    common.generate_new_seed(/*has_infoz=*/false);
1710
31.3k
  }
1711
31.3k
  const size_t new_hash = get_hash(common.seed().seed());
1712
31.3k
  h2_t new_h2 = H2(new_hash);
1713
31.3k
  size_t orig_hash =
1714
31.3k
      policy.hash_slot(policy.hash_fn(common), old_slots, common.seed().seed());
1715
31.3k
  size_t offset = Resize1To3NewOffset(new_hash);
1716
31.3k
  InitializeThreeElementsControlBytes(H2(orig_hash), new_h2, offset, new_ctrl);
1717
1718
31.3k
  void* old_element_target = NextSlot(new_slots, slot_size);
1719
31.3k
  SanitizerUnpoisonMemoryRegion(old_element_target, slot_size);
1720
31.3k
  policy.transfer_n(&common, old_element_target, old_slots, 1);
1721
1722
31.3k
  void* new_element_target_slot = SlotAddress(new_slots, offset, slot_size);
1723
31.3k
  SanitizerUnpoisonMemoryRegion(new_element_target_slot, slot_size);
1724
1725
31.3k
  DeallocBackingArrayImpl(alloc, kOldCapacity,
1726
                          // old_slots == old_ctrl in case of capacity == 1.
1727
31.3k
                          static_cast<ctrl_t*>(old_slots), slot_size,
1728
31.3k
                          slot_align, has_infoz,
1729
31.3k
                          /*blocked_element_count=*/0, policy.dealloc);
1730
31.3k
  PrepareInsertCommon(common);
1731
31.3k
  ABSL_SWISSTABLE_ASSERT(common.size() == 2);
1732
31.3k
  common.InitGrowthLeftNoDeleted(kNewCapacity - 2, kNewCapacity);
1733
1734
31.3k
  if (ABSL_PREDICT_FALSE(has_infoz)) {
1735
0
    ReportSingleGroupTableGrowthToInfoz(common, infoz, new_hash);
1736
0
  }
1737
31.3k
  return new_element_target_slot;
1738
31.3k
}
1739
1740
// Grows to next capacity and prepares insert for the given new_hash.
1741
// Returns the offset of the new element.
1742
void* GrowToNextCapacityAndPrepareInsert(
1743
    CommonFields& common, const PolicyFunctions& __restrict policy,
1744
177k
    size_t new_hash) {
1745
177k
  const size_t old_capacity = common.capacity();
1746
177k
  ABSL_SWISSTABLE_ASSERT(common.GetGrowthLeftTotalSlow(old_capacity) == 0);
1747
177k
  ABSL_SWISSTABLE_ASSERT(old_capacity > policy.soo_capacity());
1748
177k
  ABSL_SWISSTABLE_ASSERT(!IsSmallCapacity(old_capacity));
1749
177k
  ABSL_ASSUME(old_capacity > kMaxSmallCapacity);
1750
1751
177k
  const size_t new_capacity = NextCapacity(old_capacity);
1752
177k
  ctrl_t* old_ctrl = common.control();
1753
177k
  void* old_slots = common.slot_array(old_capacity);
1754
177k
  size_t old_blocked_element_count = common.blocked_element_count();
1755
1756
177k
  HashtablezInfoHandle infoz = common.infoz();
1757
177k
  const bool has_infoz = infoz.IsSampled();
1758
177k
  common.set_capacity(new_capacity);
1759
177k
  common.set_blocked_element_count_to_zero();
1760
177k
  const size_t slot_size = policy.slot_size;
1761
177k
  const size_t slot_align = policy.slot_align;
1762
177k
  void* alloc = policy.get_char_alloc(common);
1763
1764
177k
  const auto [new_ctrl, new_slots] =
1765
177k
      AllocBackingArray(common, policy, new_capacity, has_infoz, alloc,
1766
177k
                        /*blocked_element_count=*/0);
1767
177k
  common.set_control(new_ctrl);
1768
177k
  SanitizerPoisonMemoryRegion(new_slots, new_capacity * slot_size);
1769
1770
177k
  h2_t new_h2 = H2(new_hash);
1771
177k
  size_t total_probe_length = 0;
1772
177k
  FindInfo find_info;
1773
177k
  if (ABSL_PREDICT_TRUE(is_single_group(new_capacity))) {
1774
69.2k
    size_t offset;
1775
69.2k
    const size_t old_size = common.size();
1776
69.2k
    GrowIntoSingleGroupShuffleControlBytes(old_ctrl, old_capacity,
1777
69.2k
                                           old_blocked_element_count, new_ctrl,
1778
69.2k
                                           new_capacity);
1779
    // We put the new element either at the beginning or at the end of the
1780
    // table with approximately equal probability.
1781
69.2k
    offset = new_hash & 1 ? 0 : new_capacity - 1;
1782
1783
69.2k
    ABSL_SWISSTABLE_ASSERT(IsEmpty(new_ctrl[offset]));
1784
69.2k
    SetCtrlInSingleGroupTable(common, offset, new_h2, policy.slot_size);
1785
69.2k
    find_info = FindInfo{offset, 0};
1786
    // Single group tables have all slots full on resize. So we can transfer
1787
    // all slots without checking the control bytes.
1788
69.2k
    ABSL_SWISSTABLE_ASSERT(common.size() + old_blocked_element_count ==
1789
69.2k
                           old_capacity);
1790
69.2k
    void* target = NextSlot(new_slots, slot_size);
1791
69.2k
    SanitizerUnpoisonMemoryRegion(target, old_size * slot_size);
1792
69.2k
    policy.transfer_n(&common, target, old_slots, old_size);
1793
107k
  } else {
1794
107k
    total_probe_length =
1795
107k
        GrowToNextCapacityDispatch(common, policy, old_ctrl, old_slots);
1796
107k
    find_info = find_first_non_full(common, new_hash);
1797
107k
    SetCtrlInLargeTable(common, find_info.offset, new_h2, policy.slot_size);
1798
107k
  }
1799
177k
  ABSL_SWISSTABLE_ASSERT(old_capacity > policy.soo_capacity());
1800
177k
  DeallocBackingArrayImpl(alloc, old_capacity, old_ctrl, slot_size, slot_align,
1801
177k
                          has_infoz, old_blocked_element_count, policy.dealloc);
1802
177k
  PrepareInsertCommon(common);
1803
177k
  ResetGrowthLeft(new_capacity, common.size(), common);
1804
1805
177k
  if (ABSL_PREDICT_FALSE(has_infoz)) {
1806
0
    ReportGrowthToInfoz(common, infoz, new_hash, total_probe_length,
1807
0
                        find_info.probe_length);
1808
0
  }
1809
177k
  return SlotAddress(new_slots, find_info.offset, policy.slot_size);
1810
177k
}
1811
1812
}  // namespace
1813
1814
void* PrepareInsertSmallNonSoo(CommonFields& common,
1815
                               const PolicyFunctions& __restrict policy,
1816
200k
                               absl::FunctionRef<size_t(size_t)> get_hash) {
1817
200k
  ABSL_SWISSTABLE_ASSERT(common.is_small());
1818
200k
  ABSL_SWISSTABLE_ASSERT(!policy.soo_enabled);
1819
200k
  if (common.capacity() == 1) {
1820
166k
    if (common.empty()) {
1821
135k
      IncrementSmallSizeNonSoo(common, policy);
1822
135k
      if (common.has_infoz()) {
1823
0
        common.infoz().RecordInsertMiss(get_hash(common.seed().seed()),
1824
0
                                        /*distance_from_desired=*/0);
1825
0
      }
1826
135k
      return common.slot_array(/*capacity=*/1);
1827
135k
    } else {
1828
31.3k
      return Grow1To3AndPrepareInsert(common, policy, get_hash);
1829
31.3k
    }
1830
166k
  }
1831
1832
  // Growing from 0 to 1 capacity.
1833
34.0k
  ABSL_SWISSTABLE_ASSERT(common.capacity() == 0);
1834
34.0k
  constexpr size_t kNewCapacity = 1;
1835
1836
34.0k
  common.set_capacity(kNewCapacity);
1837
34.0k
  HashtablezInfoHandle infoz;
1838
34.0k
  const bool should_sample =
1839
34.0k
      policy.is_hashtablez_eligible && ShouldSampleNextTable();
1840
34.0k
  if (ABSL_PREDICT_FALSE(should_sample)) {
1841
0
    infoz = ForcedTrySample(policy.slot_size, policy.key_size,
1842
0
                            policy.value_size, policy.soo_capacity());
1843
0
  }
1844
34.0k
  const bool has_infoz = infoz.IsSampled();
1845
34.0k
  void* alloc = policy.get_char_alloc(common);
1846
1847
34.0k
  const auto [new_ctrl, new_slots] =
1848
34.0k
      AllocBackingArray(common, policy, kNewCapacity, has_infoz, alloc,
1849
34.0k
                        /*blocked_element_count=*/0);
1850
34.0k
  common.set_control(new_ctrl);
1851
1852
34.0k
  static_assert(NextCapacity(0) == 1);
1853
34.0k
  PrepareInsertCommon(common);
1854
1855
34.0k
  if (ABSL_PREDICT_FALSE(has_infoz)) {
1856
0
    common.generate_new_seed(/*has_infoz=*/true);
1857
0
    ReportSingleGroupTableGrowthToInfoz(common, infoz,
1858
0
                                        get_hash(common.seed().seed()));
1859
0
  }
1860
34.0k
  return new_slots;
1861
34.0k
}
1862
1863
namespace {
1864
1865
// Called whenever the table needs to vacate empty slots either by removing
1866
// tombstones via rehash or growth to next capacity.
1867
ABSL_ATTRIBUTE_NOINLINE
1868
void* RehashOrGrowToNextCapacityAndPrepareInsert(
1869
    CommonFields& common, const PolicyFunctions& __restrict policy,
1870
0
    size_t new_hash) {
1871
0
  ABSL_SWISSTABLE_ASSERT(!common.GetGrowthInfoLowerBound().HasNoDeleted());
1872
0
  const size_t cap = common.capacity();
1873
0
  ABSL_ASSUME(cap > 0);
1874
  // Do these calculations in 64-bit to avoid overflow.
1875
0
  if (common.size() * uint64_t{32} <=
1876
0
      (cap - kMaxBlockedElementsForLargeTables) * uint64_t{25}) {
1877
    // Squash DELETED without growing if there is enough capacity.
1878
    //
1879
    // Rehash in place if the current size is <= 25/32 of capacity.
1880
    // Rationale for such a high factor: 1) DropDeletesWithoutResize() is
1881
    // faster than resize, and 2) it takes quite a bit of work to add
1882
    // tombstones.  In the worst case, seems to take approximately 4
1883
    // insert/erase pairs to create a single tombstone and so if we are
1884
    // rehashing because of tombstones, we can afford to rehash-in-place as
1885
    // long as we are reclaiming at least 1/8 the capacity without doing more
1886
    // than 2X the work.  (Where "work" is defined to be size() for rehashing
1887
    // or rehashing in place, and 1 for an insert or erase.)  But rehashing in
1888
    // place is faster per operation than inserting or even doubling the size
1889
    // of the table, so we actually afford to reclaim even less space from a
1890
    // resize-in-place.  The decision is to rehash in place if we can reclaim
1891
    // at about 1/8th of the usable capacity (specifically 3/28 of the
1892
    // capacity) which means that the total cost of rehashing will be a small
1893
    // fraction of the total work.
1894
    //
1895
    // Here is output of an experiment using the BM_CacheInSteadyState
1896
    // benchmark running the old case (where we rehash-in-place only if we can
1897
    // reclaim at least 7/16*capacity) vs. this code (which rehashes in place
1898
    // if we can recover 3/32*capacity).
1899
    //
1900
    // Note that although in the worst-case number of rehashes jumped up from
1901
    // 15 to 190, but the number of operations per second is almost the same.
1902
    //
1903
    // Abridged output of running BM_CacheInSteadyState benchmark from
1904
    // raw_hash_set_benchmark.   N is the number of insert/erase operations.
1905
    //
1906
    //      | OLD (recover >= 7/16        | NEW (recover >= 3/32)
1907
    // size |    N/s LoadFactor NRehashes |    N/s LoadFactor NRehashes
1908
    //  448 | 145284       0.44        18 | 140118       0.44        19
1909
    //  493 | 152546       0.24        11 | 151417       0.48        28
1910
    //  538 | 151439       0.26        11 | 151152       0.53        38
1911
    //  583 | 151765       0.28        11 | 150572       0.57        50
1912
    //  628 | 150241       0.31        11 | 150853       0.61        66
1913
    //  672 | 149602       0.33        12 | 150110       0.66        90
1914
    //  717 | 149998       0.35        12 | 149531       0.70       129
1915
    //  762 | 149836       0.37        13 | 148559       0.74       190
1916
    //  807 | 149736       0.39        14 | 151107       0.39        14
1917
    //  852 | 150204       0.42        15 | 151019       0.42        15
1918
0
    return DropDeletesWithoutResizeAndPrepareInsert(common, policy, new_hash);
1919
0
  } else {
1920
    // Otherwise grow the container.
1921
0
    return GrowToNextCapacityAndPrepareInsert(common, policy, new_hash);
1922
0
  }
1923
0
}
1924
1925
// Slow path for PrepareInsertLarge that is called when the table has deleted
1926
// slots or need to be resized or rehashed.
1927
ABSL_ATTRIBUTE_NOINLINE
1928
void* PrepareInsertLargeSlow(CommonFields& common,
1929
                             const PolicyFunctions& __restrict policy,
1930
183k
                             size_t hash) {
1931
183k
  const size_t cap = common.capacity();
1932
183k
  ABSL_ASSUME(cap > kMaxSmallCapacity);
1933
183k
  GrowthInfoLowerBound growth_info_lower_bound =
1934
183k
      common.RebalanceGrowthLeftLowerBound(cap);
1935
183k
  if (ABSL_PREDICT_TRUE(
1936
183k
          growth_info_lower_bound.HasNoGrowthLeftAndNoDeleted())) {
1937
    // Table without deleted slots (>95% cases) that needs to be resized.
1938
177k
    return GrowToNextCapacityAndPrepareInsert(common, policy, hash);
1939
177k
  }
1940
6.00k
  if (ABSL_PREDICT_FALSE(
1941
6.00k
          growth_info_lower_bound.HasNoGrowthLeftAndHaveDeleted())) {
1942
    // Table with deleted slots that needs to be rehashed or resized.
1943
0
    return RehashOrGrowToNextCapacityAndPrepareInsert(common, policy, hash);
1944
0
  }
1945
  // Covers two cases:
1946
  // 1. Table with deleted slots that has space for the inserting element.
1947
  // 2. Table without deleted slots that has space and GrowthInfoView was
1948
  //    rebalanced.
1949
6.00k
  FindInfo target = find_first_non_full(common, hash);
1950
6.00k
  PrepareInsertCommon(common);
1951
6.00k
  common.OverwriteControlAsFull(common.control()[target.offset]);
1952
6.00k
  SetCtrlInLargeTable(common, target.offset, H2(hash), policy.slot_size);
1953
6.00k
  common.infoz().RecordInsertMiss(hash, target.probe_length);
1954
6.00k
  return SlotAddress(common.slot_array(cap), target.offset, policy.slot_size);
1955
6.00k
}
1956
1957
// Resizes empty non-allocated SOO table to NextCapacity(SooCapacity()),
1958
// forces the table to be sampled and prepares the insert.
1959
// SOO tables need to switch from SOO to heap in order to store the infoz.
1960
// Requires:
1961
//   1. `c.capacity() == SooCapacity()`.
1962
//   2. `c.empty()`.
1963
ABSL_ATTRIBUTE_NOINLINE void*
1964
GrowEmptySooTableToNextCapacityForceSamplingAndPrepareInsert(
1965
    CommonFields& common, const PolicyFunctions& __restrict policy,
1966
0
    absl::FunctionRef<size_t(size_t)> get_hash) {
1967
0
  const size_t kNewCapacity = NextCapacity(SooCapacity());
1968
0
  ResizeEmptyNonAllocatedTableImpl(common, policy, kNewCapacity,
1969
0
                                   /*blocked_element_count=*/0,
1970
0
                                   /*force_infoz=*/true);
1971
0
  PrepareInsertCommon(common);
1972
0
  common.OverwriteEmptyAsFull();
1973
0
  const size_t new_hash = get_hash(common.seed().seed());
1974
0
  SetCtrlInSingleGroupTable(common, SooSlotIndex(), H2(new_hash),
1975
0
                            policy.slot_size);
1976
0
  common.infoz().RecordInsertMiss(new_hash, /*distance_from_desired=*/0);
1977
0
  return SlotAddress(common.slot_array(kNewCapacity), SooSlotIndex(),
1978
0
                     policy.slot_size);
1979
0
}
1980
1981
// Returns the number of elements to block for the given capacity and reserved
1982
// size.
1983
size_t BlockedElementCountForReservedTable(size_t capacity,
1984
0
                                           size_t reserved_size) {
1985
0
  if (!IsCapacityValidForBlockedElements(capacity)) {
1986
0
    return 0;
1987
0
  }
1988
0
  const size_t blocked_elements = CapacityToGrowth(capacity) - reserved_size;
1989
0
  if (is_single_group(capacity)) {
1990
    // Single group tables never probes, so we can block all the slots.
1991
0
    return blocked_elements;
1992
0
  }
1993
0
  return (std::min)(blocked_elements, kMaxBlockedElementsForLargeTables);
1994
0
}
1995
1996
// Resizes empty non-allocated table to the capacity to fit new_size elements.
1997
// Requires:
1998
//   1. `c.capacity() == policy.soo_capacity()`.
1999
//   2. `c.empty()`.
2000
//   3. `new_size > policy.soo_capacity()`.
2001
// The table will be attempted to be sampled.
2002
void ReserveEmptyNonAllocatedTableToFitNewSize(
2003
    CommonFields& common, const PolicyFunctions& __restrict policy,
2004
0
    size_t new_size) {
2005
0
  ValidateMaxSize(new_size, policy.key_size, policy.slot_size);
2006
0
  ABSL_ASSUME(new_size > 0);
2007
0
  const size_t new_capacity = SizeToCapacity(new_size);
2008
0
  ResizeEmptyNonAllocatedTableImpl(
2009
0
      common, policy, new_capacity,
2010
0
      BlockedElementCountForReservedTable(new_capacity, new_size),
2011
0
      /*force_infoz=*/false);
2012
  // This is after resize, to ensure that we have completed the allocation
2013
  // and have potentially sampled the hashtable.
2014
0
  common.infoz().RecordReservation(new_size);
2015
0
}
2016
2017
// Type erased version of raw_hash_set::reserve for tables that have an
2018
// allocated backing array.
2019
//
2020
// Requires:
2021
//   1. `c.capacity() > policy.soo_capacity()` OR `!c.empty()`.
2022
// Reserving already allocated tables is considered to be a rare case.
2023
ABSL_ATTRIBUTE_NOINLINE void ReserveAllocatedTable(
2024
    CommonFields& common, const PolicyFunctions& __restrict policy,
2025
0
    size_t new_size) {
2026
0
  const size_t cap = common.capacity();
2027
0
  ValidateMaxSize(new_size, policy.key_size, policy.slot_size);
2028
0
  ABSL_ASSUME(new_size > 0);
2029
0
  const size_t new_capacity = SizeToCapacity(new_size);
2030
0
  if (cap == policy.soo_capacity()) {
2031
0
    ABSL_SWISSTABLE_ASSERT(!common.empty());
2032
0
    ResizeFullSooTable(common, policy, new_capacity,
2033
0
                       ResizeFullSooTableSamplingMode::kNoSampling);
2034
0
  } else {
2035
0
    ABSL_SWISSTABLE_ASSERT(cap > policy.soo_capacity());
2036
    // TODO(b/382423690): consider using GrowToNextCapacity, when applicable.
2037
0
    ResizeAllocatedTableWithSeedChange(common, policy, new_capacity);
2038
0
  }
2039
0
  common.infoz().RecordReservation(new_size);
2040
0
}
2041
2042
// As `ResizeFullSooTableToNextCapacity`, except that we also force the SOO
2043
// table to be sampled. SOO tables need to switch from SOO to heap in order to
2044
// store the infoz. No-op if sampling is disabled or not possible.
2045
void GrowFullSooTableToNextCapacityForceSampling(
2046
0
    CommonFields& common, const PolicyFunctions& __restrict policy) {
2047
0
  AssertFullSoo(common, policy);
2048
0
  ResizeFullSooTable(
2049
0
      common, policy, NextCapacity(SooCapacity()),
2050
0
      ResizeFullSooTableSamplingMode::kForceSampleNoResizeIfUnsampled);
2051
0
}
2052
2053
}  // namespace
2054
2055
324k
void* GetRefForEmptyClass(CommonFields& common) {
2056
  // Empty base optimization typically make the empty base class address to be
2057
  // the same as the first address of the derived class object.
2058
  // But we generally assume that for empty classes we can return any valid
2059
  // pointer.
2060
324k
  return &common;
2061
324k
}
2062
2063
void ResizeAllocatedTableWithSeedChange(
2064
    CommonFields& common, const PolicyFunctions& __restrict policy,
2065
0
    size_t new_capacity) {
2066
0
  ABSL_SWISSTABLE_ASSERT(IsValidCapacity(new_capacity));
2067
0
  ABSL_SWISSTABLE_ASSERT(new_capacity > policy.soo_capacity());
2068
2069
0
  const size_t old_capacity = common.capacity();
2070
0
  ctrl_t* const old_ctrl = common.control();
2071
0
  void* const old_slots = common.slot_array(old_capacity);
2072
0
  const size_t old_blocked_element_count = common.blocked_element_count();
2073
2074
0
  const size_t slot_size = policy.slot_size;
2075
0
  const size_t slot_align = policy.slot_align;
2076
0
  HashtablezInfoHandle infoz = common.infoz();
2077
0
  const bool has_infoz = infoz.IsSampled();
2078
0
  void* alloc = policy.get_char_alloc(common);
2079
2080
0
  common.set_capacity(new_capacity);
2081
0
  common.set_blocked_element_count_to_zero();
2082
0
  const auto [new_ctrl, new_slots] =
2083
0
      AllocBackingArray(common, policy, new_capacity, has_infoz, alloc,
2084
0
                        /*blocked_element_count=*/0);
2085
0
  common.set_control(new_ctrl);
2086
0
  common.generate_new_seed(has_infoz);
2087
2088
0
  size_t total_probe_length = 0;
2089
0
  ResetCtrl(common, slot_size, /*blocked_element_count=*/0);
2090
0
  ABSL_SWISSTABLE_ASSERT(old_capacity > 0);
2091
0
  total_probe_length = FindNewPositionsAndTransferSlots(
2092
0
      common, policy, old_ctrl, old_slots, old_capacity);
2093
0
  DeallocBackingArrayImpl(alloc, old_capacity, old_ctrl, slot_size, slot_align,
2094
0
                          has_infoz, old_blocked_element_count, policy.dealloc);
2095
0
  ResetGrowthLeft(new_capacity, common.size(), common);
2096
2097
0
  if (ABSL_PREDICT_FALSE(has_infoz)) {
2098
0
    ReportResizeToInfoz(common, infoz, total_probe_length);
2099
0
  }
2100
0
}
2101
2102
// Resizes a full SOO table to the NextCapacity(SooCapacity()).
2103
template <size_t SooSlotMemcpySize, bool TransferUsesMemcpy>
2104
void* GrowSooTableToNextCapacityAndPrepareInsert(
2105
    CommonFields& common, const PolicyFunctions& __restrict policy,
2106
25.2k
    absl::FunctionRef<size_t(size_t)> get_hash, bool force_sampling) {
2107
25.2k
  AssertSoo(common, policy);
2108
25.2k
  if (ABSL_PREDICT_FALSE(force_sampling)) {
2109
    // The table is empty, it is only used for forced sampling of SOO tables.
2110
0
    return GrowEmptySooTableToNextCapacityForceSamplingAndPrepareInsert(
2111
0
        common, policy, get_hash);
2112
0
  }
2113
25.2k
  ABSL_SWISSTABLE_ASSERT(common.size() == policy.soo_capacity());
2114
25.2k
  static constexpr size_t kNewCapacity = NextCapacity(SooCapacity());
2115
25.2k
  const size_t slot_size = policy.slot_size;
2116
25.2k
  void* alloc = policy.get_char_alloc(common);
2117
25.2k
  common.set_capacity(kNewCapacity);
2118
2119
  // Since the table is not empty, it will not be sampled.
2120
  // The decision to sample was already made during the first insertion.
2121
  //
2122
  // We do not set control and slots in CommonFields yet to avoid overriding
2123
  // SOO data.
2124
25.2k
  const auto [new_ctrl, new_slots] = AllocBackingArray(
2125
25.2k
      common, policy, kNewCapacity, /*has_infoz=*/false, alloc,
2126
25.2k
      /*blocked_element_count=*/0);
2127
2128
25.2k
  PrepareInsertCommon(common);
2129
25.2k
  ABSL_SWISSTABLE_ASSERT(common.size() == 2);
2130
25.2k
  common.InitGrowthLeftNoDeleted(kNewCapacity - 2, kNewCapacity);
2131
25.2k
  common.generate_new_seed(/*has_infoz=*/false);
2132
25.2k
  const h2_t soo_slot_h2 = H2(policy.hash_slot(
2133
25.2k
      policy.hash_fn(common), common.soo_data(), common.seed().seed()));
2134
25.2k
  const size_t new_hash = get_hash(common.seed().seed());
2135
2136
25.2k
  const size_t offset = Resize1To3NewOffset(new_hash);
2137
25.2k
  InitializeThreeElementsControlBytes(soo_slot_h2, H2(new_hash), offset,
2138
25.2k
                                      new_ctrl);
2139
2140
25.2k
  SanitizerPoisonMemoryRegion(new_slots, slot_size * kNewCapacity);
2141
25.2k
  void* target_slot = SlotAddress(new_slots, SooSlotIndex(), slot_size);
2142
25.2k
  SanitizerUnpoisonMemoryRegion(target_slot, slot_size);
2143
25.2k
  if constexpr (TransferUsesMemcpy) {
2144
    // Target slot is placed at index 1, but capacity is at
2145
    // minimum 3. So we are allowed to copy at least twice as much
2146
    // memory.
2147
25.2k
    static_assert(SooSlotIndex() == 1);
2148
25.2k
    static_assert(SooSlotMemcpySize > 0);
2149
25.2k
    static_assert(SooSlotMemcpySize <= MaxSooSlotSize());
2150
25.2k
    ABSL_SWISSTABLE_ASSERT(SooSlotMemcpySize <= 2 * slot_size);
2151
25.2k
    ABSL_SWISSTABLE_ASSERT(SooSlotMemcpySize >= slot_size);
2152
25.2k
    void* next_slot = SlotAddress(target_slot, 1, slot_size);
2153
25.2k
    SanitizerUnpoisonMemoryRegion(next_slot, SooSlotMemcpySize - slot_size);
2154
25.2k
    std::memcpy(target_slot, common.soo_data(), SooSlotMemcpySize);
2155
25.2k
    SanitizerPoisonMemoryRegion(next_slot, SooSlotMemcpySize - slot_size);
2156
25.2k
  } else {
2157
0
    static_assert(SooSlotMemcpySize == 0);
2158
0
    policy.transfer_n(&common, target_slot, common.soo_data(), 1);
2159
0
  }
2160
0
  common.set_control(new_ctrl);
2161
2162
  // Full SOO table couldn't be sampled. If SOO table is sampled, it would
2163
  // have been resized to the next capacity.
2164
25.2k
  ABSL_SWISSTABLE_ASSERT(!common.infoz().IsSampled());
2165
25.2k
  void* new_slot = SlotAddress(new_slots, offset, slot_size);
2166
25.2k
  SanitizerUnpoisonMemoryRegion(new_slot, slot_size);
2167
25.2k
  return new_slot;
2168
25.2k
}
Unexecuted instantiation: void* absl::container_internal::GrowSooTableToNextCapacityAndPrepareInsert<0ul, false>(absl::container_internal::CommonFields&, absl::container_internal::PolicyFunctions const&, absl::FunctionRef<unsigned long (unsigned long)>, bool)
Unexecuted instantiation: void* absl::container_internal::GrowSooTableToNextCapacityAndPrepareInsert<1ul, true>(absl::container_internal::CommonFields&, absl::container_internal::PolicyFunctions const&, absl::FunctionRef<unsigned long (unsigned long)>, bool)
Unexecuted instantiation: void* absl::container_internal::GrowSooTableToNextCapacityAndPrepareInsert<4ul, true>(absl::container_internal::CommonFields&, absl::container_internal::PolicyFunctions const&, absl::FunctionRef<unsigned long (unsigned long)>, bool)
void* absl::container_internal::GrowSooTableToNextCapacityAndPrepareInsert<8ul, true>(absl::container_internal::CommonFields&, absl::container_internal::PolicyFunctions const&, absl::FunctionRef<unsigned long (unsigned long)>, bool)
Line
Count
Source
2106
25.2k
    absl::FunctionRef<size_t(size_t)> get_hash, bool force_sampling) {
2107
25.2k
  AssertSoo(common, policy);
2108
25.2k
  if (ABSL_PREDICT_FALSE(force_sampling)) {
2109
    // The table is empty, it is only used for forced sampling of SOO tables.
2110
0
    return GrowEmptySooTableToNextCapacityForceSamplingAndPrepareInsert(
2111
0
        common, policy, get_hash);
2112
0
  }
2113
25.2k
  ABSL_SWISSTABLE_ASSERT(common.size() == policy.soo_capacity());
2114
25.2k
  static constexpr size_t kNewCapacity = NextCapacity(SooCapacity());
2115
25.2k
  const size_t slot_size = policy.slot_size;
2116
25.2k
  void* alloc = policy.get_char_alloc(common);
2117
25.2k
  common.set_capacity(kNewCapacity);
2118
2119
  // Since the table is not empty, it will not be sampled.
2120
  // The decision to sample was already made during the first insertion.
2121
  //
2122
  // We do not set control and slots in CommonFields yet to avoid overriding
2123
  // SOO data.
2124
25.2k
  const auto [new_ctrl, new_slots] = AllocBackingArray(
2125
25.2k
      common, policy, kNewCapacity, /*has_infoz=*/false, alloc,
2126
25.2k
      /*blocked_element_count=*/0);
2127
2128
25.2k
  PrepareInsertCommon(common);
2129
25.2k
  ABSL_SWISSTABLE_ASSERT(common.size() == 2);
2130
25.2k
  common.InitGrowthLeftNoDeleted(kNewCapacity - 2, kNewCapacity);
2131
25.2k
  common.generate_new_seed(/*has_infoz=*/false);
2132
25.2k
  const h2_t soo_slot_h2 = H2(policy.hash_slot(
2133
25.2k
      policy.hash_fn(common), common.soo_data(), common.seed().seed()));
2134
25.2k
  const size_t new_hash = get_hash(common.seed().seed());
2135
2136
25.2k
  const size_t offset = Resize1To3NewOffset(new_hash);
2137
25.2k
  InitializeThreeElementsControlBytes(soo_slot_h2, H2(new_hash), offset,
2138
25.2k
                                      new_ctrl);
2139
2140
25.2k
  SanitizerPoisonMemoryRegion(new_slots, slot_size * kNewCapacity);
2141
25.2k
  void* target_slot = SlotAddress(new_slots, SooSlotIndex(), slot_size);
2142
25.2k
  SanitizerUnpoisonMemoryRegion(target_slot, slot_size);
2143
25.2k
  if constexpr (TransferUsesMemcpy) {
2144
    // Target slot is placed at index 1, but capacity is at
2145
    // minimum 3. So we are allowed to copy at least twice as much
2146
    // memory.
2147
25.2k
    static_assert(SooSlotIndex() == 1);
2148
25.2k
    static_assert(SooSlotMemcpySize > 0);
2149
25.2k
    static_assert(SooSlotMemcpySize <= MaxSooSlotSize());
2150
25.2k
    ABSL_SWISSTABLE_ASSERT(SooSlotMemcpySize <= 2 * slot_size);
2151
25.2k
    ABSL_SWISSTABLE_ASSERT(SooSlotMemcpySize >= slot_size);
2152
25.2k
    void* next_slot = SlotAddress(target_slot, 1, slot_size);
2153
25.2k
    SanitizerUnpoisonMemoryRegion(next_slot, SooSlotMemcpySize - slot_size);
2154
25.2k
    std::memcpy(target_slot, common.soo_data(), SooSlotMemcpySize);
2155
25.2k
    SanitizerPoisonMemoryRegion(next_slot, SooSlotMemcpySize - slot_size);
2156
  } else {
2157
    static_assert(SooSlotMemcpySize == 0);
2158
    policy.transfer_n(&common, target_slot, common.soo_data(), 1);
2159
  }
2160
0
  common.set_control(new_ctrl);
2161
2162
  // Full SOO table couldn't be sampled. If SOO table is sampled, it would
2163
  // have been resized to the next capacity.
2164
25.2k
  ABSL_SWISSTABLE_ASSERT(!common.infoz().IsSampled());
2165
25.2k
  void* new_slot = SlotAddress(new_slots, offset, slot_size);
2166
25.2k
  SanitizerUnpoisonMemoryRegion(new_slot, slot_size);
2167
25.2k
  return new_slot;
2168
25.2k
}
2169
2170
void Rehash(CommonFields& common, const PolicyFunctions& __restrict policy,
2171
0
            size_t n) {
2172
0
  const size_t cap = common.capacity();
2173
2174
0
  auto clear_backing_array = [&]() {
2175
0
    ClearBackingArrayNoReuse(common, policy, policy.get_char_alloc(common));
2176
0
  };
2177
2178
0
  const size_t slot_size = policy.slot_size;
2179
2180
0
  if (n == 0) {
2181
0
    if (cap <= policy.soo_capacity()) return;
2182
0
    if (common.empty()) {
2183
0
      clear_backing_array();
2184
0
      return;
2185
0
    }
2186
0
    if (common.size() <= policy.soo_capacity()) {
2187
      // When the table is already sampled, we keep it sampled.
2188
0
      if (common.infoz().IsSampled()) {
2189
0
        static constexpr size_t kInitialSampledCapacity =
2190
0
            NextCapacity(SooCapacity());
2191
0
        if (cap > kInitialSampledCapacity) {
2192
0
          ResizeAllocatedTableWithSeedChange(common, policy,
2193
0
                                             kInitialSampledCapacity);
2194
0
        }
2195
        // This asserts that we didn't lose sampling coverage in `resize`.
2196
0
        ABSL_SWISSTABLE_ASSERT(common.infoz().IsSampled());
2197
0
        return;
2198
0
      }
2199
0
      ABSL_SWISSTABLE_ASSERT(slot_size <= sizeof(HeapOrSoo));
2200
0
      ABSL_SWISSTABLE_ASSERT(policy.slot_align <= alignof(HeapOrSoo));
2201
0
      HeapOrSoo tmp_slot;
2202
0
      size_t begin_offset = FindFirstFullSlot(0, cap, common.control());
2203
0
      policy.transfer_n(
2204
0
          &common, &tmp_slot,
2205
0
          SlotAddress(common.slot_array(cap), begin_offset, slot_size), 1);
2206
0
      clear_backing_array();
2207
0
      policy.transfer_n(&common, common.soo_data(), &tmp_slot, 1);
2208
0
      common.set_full_soo();
2209
0
      return;
2210
0
    }
2211
0
  }
2212
2213
  // bitor is a faster way of doing `max` here. We will round up to the next
2214
  // power-of-2-minus-1, so bitor is good enough.
2215
0
  const size_t new_capacity =
2216
0
      NormalizeCapacity(n | SizeToCapacity(common.size()));
2217
0
  ValidateMaxCapacity(new_capacity, policy.key_size, policy.slot_size);
2218
  // n == 0 unconditionally rehashes as per the standard.
2219
0
  if (n == 0 || new_capacity > cap) {
2220
0
    if (cap == policy.soo_capacity()) {
2221
0
      if (common.empty()) {
2222
0
        ResizeEmptyNonAllocatedTableImpl(common, policy, new_capacity,
2223
0
                                         /*blocked_element_count=*/0,
2224
0
                                         /*force_infoz=*/false);
2225
0
      } else {
2226
0
        ResizeFullSooTable(common, policy, new_capacity,
2227
0
                           ResizeFullSooTableSamplingMode::kNoSampling);
2228
0
      }
2229
0
    } else {
2230
0
      ResizeAllocatedTableWithSeedChange(common, policy, new_capacity);
2231
0
    }
2232
    // This is after resize, to ensure that we have completed the allocation
2233
    // and have potentially sampled the hashtable.
2234
0
    common.infoz().RecordReservation(n);
2235
0
  }
2236
0
}
2237
2238
void Copy(CommonFields& common, const PolicyFunctions& __restrict policy,
2239
          const CommonFields& other,
2240
0
          absl::FunctionRef<void(void*, const void*)> copy_fn) {
2241
0
  const size_t size = other.size();
2242
0
  ABSL_SWISSTABLE_ASSERT(size > 0);
2243
0
  const size_t soo_capacity = policy.soo_capacity();
2244
0
  const size_t slot_size = policy.slot_size;
2245
0
  const bool soo_enabled = policy.soo_enabled;
2246
0
  if (size == 1) {
2247
0
    if (!soo_enabled) {
2248
0
      ReserveEmptyNonAllocatedTableToFitNewSize(common, policy, 1);
2249
0
      common.infoz().RecordStorageChanged(1, 1);
2250
0
    }
2251
0
    IncrementSmallSize(common, policy);
2252
0
    const size_t other_capacity = other.capacity();
2253
0
    const void* other_slot =
2254
0
        other_capacity <= soo_capacity ? other.soo_data()
2255
0
        : IsSmallCapacity(other_capacity)
2256
0
            ? other.slot_array(other_capacity)
2257
0
            : SlotAddress(other.slot_array(other_capacity),
2258
0
                          FindFirstFullSlot(0, other_capacity, other.control()),
2259
0
                          slot_size);
2260
0
    copy_fn(soo_enabled ? common.soo_data()
2261
0
                        : SingleSlotAddress</*kSooEnabled=*/false>(common),
2262
0
            other_slot);
2263
2264
0
    if (soo_enabled && policy.is_hashtablez_eligible &&
2265
0
        ShouldSampleNextTable()) {
2266
0
      GrowFullSooTableToNextCapacityForceSampling(common, policy);
2267
0
    }
2268
0
    return;
2269
0
  }
2270
2271
0
  ReserveTableToFitNewSize(common, policy, size);
2272
0
  const size_t blocked_element_count = common.blocked_element_count();
2273
0
  auto infoz = common.infoz();
2274
0
  ABSL_SWISSTABLE_ASSERT(other.capacity() > soo_capacity);
2275
0
  const size_t cap = common.capacity();
2276
0
  ABSL_SWISSTABLE_ASSERT(cap > soo_capacity);
2277
0
  ABSL_ASSUME(cap > kMaxSmallCapacity);
2278
0
  size_t offset = cap;
2279
0
  const void* hash_fn = policy.hash_fn(common);
2280
0
  auto hasher = policy.hash_slot;
2281
0
  const size_t seed = common.seed().seed();
2282
0
  void* target_slot_array = common.slot_array(cap);
2283
0
  IterateOverFullSlotsImpl(
2284
0
      other, slot_size, [&](const ctrl_t*, void* that_slot) {
2285
        // The table is guaranteed to be empty, so we can do faster than
2286
        // a full `insert`.
2287
0
        const size_t hash = (*hasher)(hash_fn, that_slot, seed);
2288
0
        FindInfo target = find_first_non_full(common, hash);
2289
0
        infoz.RecordInsertMiss(hash, target.probe_length);
2290
0
        offset = target.offset;
2291
0
        SetCtrl(common, offset, H2(hash), slot_size);
2292
0
        copy_fn(SlotAddress(target_slot_array, offset, slot_size), that_slot);
2293
0
        common.maybe_increment_generation_on_insert();
2294
0
      });
2295
0
  common.increment_size(size);
2296
0
  ResetGrowthLeft(cap, size + blocked_element_count, common);
2297
0
}
2298
2299
void ReserveTableToFitNewSize(CommonFields& common,
2300
                              const PolicyFunctions& __restrict policy,
2301
0
                              size_t new_size) {
2302
0
  new_size =
2303
0
      std::min(new_size, MaxValidSize(policy.key_size, policy.slot_size));
2304
0
  common.reset_reserved_growth(new_size);
2305
0
  common.set_reservation_size(new_size);
2306
0
  ABSL_SWISSTABLE_ASSERT(new_size > policy.soo_capacity());
2307
0
  const size_t cap = common.capacity();
2308
0
  if (ABSL_PREDICT_TRUE(common.empty() && cap <= policy.soo_capacity())) {
2309
0
    return ReserveEmptyNonAllocatedTableToFitNewSize(common, policy, new_size);
2310
0
  }
2311
2312
0
  ABSL_SWISSTABLE_ASSERT(!common.empty() || cap > policy.soo_capacity());
2313
0
  ABSL_SWISSTABLE_ASSERT(cap > 0);
2314
0
  const size_t max_size_before_growth =
2315
0
      IsSmallCapacity(cap) ? cap
2316
0
                           : common.size() + common.GetGrowthLeftTotalSlow(cap);
2317
0
  if (new_size <= max_size_before_growth) {
2318
0
    return;
2319
0
  }
2320
0
  ReserveAllocatedTable(common, policy, new_size);
2321
0
}
2322
2323
namespace {
2324
void* PrepareInsertLargeImpl(CommonFields& common,
2325
                             const PolicyFunctions& __restrict policy,
2326
                             size_t hash,
2327
                             Group::NonIterableBitMaskType mask_empty,
2328
10.8M
                             FindInfo target_group) {
2329
10.8M
  ABSL_SWISSTABLE_ASSERT(!common.is_small());
2330
  // When there are no deleted slots in the table
2331
  // and growth_left is positive, we can insert at the first
2332
  // empty slot in the probe sequence (target).
2333
10.8M
  if (ABSL_PREDICT_FALSE(
2334
10.8M
          !common.GetGrowthInfoLowerBound().HasNoDeletedAndGrowthLeft())) {
2335
183k
    return PrepareInsertLargeSlow(common, policy, hash);
2336
183k
  }
2337
10.6M
  PrepareInsertCommon(common);
2338
10.6M
  common.OverwriteEmptyAsFull();
2339
10.6M
  const size_t cap = common.capacity();
2340
10.6M
  ABSL_ASSUME(cap > kMaxSmallCapacity);
2341
10.6M
  target_group.offset += mask_empty.LowestBitSet();
2342
10.6M
  target_group.offset &= cap;
2343
10.6M
  SetCtrl(common, target_group.offset, H2(hash), policy.slot_size);
2344
10.6M
  common.infoz().RecordInsertMiss(hash, target_group.probe_length);
2345
10.6M
  return SlotAddress(common.slot_array(cap), target_group.offset,
2346
10.6M
                     policy.slot_size);
2347
10.6M
}
2348
}  // namespace
2349
2350
void* PrepareInsertLarge(CommonFields& common,
2351
                         const PolicyFunctions& __restrict policy, size_t hash,
2352
                         Group::NonIterableBitMaskType mask_empty,
2353
10.8M
                         FindInfo target_group) {
2354
  // NOLINTNEXTLINE(misc-static-assert)
2355
10.8M
  ABSL_SWISSTABLE_ASSERT(!SwisstableGenerationsEnabled());
2356
10.8M
  return PrepareInsertLargeImpl(common, policy, hash, mask_empty, target_group);
2357
10.8M
}
2358
2359
void* PrepareInsertLargeGenerationsEnabled(
2360
    CommonFields& common, const PolicyFunctions& __restrict policy, size_t hash,
2361
    Group::NonIterableBitMaskType mask_empty, FindInfo target_group,
2362
0
    absl::FunctionRef<size_t(size_t)> recompute_hash) {
2363
  // NOLINTNEXTLINE(misc-static-assert)
2364
0
  ABSL_SWISSTABLE_ASSERT(SwisstableGenerationsEnabled());
2365
0
  const size_t cap = common.capacity();
2366
0
  const size_t growth_left = common.GetGrowthLeftTotalSlow(cap);
2367
  // As an optimization, we avoid calling ShouldRehashForBugDetection if we
2368
  // will end up rehashing anyways.
2369
0
  if (growth_left > 0 && common.should_rehash_for_bug_detection_on_insert()) {
2370
    // Move to a different heap allocation in order to detect bugs.
2371
0
    ResizeAllocatedTableWithSeedChange(common, policy, cap);
2372
0
    hash = recompute_hash(common.seed().seed());
2373
0
    std::tie(target_group, mask_empty) =
2374
0
        find_first_non_full_group(common, hash);
2375
0
  }
2376
0
  return PrepareInsertLargeImpl(common, policy, hash, mask_empty, target_group);
2377
0
}
2378
2379
namespace {
2380
// Returns true if the following is true
2381
// 1. OptimalMemcpySizeForSooSlotTransfer(left) >
2382
//    OptimalMemcpySizeForSooSlotTransfer(left - 1)
2383
// 2. OptimalMemcpySizeForSooSlotTransfer(left) are equal for all i in [left,
2384
// right].
2385
// This function is used to verify that we have all the possible template
2386
// instantiations for GrowFullSooTableToNextCapacity.
2387
// With this verification the problem may be detected at compile time instead of
2388
// link time.
2389
constexpr bool VerifyOptimalMemcpySizeForSooSlotTransferRange(size_t left,
2390
0
                                                              size_t right) {
2391
0
  size_t optimal_size_for_range = OptimalMemcpySizeForSooSlotTransfer(left);
2392
0
  if (optimal_size_for_range <= OptimalMemcpySizeForSooSlotTransfer(left - 1)) {
2393
0
    return false;
2394
0
  }
2395
0
  for (size_t i = left + 1; i <= right; ++i) {
2396
0
    if (OptimalMemcpySizeForSooSlotTransfer(i) != optimal_size_for_range) {
2397
0
      return false;
2398
0
    }
2399
0
  }
2400
0
  return true;
2401
0
}
2402
}  // namespace
2403
2404
// Extern template instantiation for inline function.
2405
template size_t TryFindNewIndexWithoutProbing(size_t h1, size_t old_index,
2406
                                              size_t old_capacity,
2407
                                              ctrl_t* new_ctrl,
2408
                                              size_t new_capacity);
2409
2410
// We need to instantiate ALL possible template combinations because we define
2411
// the function in the cc file.
2412
template void* GrowSooTableToNextCapacityAndPrepareInsert<0, false>(
2413
    CommonFields&, const PolicyFunctions&, absl::FunctionRef<size_t(size_t)>,
2414
    bool);
2415
template void* GrowSooTableToNextCapacityAndPrepareInsert<
2416
    OptimalMemcpySizeForSooSlotTransfer(1), true>(
2417
    CommonFields&, const PolicyFunctions&, absl::FunctionRef<size_t(size_t)>,
2418
    bool);
2419
2420
static_assert(VerifyOptimalMemcpySizeForSooSlotTransferRange(2, 3));
2421
template void* GrowSooTableToNextCapacityAndPrepareInsert<
2422
    OptimalMemcpySizeForSooSlotTransfer(3), true>(
2423
    CommonFields&, const PolicyFunctions&, absl::FunctionRef<size_t(size_t)>,
2424
    bool);
2425
2426
#if UINTPTR_MAX == UINT32_MAX
2427
static_assert(MaxSooSlotSize() == 4);
2428
static_assert(VerifyOptimalMemcpySizeForSooSlotTransferRange(2, 4));
2429
#else
2430
static_assert(VerifyOptimalMemcpySizeForSooSlotTransferRange(4, 8));
2431
template void* GrowSooTableToNextCapacityAndPrepareInsert<
2432
    OptimalMemcpySizeForSooSlotTransfer(8), true>(
2433
    CommonFields&, const PolicyFunctions&, absl::FunctionRef<size_t(size_t)>,
2434
    bool);
2435
static_assert(MaxSooSlotSize() == 8);
2436
#endif
2437
2438
template void* AllocateBackingArray<kStandardBackingArrayAlignment,
2439
                                    std::allocator<char>>(void* alloc,
2440
                                                          size_t n);
2441
template void DeallocateBackingArray<kStandardBackingArrayAlignment,
2442
                                     std::allocator<char>>(void* alloc,
2443
                                                           void* backing_array,
2444
                                                           size_t n);
2445
2446
template void Clear</*kSooEnabled=*/true>(CommonFields& c,
2447
                                          const PolicyFunctions& policy,
2448
                                          DestroySlotFn destroy_slot,
2449
                                          void* alloc);
2450
template void Clear</*kSooEnabled=*/false>(CommonFields& c,
2451
                                           const PolicyFunctions& policy,
2452
                                           DestroySlotFn destroy_slot,
2453
                                           void* alloc);
2454
2455
template void Destruct</*kSooEnabled=*/true>(CommonFields& c,
2456
                                             const DtorPolicy& policy,
2457
                                             DeallocBackingArrayFn dealloc,
2458
                                             void* alloc);
2459
template void Destruct</*kSooEnabled=*/true>(CommonFields& c,
2460
                                             const DtorPolicy& policy,
2461
                                             DeallocBackingArrayFn dealloc);
2462
template void Destruct</*kSooEnabled=*/true>(CommonFields& c,
2463
                                             const DtorPolicy& policy);
2464
template void Destruct</*kSooEnabled=*/false>(CommonFields& c,
2465
                                              const DtorPolicy& policy,
2466
                                              DeallocBackingArrayFn dealloc,
2467
                                              void* alloc);
2468
template void Destruct</*kSooEnabled=*/false>(CommonFields& c,
2469
                                              const DtorPolicy& policy,
2470
                                              DeallocBackingArrayFn dealloc);
2471
template void Destruct</*kSooEnabled=*/false>(CommonFields& c,
2472
                                              const DtorPolicy& policy);
2473
2474
}  // namespace container_internal
2475
ABSL_NAMESPACE_END
2476
}  // namespace absl