Coverage Report

Created: 2025-07-11 06:37

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