From bd4bfed9ba625ebf9b992dc425202d59ee3ca90c Mon Sep 17 00:00:00 2001 From: Evan Brown Date: Tue, 27 May 2025 10:45:01 -0700 Subject: [PATCH] Avoid hashing the key in prefetch() for small tables. Also: - Assert that we aren't reading control when the table has capacity 0 because it is uninitialized in that case. - Use [[maybe_unused]] instead of cast to void. - Add NOLINT for erroneous "assert can be static_assert" lint warning. PiperOrigin-RevId: 763859124 Change-Id: Id8b8acb24882357e75cfe751e9b62fb94befddfa --- absl/container/internal/raw_hash_set.cc | 10 +++++++--- absl/container/internal/raw_hash_set.h | 9 ++++----- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/absl/container/internal/raw_hash_set.cc b/absl/container/internal/raw_hash_set.cc index 5238c81a..79d2e123 100644 --- a/absl/container/internal/raw_hash_set.cc +++ b/absl/container/internal/raw_hash_set.cc @@ -669,8 +669,12 @@ void ResizeNonSooImpl(CommonFields& common, ABSL_SWISSTABLE_ASSERT(new_capacity > policy.soo_capacity()); const size_t old_capacity = common.capacity(); - [[maybe_unused]] ctrl_t* old_ctrl = common.control(); - [[maybe_unused]] void* old_slots = common.slot_array(); + [[maybe_unused]] ctrl_t* old_ctrl; + [[maybe_unused]] void* old_slots; + if constexpr (kMode == ResizeNonSooMode::kGuaranteedAllocated) { + old_ctrl = common.control(); + old_slots = common.slot_array(); + } const size_t slot_size = policy.slot_size; const size_t slot_align = policy.slot_align; @@ -879,7 +883,7 @@ void GrowIntoSingleGroupShuffleControlBytes(ctrl_t* __restrict old_ctrl, return; } - ABSL_SWISSTABLE_ASSERT(Group::kWidth == 16); + ABSL_SWISSTABLE_ASSERT(Group::kWidth == 16); // NOLINT(misc-static-assert) // Fill the second half of the main control bytes with kEmpty. // For small capacity that may write into mirrored control bytes. diff --git a/absl/container/internal/raw_hash_set.h b/absl/container/internal/raw_hash_set.h index f9c9b0b7..002fd887 100644 --- a/absl/container/internal/raw_hash_set.h +++ b/absl/container/internal/raw_hash_set.h @@ -282,10 +282,8 @@ void SwapAlloc(AllocType& lhs, AllocType& rhs, swap(lhs, rhs); } template -void SwapAlloc(AllocType& lhs, AllocType& rhs, +void SwapAlloc([[maybe_unused]] AllocType& lhs, [[maybe_unused]] AllocType& rhs, std::false_type /* propagate_on_container_swap */) { - (void)lhs; - (void)rhs; assert(lhs == rhs && "It's UB to call swap with unequal non-propagating allocators."); } @@ -949,6 +947,7 @@ class CommonFields : public CommonFieldsGenerationInfo { void* soo_data() { return heap_or_soo_.get_soo_data(); } ctrl_t* control() const { + ABSL_SWISSTABLE_ASSERT(capacity() > 0); ABSL_SWISSTABLE_IGNORE_UNINITIALIZED_RETURN(heap_or_soo_.control().get()); } @@ -2806,12 +2805,12 @@ class raw_hash_set { // NOTE: This is a very low level operation and should not be used without // specific benchmarks indicating its importance. template - void prefetch(const key_arg& key) const { + void prefetch([[maybe_unused]] const key_arg& key) const { if (capacity() == DefaultCapacity()) return; - (void)key; // Avoid probing if we won't be able to prefetch the addresses received. #ifdef ABSL_HAVE_PREFETCH prefetch_heap_block(); + if (is_small()) return; auto seq = probe(common(), hash_of(key)); PrefetchToLocalCache(control() + seq.offset()); PrefetchToLocalCache(slot_array() + seq.offset());