Report accurate allocated capacity in SwissTable hashtablez samples. 1. Report capacity 1 (SooCapacity()) for sampled SOO tables that have never been reserved or resized for capacity > 1, even though sampled tables allocate a 3-slot backing array on the heap to store the infoz handle. Transition the reported capacity to the allocated heap capacity in the cold hashtablez sampler once the table grows past size 1 or is reserved or rehashed for capacity > 1. 2. Report capacity - blocked_element_count instead of raw capacity so unallocated blocked tail slots in reserved tables are not counted toward capacity or wasted slot bytes. All capacity adjustments are performed in cold sampling functions so hot lookup, insertion, erase, and unsampled resize paths are unaffected. PiperOrigin-RevId: 1021119632
diff --git a/absl/container/internal/hashtablez_sampler.cc b/absl/container/internal/hashtablez_sampler.cc index 82929bb..a25788b 100644 --- a/absl/container/internal/hashtablez_sampler.cc +++ b/absl/container/internal/hashtablez_sampler.cc
@@ -61,6 +61,30 @@ if (listener != nullptr) listener(); } +// Sampled SOO tables allocate a `NextCapacity(soo_capacity)` (3-slot) backing +// array on the heap to store `HashtablezInfoHandle`, while reporting +// `capacity == soo_capacity` until grown or reserved past `soo_capacity`. +constexpr size_t SampledSooAllocatedCapacity(size_t soo_capacity) { + return 2 * soo_capacity + 1; +} + +// Returns true when `info` represents a sampled SOO table that is still +// reporting `soo_capacity`. `PrepareForSampling` initializes `capacity` to 0, +// and the initial `RecordStorageChangedSlow` call clamps the 3-slot heap +// capacity to `soo_capacity` so this becomes true until `LeaveSooCapacity`. +bool IsReportingSooCapacity(const HashtablezInfo* info) { + return info->soo_capacity > 0 && + info->capacity.load(std::memory_order_relaxed) == info->soo_capacity; +} + +// Transitions `info` from reporting `soo_capacity` to reporting its actual +// 3-slot heap capacity and records the logical SOO-to-heap growth rehash. +void LeaveSooCapacity(HashtablezInfo* info) { + info->capacity.store(SampledSooAllocatedCapacity(info->soo_capacity), + std::memory_order_relaxed); + RecordRehashSlow(info, /*total_probe_length=*/0); +} + } // namespace #if defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE) @@ -195,6 +219,7 @@ } void RecordRehashSlow(HashtablezInfo* info, size_t total_probe_length) { + if (IsReportingSooCapacity(info)) return; #ifdef ABSL_INTERNAL_HAVE_SSE2 total_probe_length /= 16; #else @@ -210,6 +235,10 @@ } void RecordReservationSlow(HashtablezInfo* info, size_t target_capacity) { + if (target_capacity <= info->soo_capacity) return; + if (IsReportingSooCapacity(info)) { + LeaveSooCapacity(info); + } info->max_reserve.store( (std::max)(info->max_reserve.load(std::memory_order_relaxed), target_capacity), @@ -222,6 +251,11 @@ void RecordStorageChangedSlow(HashtablezInfo* info, size_t size, size_t capacity) { + if (info->soo_capacity > 0 && size <= info->soo_capacity && + capacity == SampledSooAllocatedCapacity(info->soo_capacity) && + info->capacity.load(std::memory_order_relaxed) <= info->soo_capacity) { + capacity = info->soo_capacity; + } info->size.store(size, std::memory_order_relaxed); info->capacity.store(capacity, std::memory_order_relaxed); if (size == 0) { @@ -233,6 +267,10 @@ void RecordInsertMissSlow(HashtablezInfo* info, size_t hash, size_t distance_from_desired) { + if (IsReportingSooCapacity(info) && + info->size.load(std::memory_order_relaxed) >= info->soo_capacity) { + LeaveSooCapacity(info); + } // SwissTables probe in groups of 16, so scale this to count items probes and // not offset from desired. size_t probe_length = distance_from_desired;
diff --git a/absl/container/internal/hashtablez_sampler.h b/absl/container/internal/hashtablez_sampler.h index 540205e..4ad5e5f 100644 --- a/absl/container/internal/hashtablez_sampler.h +++ b/absl/container/internal/hashtablez_sampler.h
@@ -99,9 +99,10 @@ absl::Time create_time; uint32_t depth; // The SOO capacity for this table in elements (not bytes). Note that sampled - // tables are never SOO because we need to store the infoz handle on the heap. - // Tables that would be SOO if not sampled should have: soo_capacity > 0 && - // size <= soo_capacity && max_reserve <= soo_capacity. + // tables are never SOO because we need to store the infoz handle on the heap, + // but tables that would be SOO if not sampled report `capacity == + // soo_capacity` (and have `soo_capacity > 0 && size <= soo_capacity && + // max_reserve <= soo_capacity`). uint16_t soo_capacity; void* stack[kMaxStackDepth]; size_t inline_element_size; // How big is the slot in bytes?
diff --git a/absl/container/internal/raw_hash_set.cc b/absl/container/internal/raw_hash_set.cc index bd36659..9c188a2 100644 --- a/absl/container/internal/raw_hash_set.cc +++ b/absl/container/internal/raw_hash_set.cc
@@ -843,6 +843,15 @@ // new_ctrl after 2nd store = EHNSEHNEEEE } +size_t AllocatedCapacityForInfoz(const CommonFields& c) { + return c.capacity() - c.blocked_element_count(); +} + +ABSL_ATTRIBUTE_COLD ABSL_ATTRIBUTE_NOINLINE void ReportClearToInfoz( + CommonFields& c) { + c.infoz().RecordStorageChanged(0, AllocatedCapacityForInfoz(c)); +} + // Clears the backing array and sets the common fields to the default values for // empty non-allocated tables. REQUIRES: c.capacity > policy.soo_capacity. void ClearBackingArrayNoReuse(CommonFields& c, @@ -952,7 +961,9 @@ ResetCtrl(cap, ctrl, slot_array, policy.slot_size, blocked_element_count); ResetGrowthLeft(cap, blocked_element_count, c); ABSL_SWISSTABLE_ASSERT(c.blocked_element_count() == blocked_element_count); - c.infoz().RecordStorageChanged(0, c.capacity()); + if (c.has_infoz()) { + ReportClearToInfoz(c); + } } else { ClearBackingArrayNoReuse(c, policy); } @@ -1129,7 +1140,8 @@ ABSL_ATTRIBUTE_COLD ABSL_ATTRIBUTE_NOINLINE void ReportGrowthToInfozImpl( CommonFields& common, HashtablezInfoHandle infoz, size_t hash, size_t total_probe_length, size_t distance_from_desired) { - infoz.RecordStorageChanged(common.size() - 1, common.capacity()); + infoz.RecordStorageChanged(common.size() - 1, + AllocatedCapacityForInfoz(common)); infoz.RecordRehash(total_probe_length); infoz.RecordInsertMiss(hash, distance_from_desired); common.set_has_infoz(); @@ -1201,30 +1213,25 @@ total_probe_length, distance_from_desired); } -// Passed as `total_probe_length` to `ReportResizeToInfoz` when the resize must -// not be reported as a rehash. -constexpr size_t kNoRehash = ~size_t{}; - ABSL_ATTRIBUTE_COLD ABSL_ATTRIBUTE_NOINLINE void ReportResizeToInfoz( CommonFields& common, HashtablezInfoHandle infoz, size_t total_probe_length) { - infoz.RecordStorageChanged(common.size(), common.capacity()); - if (total_probe_length != kNoRehash) { - infoz.RecordRehash(total_probe_length); - } + infoz.RecordStorageChanged(common.size(), AllocatedCapacityForInfoz(common)); + infoz.RecordRehash(total_probe_length); common.set_has_infoz(); common.set_infoz(infoz); } ABSL_ATTRIBUTE_COLD ABSL_ATTRIBUTE_NOINLINE void -ReportResizeToInfozAndForceSample( - CommonFields& common, const AllocPolicy& __restrict policy, - size_t total_probe_length) { - HashtablezInfoHandle infoz = ForcedTrySample(policy); - infoz.RecordStorageChanged(common.size(), common.capacity()); - infoz.RecordRehash(total_probe_length); - common.set_has_infoz(); - common.set_infoz(infoz); +ReportResizeToInfozAndForceSample(CommonFields& common, + const AllocPolicy& __restrict policy) { + ReportResizeToInfoz(common, ForcedTrySample(policy), + /*total_probe_length=*/0); +} + +ABSL_ATTRIBUTE_COLD ABSL_ATTRIBUTE_NOINLINE void ReportReserveToInfoz( + CommonFields& common, size_t new_size) { + common.infoz().RecordReservation(new_size); } struct BackingArrayPtrs { @@ -1290,7 +1297,7 @@ ResetGrowthLeft(new_capacity, blocked_element_count, common); if (ABSL_PREDICT_FALSE(has_infoz)) { - ReportResizeToInfozAndForceSample(common, policy, 0); + ReportResizeToInfozAndForceSample(common, policy); } } @@ -1391,9 +1398,7 @@ } ResetGrowthLeft(new_capacity, common.size(), common); if (ABSL_PREDICT_FALSE(has_infoz)) { - // Forced sampling of a full SOO table is not reported as a rehash. - ReportResizeToInfoz(common, infoz, - force_sample_soo ? kNoRehash : total_probe_length); + ReportResizeToInfoz(common, infoz, total_probe_length); } } @@ -2292,7 +2297,9 @@ /*force_infoz=*/false); // This is after resize, to ensure that we have completed the allocation // and have potentially sampled the hashtable. - common.infoz().RecordReservation(new_size); + if (common.has_infoz()) { + ReportReserveToInfoz(common, new_size); + } } // Type erased version of raw_hash_set::reserve for tables that have an @@ -2313,7 +2320,9 @@ // TODO(b/382423690): consider using GrowToNextCapacity, when applicable. ResizeTableWithSeedChangeImpl(common, policy, new_capacity, /*force_sample_soo=*/false); - common.infoz().RecordReservation(new_size); + if (common.has_infoz()) { + ReportReserveToInfoz(common, new_size); + } } // Grows a full SOO table to the next capacity and forces it to be sampled. SOO @@ -2476,9 +2485,12 @@ ResizeTableWithSeedChangeImpl(common, policy, new_capacity, /*force_sample_soo=*/false); } - // This is after resize, to ensure that we have completed the allocation - // and have potentially sampled the hashtable. - common.infoz().RecordReservation(n); + } + // This is after resize, to ensure that we have completed the allocation and + // have potentially sampled the hashtable. Also record when `new_capacity <= + // cap` so sampled SOO tables report leaving SOO capacity. + if (common.has_infoz()) { + ReportReserveToInfoz(common, n); } } @@ -2575,6 +2587,9 @@ ? cap : common.size() + common.GetGrowthLeftTotalSlow(cap); if (new_size <= max_size_before_growth) { + if (common.has_infoz()) { + ReportReserveToInfoz(common, new_size); + } return; } ReserveAllocatedTable(common, policy, new_size);
diff --git a/absl/container/internal/raw_hash_set_test.cc b/absl/container/internal/raw_hash_set_test.cc index a62adf5..e0a6cb1 100644 --- a/absl/container/internal/raw_hash_set_test.cc +++ b/absl/container/internal/raw_hash_set_test.cc
@@ -97,6 +97,16 @@ } }; +#if defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE) +class HashtablezInfoHandlePeer { + public: + template <typename C> + static const HashtablezInfo* GetInfo(C& c) { + return RawHashSetTestOnlyAccess::GetCommon(c).infoz().info_; + } +}; +#endif // defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE) + namespace { using ::testing::ElementsAre; @@ -4282,10 +4292,11 @@ ASSERT_EQ(info->inline_element_size, sizeof(typename SooInt32Table::value_type)); ASSERT_EQ(info->soo_capacity, SooCapacity()); - ASSERT_EQ(info->capacity, NextCapacity(SooCapacity())); + ASSERT_EQ(info->capacity, SooCapacity()); ASSERT_EQ(info->size, 1); ASSERT_EQ(info->max_reserve, 0); ASSERT_EQ(info->num_erases, 0); + ASSERT_EQ(info->num_rehashes, 0); ASSERT_EQ(info->max_probe_length, 0); ASSERT_EQ(info->total_probe_length, 0); } @@ -4323,6 +4334,11 @@ t.erase(1); } }); + for (const HashtablezInfo* info : infos) { + EXPECT_EQ(info->soo_capacity, SooCapacity()); + ASSERT_EQ(info->capacity, SooCapacity()); + ASSERT_EQ(info->size, 0); + } // SampleSooMutation checks EXPECT_NEAR(sampled/total, 1%, 0.5%). // If the sampling logic is incorrectly evaluated on every 0->1 element @@ -4416,7 +4432,8 @@ ASSERT_EQ(info->inline_element_size, sizeof(typename SooInt32Table::value_type)); ASSERT_EQ(info->soo_capacity, SooCapacity()); - ASSERT_GE(info->capacity, 100); + ASSERT_EQ(info->capacity, + SizeToCapacity(100) - kMaxBlockedElementsForLargeTables); ASSERT_EQ(info->size, 0); ASSERT_EQ(info->max_reserve, 100); ASSERT_EQ(info->num_erases, 0); @@ -4467,8 +4484,9 @@ ASSERT_EQ(info->inline_element_size, sizeof(typename SooInt32Table::value_type)); ASSERT_EQ(info->soo_capacity, SooCapacity()); - ASSERT_EQ(info->capacity, NextCapacity(SooCapacity())); + ASSERT_EQ(info->capacity, SooCapacity()); ASSERT_EQ(info->size, 1); + ASSERT_EQ(info->num_rehashes, 0); } } @@ -4570,6 +4588,135 @@ EXPECT_EQ(final_size, start_size + 1); ASSERT_NE(latest_info, nullptr); EXPECT_EQ(latest_info->size.load(std::memory_order_relaxed), 2); + EXPECT_EQ(latest_info->capacity.load(std::memory_order_relaxed), + NextCapacity(SooCapacity())); +} + +struct SampleEveryTable { + SampleEveryTable() { + SetHashtablezEnabled(true); + SetHashtablezSampleParameter(1); + TestOnlyRefreshSamplingStateForCurrentThread(); + } + ~SampleEveryTable() { DisableSampling(); } +}; + +class RawHashSooSamplerTest : public testing::Test { + protected: + void SetUp() override { + if (SooInt32Table().capacity() != SooCapacity()) { + CHECK_LT(sizeof(void*), 8) << "missing SOO coverage"; + GTEST_SKIP() << "not SOO on this platform"; + } + } + + SampleEveryTable sample_all_; +}; + +TEST_F(RawHashSooSamplerTest, StaysSooCapacityWithoutGrowthOrReserve) { + SooInt32Table t; + t.insert(1); + const HashtablezInfo* info = HashtablezInfoHandlePeer::GetInfo(t); + ASSERT_NE(info, nullptr); + EXPECT_EQ(info->capacity, SooCapacity()); + + auto expect_stays_soo = [&](auto op) { + op(); + EXPECT_EQ(info->capacity, SooCapacity()); + }; + + // These operations don't change reported capacity to non-SOO. + expect_stays_soo([&] { t.erase(t.begin(), t.end()); }); + EXPECT_EQ(info->size, 0); + expect_stays_soo([&] { t.insert(2); }); + expect_stays_soo([&] { t.rehash(0); }); + expect_stays_soo([&] { t.rehash(1); }); + expect_stays_soo([&] { t.reserve(1); }); + std::optional<SooInt32Table> moved; + expect_stays_soo([&] { moved.emplace(std::move(t)); }); + EXPECT_EQ(info->size, 1); + EXPECT_EQ(info->max_reserve, 0); + EXPECT_EQ(info->num_rehashes, 0); +} + +TEST_F(RawHashSooSamplerTest, LeavesSooCapacityOnGrowth) { + SooInt32Table t; + t.insert(1); + const HashtablezInfo* info = HashtablezInfoHandlePeer::GetInfo(t); + ASSERT_NE(info, nullptr); + EXPECT_EQ(info->capacity, SooCapacity()); + + // Growing to 2 elements transitions reported capacity to 3, and subsequent + // erases or rehash(0) keep capacity 3 because the table was resized. + t.insert(2); + EXPECT_EQ(info->capacity, NextCapacity(SooCapacity())); + EXPECT_EQ(info->size, 2); + EXPECT_EQ(info->num_rehashes, 1); + + t.erase(2); + t.rehash(0); + EXPECT_EQ(info->capacity, NextCapacity(SooCapacity())); + EXPECT_EQ(info->size, 1); + + t.erase(t.begin(), t.end()); + EXPECT_EQ(info->capacity, NextCapacity(SooCapacity())); + EXPECT_EQ(info->size, 0); +} + +TEST_F(RawHashSooSamplerTest, LeavesSooCapacityOnReserveOrRehash) { + // Reserving or rehashing for >1 capacity on a size-1 sampled SOO table + // transitions reported capacity out of SOO even when heap capacity is 3. + for (bool use_rehash : {false, true}) { + SooInt32Table s; + s.insert(1); + use_rehash ? s.rehash(2) : s.reserve(2); + const HashtablezInfo* s_info = HashtablezInfoHandlePeer::GetInfo(s); + ASSERT_NE(s_info, nullptr); + EXPECT_EQ(s_info->capacity, NextCapacity(SooCapacity())); + EXPECT_EQ(s_info->max_reserve, 2); + EXPECT_EQ(s_info->num_rehashes, 1); + } + + SooInt32Table reserved_three; + reserved_three.reserve(3); + const HashtablezInfo* r3_info = + HashtablezInfoHandlePeer::GetInfo(reserved_three); + ASSERT_NE(r3_info, nullptr); + EXPECT_EQ(r3_info->capacity, NextCapacity(SooCapacity())); + EXPECT_EQ(r3_info->max_reserve, 3); + EXPECT_EQ(r3_info->num_rehashes, 1); +} + +TYPED_TEST(RawHashSamplerTest, ReportsCapacityMinusBlockedElements) { + SampleEveryTable sample_all; + + TypeParam t; + t.reserve(2); + const HashtablezInfo* info = HashtablezInfoHandlePeer::GetInfo(t); + ASSERT_NE(info, nullptr); + // SizeToCapacity(2) == 3 with 1 blocked element, so allocated slots == 2. + EXPECT_EQ(info->capacity, 2); + + t.insert({1, 2}); + EXPECT_EQ(info->capacity, 2); + EXPECT_EQ(info->size, 2); + + t.erase(t.begin(), t.end()); + EXPECT_EQ(info->capacity, 2); + EXPECT_EQ(info->size, 0); + + // Growing past reserved size clears blocked elements. + t.insert({1, 2, 3}); + EXPECT_EQ(info->capacity, 7); + EXPECT_EQ(info->size, 3); + + // Copy-constructing a 2-element table reserves exact size 2 (3 - 1 blocked). + TypeParam src = {10, 20}; + TypeParam copy(src); + const HashtablezInfo* copy_info = HashtablezInfoHandlePeer::GetInfo(copy); + ASSERT_NE(copy_info, nullptr); + EXPECT_EQ(copy_info->capacity, 2); + EXPECT_EQ(copy_info->size, 2); } #endif // ABSL_INTERNAL_HASHTABLEZ_SAMPLE