@@ -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.
11831181constexpr 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 )`.
11981196constexpr 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
0 commit comments