Coverage Report

Created: 2025-07-09 06:39

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