Skip to content

Commit 5bd1257

Browse files
goldvitalycopybara-github
authored andcommitted
Rollback reduction of maximum load factor. Now it is back to 28/32.
There was no visible improvement and potentially performance regression in the production. PiperOrigin-RevId: 828365574 Change-Id: Id3dab84ce9a6a6c132db40f370303fee0a42ed9b
1 parent 18ac608 commit 5bd1257

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

absl/container/internal/raw_hash_set.h

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,32 +1169,30 @@ constexpr size_t NormalizeCapacity(size_t n) {
11691169
}
11701170

11711171
// General notes on capacity/growth methods below:
1172-
// - We use 27/32 as maximum load factor. For 16-wide groups, that gives an
1173-
// average of 2.5 empty slots per group.
1172+
// - We use 7/8th as maximum load factor. For 16-wide groups, that gives an
1173+
// average of two empty slots per group.
1174+
// - For (capacity+1) >= Group::kWidth, growth is 7/8*capacity.
11741175
// - For (capacity+1) < Group::kWidth, growth == capacity. In this case, we
11751176
// never need to probe (the whole table fits in one group) so we don't need a
11761177
// load factor less than 1.
1177-
// - For (capacity+1) == Group::kWidth, growth is capacity - 1 since we need
1178-
// at least one empty slot for probing algorithm.
1179-
// - For (capacity+1) > Group::kWidth, growth is 27/32*capacity.
11801178

11811179
// Given `capacity`, applies the load factor; i.e., it returns the maximum
11821180
// number of values we should put into the table before a resizing rehash.
11831181
constexpr size_t CapacityToGrowth(size_t capacity) {
11841182
ABSL_SWISSTABLE_ASSERT(IsValidCapacity(capacity));
1185-
// `capacity*27/32`
1183+
// `capacity*7/8`
11861184
if (Group::kWidth == 8 && capacity == 7) {
1187-
// formula does not work when x==7.
1185+
// x-x/8 does not work when x==7.
11881186
return 6;
11891187
}
1190-
return capacity - capacity / 8 - capacity / 32;
1188+
return capacity - capacity / 8;
11911189
}
11921190

11931191
// Given `size`, "unapplies" the load factor to find how large the capacity
11941192
// should be to stay within the load factor.
11951193
//
11961194
// For size == 0, returns 0.
1197-
// For other values, returns the same as `NormalizeCapacity(size*32/27)`.
1195+
// For other values, returns the same as `NormalizeCapacity(size*8/7)`.
11981196
constexpr size_t SizeToCapacity(size_t size) {
11991197
if (size == 0) {
12001198
return 0;
@@ -1203,10 +1201,18 @@ constexpr size_t SizeToCapacity(size_t size) {
12031201
// Shifting right `~size_t{}` by `leading_zeros` yields
12041202
// NormalizeCapacity(size).
12051203
int leading_zeros = absl::countl_zero(size);
1206-
size_t next_capacity = ~size_t{} >> leading_zeros;
1207-
size_t max_size_for_next_capacity = CapacityToGrowth(next_capacity);
1204+
constexpr size_t kLast3Bits = size_t{7} << (sizeof(size_t) * 8 - 3);
1205+
// max_size_for_next_capacity = max_load_factor * next_capacity
1206+
// = (7/8) * (~size_t{} >> leading_zeros)
1207+
// = (7/8*~size_t{}) >> leading_zeros
1208+
// = kLast3Bits >> leading_zeros
1209+
size_t max_size_for_next_capacity = kLast3Bits >> leading_zeros;
12081210
// Decrease shift if size is too big for the minimum capacity.
12091211
leading_zeros -= static_cast<int>(size > max_size_for_next_capacity);
1212+
if constexpr (Group::kWidth == 8) {
1213+
// Formula doesn't work when size==7 for 8-wide groups.
1214+
leading_zeros -= (size == 7);
1215+
}
12101216
return (~size_t{}) >> leading_zeros;
12111217
}
12121218

absl/container/internal/raw_hash_set_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ TEST(Util, CapacityToGrowthSmallValues) {
302302
}
303303
EXPECT_EQ(CapacityToGrowth(15), 14);
304304
EXPECT_EQ(CapacityToGrowth(31), 28);
305-
EXPECT_EQ(CapacityToGrowth(63), 55);
305+
EXPECT_EQ(CapacityToGrowth(63), 56);
306306
}
307307

308308
TEST(Util, GrowthAndCapacity) {

absl/hash/hash_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1311,7 +1311,7 @@ TEST(SwisstableCollisions, LowEntropyInts) {
13111311
for (size_t i = 0; i < 128 * 1024; ++i) {
13121312
size_t v = absl::rotl(i, bit);
13131313
set.insert(v);
1314-
ASSERT_LT(HashtableDebugAccess<decltype(set)>::GetNumProbes(set, v), 32)
1314+
ASSERT_LT(HashtableDebugAccess<decltype(set)>::GetNumProbes(set, v), 48)
13151315
<< bit << " " << i;
13161316
}
13171317
}

0 commit comments

Comments
 (0)