Skip to content

Commit c65fa83

Browse files
goldvitalycopybara-github
authored andcommitted
Rename policy.transfer to policy.transfer_n.
Also added `ABSL_PREDICT_*` to `GrowToNextCapacityAndPrepareInsert` for branches known to be common/uncommon. PiperOrigin-RevId: 742722991 Change-Id: Ib7f30ff71adb3824752ff35e779c5fbb2d060820
1 parent bcf4bf3 commit c65fa83

File tree

2 files changed

+27
-27
lines changed

2 files changed

+27
-27
lines changed

absl/container/internal/raw_hash_set.cc

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ size_t DropDeletesWithoutResizeAndPrepareInsert(CommonFields& common,
292292
ConvertDeletedToEmptyAndFullToDeleted(ctrl, capacity);
293293
const void* hash_fn = policy.hash_fn(common);
294294
auto hasher = policy.hash_slot;
295-
auto transfer = policy.transfer;
295+
auto transfer_n = policy.transfer_n;
296296
const size_t slot_size = policy.slot_size;
297297

298298
size_t total_probe_length = 0;
@@ -337,7 +337,7 @@ size_t DropDeletesWithoutResizeAndPrepareInsert(CommonFields& common,
337337
// SetCtrl poisons/unpoisons the slots so we have to call it at the
338338
// right time.
339339
SetCtrlInLargeTable(common, new_i, h2, slot_size);
340-
(*transfer)(set, new_slot_ptr, slot_ptr, 1);
340+
(*transfer_n)(set, new_slot_ptr, slot_ptr, 1);
341341
SetCtrlInLargeTable(common, i, ctrl_t::kEmpty, slot_size);
342342
// Initialize or change empty space id.
343343
tmp_space_id = i;
@@ -353,9 +353,9 @@ size_t DropDeletesWithoutResizeAndPrepareInsert(CommonFields& common,
353353
SanitizerUnpoisonMemoryRegion(tmp_space, slot_size);
354354

355355
// Swap i and new_i elements.
356-
(*transfer)(set, tmp_space, new_slot_ptr, 1);
357-
(*transfer)(set, new_slot_ptr, slot_ptr, 1);
358-
(*transfer)(set, slot_ptr, tmp_space, 1);
356+
(*transfer_n)(set, tmp_space, new_slot_ptr, 1);
357+
(*transfer_n)(set, new_slot_ptr, slot_ptr, 1);
358+
(*transfer_n)(set, slot_ptr, tmp_space, 1);
359359

360360
SanitizerPoisonMemoryRegion(tmp_space, slot_size);
361361

@@ -623,7 +623,7 @@ void InsertOldSooSlotAndInitializeControlBytes(CommonFields& c,
623623
SanitizerPoisonMemoryRegion(new_slots, policy.slot_size * new_capacity);
624624
void* target_slot = SlotAddress(new_slots, offset, policy.slot_size);
625625
SanitizerUnpoisonMemoryRegion(target_slot, policy.slot_size);
626-
policy.transfer(&c, target_slot, c.soo_data(), 1);
626+
policy.transfer_n(&c, target_slot, c.soo_data(), 1);
627627
c.set_control</*kGenerateSeed=*/false>(new_ctrl);
628628
c.set_slots(new_slots);
629629
ResetCtrl(c, policy.slot_size);
@@ -863,14 +863,14 @@ size_t GrowToNextCapacityAndPrepareInsert(CommonFields& common,
863863
common.generate_new_seed();
864864
find_info = FindInfo{0, 0};
865865
} else {
866-
if (is_single_group(new_capacity)) {
866+
if (ABSL_PREDICT_TRUE(is_single_group(new_capacity))) {
867867
GrowIntoSingleGroupShuffleControlBytes(old_ctrl, old_capacity, new_ctrl,
868868
new_capacity);
869-
// Single group tables have no deleted slots, so we can transfer all slots
870-
// without checking the control bytes.
869+
// Single group tables have all slots full on resize. So we can transfer
870+
// all slots without checking the control bytes.
871871
assert(common.size() == old_capacity);
872-
policy.transfer(&common, NextSlot(new_slots, slot_size), old_slots,
873-
old_capacity);
872+
policy.transfer_n(&common, NextSlot(new_slots, slot_size), old_slots,
873+
old_capacity);
874874
PoisonEmptySlots(common, slot_size);
875875
// We put the new element either at the beginning or at the end of the
876876
// table with approximately equal probability.
@@ -895,7 +895,7 @@ size_t GrowToNextCapacityAndPrepareInsert(CommonFields& common,
895895
PrepareInsertCommon(common);
896896
ResetGrowthLeft(common);
897897

898-
if (has_infoz) {
898+
if (ABSL_PREDICT_FALSE(has_infoz)) {
899899
common.set_has_infoz();
900900
infoz.RecordStorageChanged(common.size() - 1, new_capacity);
901901
infoz.RecordRehash(total_probe_length);
@@ -1083,7 +1083,7 @@ void GrowFullSooTableToNextCapacity(CommonFields& common,
10831083
SanitizerPoisonMemoryRegion(next_slot, SooSlotMemcpySize - slot_size);
10841084
} else {
10851085
static_assert(SooSlotMemcpySize == 0);
1086-
policy.transfer(&common, target_slot, common.soo_data(), 1);
1086+
policy.transfer_n(&common, target_slot, common.soo_data(), 1);
10871087
}
10881088
common.set_control</*kGenerateSeed=*/true>(new_ctrl);
10891089
common.set_slots(new_slots);
@@ -1134,11 +1134,11 @@ void Rehash(CommonFields& common, const PolicyFunctions& policy, size_t n) {
11341134
assert(policy.slot_align <= alignof(HeapOrSoo));
11351135
HeapOrSoo tmp_slot(uninitialized_tag_t{});
11361136
size_t begin_offset = FindFirstFullSlot(0, cap, common.control());
1137-
policy.transfer(&common, &tmp_slot,
1138-
SlotAddress(common.slot_array(), begin_offset, slot_size),
1139-
1);
1137+
policy.transfer_n(
1138+
&common, &tmp_slot,
1139+
SlotAddress(common.slot_array(), begin_offset, slot_size), 1);
11401140
clear_backing_array();
1141-
policy.transfer(&common, common.soo_data(), &tmp_slot, 1);
1141+
policy.transfer_n(&common, common.soo_data(), &tmp_slot, 1);
11421142
common.set_full_soo();
11431143
return;
11441144
}

absl/container/internal/raw_hash_set.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ constexpr size_t NumControlBytes(size_t capacity) {
831831

832832
// Computes the offset from the start of the backing allocation of control.
833833
// infoz and growth_info are stored at the beginning of the backing array.
834-
constexpr static size_t ControlOffset(bool has_infoz) {
834+
constexpr size_t ControlOffset(bool has_infoz) {
835835
return (has_infoz ? sizeof(HashtablezInfoHandle) : 0) + sizeof(GrowthInfo);
836836
}
837837

@@ -1232,7 +1232,7 @@ constexpr size_t CapacityToGrowth(size_t capacity) {
12321232
//
12331233
// This might not be a valid capacity and `NormalizeCapacity()` should be
12341234
// called on this.
1235-
inline size_t GrowthToLowerboundCapacity(size_t growth) {
1235+
constexpr size_t GrowthToLowerboundCapacity(size_t growth) {
12361236
// `growth*8/7`
12371237
if (Group::kWidth == 8 && growth == 7) {
12381238
// x+(x-1)/7 does not work when x==7.
@@ -1639,8 +1639,7 @@ struct PolicyFunctions {
16391639

16401640
// Transfers the contents of `count` slots from src_slot to dst_slot.
16411641
// We use ability to transfer several slots in single group table growth.
1642-
// TODO(b/382423690): consider having separate `transfer` and `transfer_n`.
1643-
void (*transfer)(void* set, void* dst_slot, void* src_slot, size_t count);
1642+
void (*transfer_n)(void* set, void* dst_slot, void* src_slot, size_t count);
16441643

16451644
// Returns the pointer to the CharAlloc stored in the set.
16461645
void* (*get_char_alloc)(CommonFields& common);
@@ -1833,10 +1832,10 @@ void EraseMetaOnly(CommonFields& c, size_t index, size_t slot_size);
18331832
// For trivially relocatable types we use memcpy directly. This allows us to
18341833
// share the same function body for raw_hash_set instantiations that have the
18351834
// same slot size as long as they are relocatable.
1835+
// Separate function for relocating single slot cause significant binary bloat.
18361836
template <size_t SizeOfSlot>
1837-
ABSL_ATTRIBUTE_NOINLINE void TransferRelocatable(void*, void* dst, void* src,
1838-
size_t count) {
1839-
// TODO(b/382423690): Experiment with transfer and transfer_n.
1837+
ABSL_ATTRIBUTE_NOINLINE void TransferNRelocatable(void*, void* dst, void* src,
1838+
size_t count) {
18401839
// TODO(b/382423690): Experiment with making specialization for power of 2 and
18411840
// non power of 2. This would require passing the size of the slot.
18421841
memcpy(dst, src, SizeOfSlot * count);
@@ -3567,7 +3566,8 @@ class raw_hash_set {
35673566
// TODO(b/397453582): Remove support for const hasher.
35683567
return const_cast<std::remove_const_t<hasher>*>(&h->hash_ref());
35693568
}
3570-
static void transfer_slots_fn(void* set, void* dst, void* src, size_t count) {
3569+
static void transfer_n_slots_fn(void* set, void* dst, void* src,
3570+
size_t count) {
35713571
auto* src_slot = to_slot(src);
35723572
auto* dst_slot = to_slot(dst);
35733573

@@ -3627,8 +3627,8 @@ class raw_hash_set {
36273627
: &raw_hash_set::get_hash_ref_fn,
36283628
PolicyTraits::template get_hash_slot_fn<hasher>(),
36293629
PolicyTraits::transfer_uses_memcpy()
3630-
? TransferRelocatable<sizeof(slot_type)>
3631-
: &raw_hash_set::transfer_slots_fn,
3630+
? TransferNRelocatable<sizeof(slot_type)>
3631+
: &raw_hash_set::transfer_n_slots_fn,
36323632
std::is_empty_v<Alloc> ? &GetRefForEmptyClass
36333633
: &raw_hash_set::get_char_alloc_ref_fn,
36343634
&AllocateBackingArray<kBackingArrayAlignment, CharAlloc>,

0 commit comments

Comments
 (0)