Coverage Report

Created: 2026-05-30 06:40

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