Skip to content

Commit 41a1f43

Browse files
ezbrcopybara-github
authored andcommitted
Avoid allocating control bytes in capacity==1 swisstables.
PiperOrigin-RevId: 759275918 Change-Id: Id4e88ae026e3ebee04f5bdb28a7191e54aaea1e9
1 parent be56618 commit 41a1f43

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

absl/container/internal/raw_hash_set.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@ void ResetCtrl(CommonFields& common, size_t slot_size) {
443443
ctrl_t* ctrl = common.control();
444444
static constexpr size_t kTwoGroupCapacity = 2 * Group::kWidth - 1;
445445
if (ABSL_PREDICT_TRUE(capacity <= kTwoGroupCapacity)) {
446+
if (IsSmallCapacity(capacity)) return;
446447
std::memset(ctrl, static_cast<int8_t>(ctrl_t::kEmpty), Group::kWidth);
447448
std::memset(ctrl + capacity, static_cast<int8_t>(ctrl_t::kEmpty),
448449
Group::kWidth);
@@ -591,13 +592,14 @@ size_t FindNewPositionsAndTransferSlots(
591592

592593
const auto insert_slot = [&](void* slot) {
593594
size_t hash = policy.hash_slot(hash_fn, slot);
594-
auto target = find_first_non_full(common, hash);
595+
FindInfo target =
596+
common.is_small() ? FindInfo{0, 0} : find_first_non_full(common, hash);
595597
SetCtrl(common, target.offset, H2(hash), slot_size);
596598
policy.transfer_n(&common, SlotAddress(new_slots, target.offset, slot_size),
597599
slot, 1);
598600
return target.probe_length;
599601
};
600-
if (old_capacity == 1) {
602+
if (IsSmallCapacity(old_capacity)) {
601603
if (common.size() == 1) insert_slot(old_slots);
602604
return 0;
603605
}
@@ -1343,8 +1345,6 @@ void SmallEmptyNonSooPrepareInsert(CommonFields& common,
13431345
const bool has_infoz = infoz.IsSampled();
13441346
void* alloc = policy.get_char_alloc(common);
13451347

1346-
// TODO(b/413062340): don't allocate control bytes for capacity 1 tables. We
1347-
// don't use the control bytes in this case.
13481348
const auto [new_ctrl, new_slots] =
13491349
AllocBackingArray(common, policy, kNewCapacity, has_infoz, alloc);
13501350
common.set_control</*kGenerateSeed=*/true>(new_ctrl);

absl/container/internal/raw_hash_set.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,7 @@ constexpr size_t NumClonedBytes() { return Group::kWidth - 1; }
796796

797797
// Returns the number of control bytes including cloned.
798798
constexpr size_t NumControlBytes(size_t capacity) {
799-
return capacity + 1 + NumClonedBytes();
799+
return IsSmallCapacity(capacity) ? 0 : capacity + 1 + NumClonedBytes();
800800
}
801801

802802
// Computes the offset from the start of the backing allocation of control.

absl/container/internal/raw_hash_set_test.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,7 +1184,7 @@ TYPED_TEST(SmallTableResizeTest, InsertIntoSmallTable) {
11841184
t.insert(i);
11851185
ASSERT_EQ(t.size(), i + 1);
11861186
for (int j = 0; j < i + 1; ++j) {
1187-
EXPECT_TRUE(t.find(j) != t.end());
1187+
ASSERT_TRUE(t.find(j) != t.end());
11881188
EXPECT_EQ(*t.find(j), j);
11891189
}
11901190
}
@@ -1207,7 +1207,7 @@ TYPED_TEST(SmallTableResizeTest, ResizeGrowSmallTables) {
12071207
t.reserve(target_size);
12081208
}
12091209
for (size_t i = 0; i < source_size; ++i) {
1210-
EXPECT_TRUE(t.find(static_cast<int>(i)) != t.end());
1210+
ASSERT_TRUE(t.find(static_cast<int>(i)) != t.end());
12111211
EXPECT_EQ(*t.find(static_cast<int>(i)), static_cast<int>(i));
12121212
}
12131213
}
@@ -1232,7 +1232,7 @@ TYPED_TEST(SmallTableResizeTest, ResizeReduceSmallTables) {
12321232
<< "rehash(0) must resize to the minimum capacity";
12331233
}
12341234
for (size_t i = 0; i < inserted_count; ++i) {
1235-
EXPECT_TRUE(t.find(static_cast<int>(i)) != t.end());
1235+
ASSERT_TRUE(t.find(static_cast<int>(i)) != t.end());
12361236
EXPECT_EQ(*t.find(static_cast<int>(i)), static_cast<int>(i));
12371237
}
12381238
}

0 commit comments

Comments
 (0)