Unify direct and dereferenced TypeErasedApplyToSlotFn templates via kDerefSlot Extract a DerefSlot<T, kDerefSlot> helper shared with SlotEqFn, and parameterize TypeErasedApplyToSlotFn, TypeErasedApplyAbslHashToSlotFn, and GetTypeErasedApplyToSlotFn on `bool kDerefSlot = false` instead of duplicating them as TypeErasedDerefAndApplyToSlotFn, TypeErasedDerefAndApplyAbslHashToSlotFn, and GetTypeErasedDerefAndApplyToSlotFn. PiperOrigin-RevId: 1025723514
diff --git a/absl/container/internal/container_memory.h b/absl/container/internal/container_memory.h index 54cde80..39fe931 100644 --- a/absl/container/internal/container_memory.h +++ b/absl/container/internal/container_memory.h
@@ -664,6 +664,20 @@ type_traits_internal::AddConstRefIfExpensiveToCopy<KeyEqual> eq; }; +template <class T, bool kDerefSlot> +const T& DerefSlot(const void* slot) { + if constexpr (kDerefSlot) { + const T* node; + // Copy the pointer via memcpy to avoid a strict-aliasing violation when + // `slot` has type `std::pair<const T, mapped_type>**`, which cannot be + // dereferenced as `const T**` (`first` is at offset 0 of the pair). + std::memcpy(&node, slot, sizeof(node)); + return *node; + } else { + return *static_cast<const T*>(slot); + } +} + // Type-erased slot equality functor with the key at offset 0 of `slot` (or // `*slot` if `kDerefSlot` is true). template <class SlotKey, bool kDerefSlot> @@ -672,38 +686,35 @@ template <class Eq, class SearchKey> bool operator()(const Eq& eq, const void* slot, const SearchKey& key) const { - if constexpr (kDerefSlot) { - const void* node; - // Copy the pointer to avoid strict aliasing issues. - std::memcpy(&node, slot, sizeof(node)); - slot = node; - } - return EqualElement<SearchKey, Eq>{key, - eq}(*static_cast<const SlotKey*>(slot)); + return EqualElement<SearchKey, Eq>{key, eq}( + DerefSlot<SlotKey, kDerefSlot>(slot)); } }; // Type erased function for computing hash of the slot. using HashSlotFn = size_t (*)(const void* hash_fn, void* slot, size_t seed); -// Type erased function to apply `Fn` to data inside of the `slot`. -// The data is expected to have type `T`. -template <class Fn, class T, bool kIsAbsl, size_t kSeedShift> +// Type erased function to apply `Fn` to data inside of `slot` (or `*slot` if +// `kDerefSlot` is true). The data is expected to have type `T`. +template <class Fn, class T, bool kIsAbsl, size_t kSeedShift, + bool kDerefSlot = false> size_t TypeErasedApplyToSlotFn(const void* fn, void* slot, size_t seed) { const auto* f = static_cast<const Fn*>(fn); - return HashElement<Fn, kIsAbsl, kSeedShift>{ - *f, seed}(*static_cast<const T*>(slot)); + return HashElement<Fn, kIsAbsl, kSeedShift>{*f, seed}( + DerefSlot<T, kDerefSlot>(slot)); } -// Type erased function to apply `CanonicalHasher` to data inside of the `slot`. -template <class CanonicalHasher> +// Type erased function to apply `CanonicalHasher` to data inside of `slot` (or +// `*slot` if `kDerefSlot` is true). +template <class CanonicalHasher, bool kDerefSlot = false> size_t TypeErasedApplyAbslHashToSlotFn(const void*, void* slot, size_t seed) { using T = typename CanonicalHasher::KeyType; return absl::hash_internal::HashWithSeed().hash( - CanonicalHasher(), *static_cast<const T*>(slot), seed); + CanonicalHasher(), DerefSlot<T, kDerefSlot>(slot), seed); } -template <class Fn, class T, bool kIsAbsl, size_t kSeedShift> +template <class Fn, class T, bool kIsAbsl, size_t kSeedShift, + bool kDerefSlot = false> constexpr HashSlotFn GetTypeErasedApplyToSlotFn() { using NonConstT = std::remove_const_t<T>; // Use the canonical `absl::Hash<NonConstT>` implementation when `Fn` is @@ -711,49 +722,11 @@ // that hashes `NonConstT` identically. if constexpr (kIsAbsl && std::is_invocable_v<Fn, const NonConstT&>) { return &TypeErasedApplyAbslHashToSlotFn< - decltype(absl::hash_internal::CanonicalHasher<NonConstT>())>; + decltype(absl::hash_internal::CanonicalHasher<NonConstT>()), + kDerefSlot>; } - return &TypeErasedApplyToSlotFn<Fn, NonConstT, kIsAbsl, kSeedShift>; -} - -// Type erased function to apply `Fn` to data inside of the `*slot_ptr`. -// The data is expected to have type `T`. -template <class Fn, class T, bool kIsAbsl, size_t kSeedShift> -size_t TypeErasedDerefAndApplyToSlotFn(const void* fn, void* slot_ptr, - size_t seed) { - const auto* f = static_cast<const Fn*>(fn); - const T* slot; - // We use memcpy to avoid a strict-aliasing violation when this is called from - // NodeHashMapPolicy::get_hash_slot_fn(). In that case, slot_ptr has type - // `std::pair<const T, mapped_type>**`, which is not safe to dereference as - // `const T**`. Note that it is safe to dereference `slot` afterwards because - // `std::pair<const T, mapped_type>::first` is at offset 0. - std::memcpy(&slot, slot_ptr, sizeof(slot)); - return HashElement<Fn, kIsAbsl, kSeedShift>{*f, seed}(*slot); -} - -// Type erased function to apply `CanonicalHasher` to data inside of the -// `*slot_ptr`. -template <class CanonicalHasher> -size_t TypeErasedDerefAndApplyAbslHashToSlotFn(const void*, void* slot_ptr, - size_t seed) { - using T = typename CanonicalHasher::KeyType; - const T* slot; - // See comment in `TypeErasedDerefAndApplyToSlotFn`. - std::memcpy(&slot, slot_ptr, sizeof(slot)); - return absl::hash_internal::HashWithSeed().hash(CanonicalHasher(), *slot, - seed); -} - -template <class Fn, class T, bool kIsAbsl, size_t kSeedShift> -constexpr HashSlotFn GetTypeErasedDerefAndApplyToSlotFn() { - using NonConstT = std::remove_const_t<T>; - // See comment in `GetTypeErasedApplyToSlotFn`. - if constexpr (kIsAbsl && std::is_invocable_v<Fn, const NonConstT&>) { - return &TypeErasedDerefAndApplyAbslHashToSlotFn< - decltype(absl::hash_internal::CanonicalHasher<NonConstT>())>; - } - return &TypeErasedDerefAndApplyToSlotFn<Fn, NonConstT, kIsAbsl, kSeedShift>; + return &TypeErasedApplyToSlotFn<Fn, NonConstT, kIsAbsl, kSeedShift, + kDerefSlot>; } } // namespace container_internal
diff --git a/absl/container/internal/container_memory_test.cc b/absl/container/internal/container_memory_test.cc index 619a749..4481c25 100644 --- a/absl/container/internal/container_memory_test.cc +++ b/absl/container/internal/container_memory_test.cc
@@ -323,72 +323,56 @@ TEST(ApplyTest, TypeErasedApplyToSlotFn) { size_t x = 7; + size_t* x_ptr = &x; size_t seed = 100; auto fn = [](size_t v) { return v * 2; }; + const size_t expected = + HashElement<decltype(fn), /*kIsAbsl=*/false, /*kSeedShift=*/0>(fn, + seed)(x); EXPECT_EQ((TypeErasedApplyToSlotFn<decltype(fn), size_t, /*kIsAbsl=*/false, /*kSeedShift=*/0>(&fn, &x, seed)), - (HashElement<decltype(fn), /*kIsAbsl=*/false, /*kSeedShift=*/0>( - fn, seed)(x))); + expected); + EXPECT_EQ((TypeErasedApplyToSlotFn<decltype(fn), size_t, /*kIsAbsl=*/false, + /*kSeedShift=*/0, /*kDerefSlot=*/true>( + &fn, &x_ptr, seed)), + expected); } -TEST(ApplyTest, GetTypeErasedApplyToSlotFnCanonicalizesTypes) { +template <bool kDerefSlot> +void ExpectGetTypeErasedApplyToSlotFnCanonicalizesTypes() { enum class MyEnum : uint32_t { kOne = 1, kTwo = 2 }; HashSlotFn fn = GetTypeErasedApplyToSlotFn<absl::Hash<int32_t>, int32_t, - /*kIsAbsl=*/true, 0>(); - EXPECT_EQ(fn, (GetTypeErasedApplyToSlotFn<absl::Hash<uint32_t>, uint32_t, - /*kIsAbsl=*/true, 0>())); - EXPECT_EQ(fn, (GetTypeErasedApplyToSlotFn<absl::Hash<MyEnum>, MyEnum, - /*kIsAbsl=*/true, 0>())); + /*kIsAbsl=*/true, 0, kDerefSlot>(); + EXPECT_EQ( + fn, (GetTypeErasedApplyToSlotFn<absl::Hash<uint32_t>, uint32_t, + /*kIsAbsl=*/true, 0, kDerefSlot>())); + EXPECT_EQ( + fn, (GetTypeErasedApplyToSlotFn<absl::Hash<MyEnum>, MyEnum, + /*kIsAbsl=*/true, 0, kDerefSlot>())); EXPECT_EQ(fn, (GetTypeErasedApplyToSlotFn<absl::TransparentHash<int32_t, int64_t>, - int32_t, - /*kIsAbsl=*/true, 0>())); - EXPECT_NE(fn, (GetTypeErasedApplyToSlotFn<absl::Hash<int32_t>, int32_t, - /*kIsAbsl=*/false, 0>())); - MyEnum c = MyEnum::kTwo; - size_t seed = 100; - EXPECT_EQ(fn(nullptr, &c, seed), - (HashElement<absl::Hash<MyEnum>, /*kIsAbsl=*/true, 0>( - absl::Hash<MyEnum>{}, seed)(c))); -} - -TEST(ApplyTest, TypeErasedDerefAndApplyToSlotFn) { - size_t x = 7; - size_t seed = 100; - auto fn = [](size_t v) { return v * 2; }; - size_t* x_ptr = &x; - EXPECT_EQ( - (TypeErasedDerefAndApplyToSlotFn<decltype(fn), size_t, - /*kIsAbsl=*/false, - /*kSeedShift=*/0>(&fn, &x_ptr, seed)), - (HashElement<decltype(fn), /*kIsAbsl=*/false, /*kSeedShift=*/0>( - fn, seed)(x))); -} - -TEST(ApplyTest, GetTypeErasedDerefAndApplyToSlotFnCanonicalizesTypes) { - enum class MyEnum : uint32_t { kOne = 1, kTwo = 2 }; - HashSlotFn fn = - GetTypeErasedDerefAndApplyToSlotFn<absl::Hash<int32_t>, int32_t, - /*kIsAbsl=*/true, 0>(); - EXPECT_EQ(fn, - (GetTypeErasedDerefAndApplyToSlotFn<absl::Hash<uint32_t>, uint32_t, - /*kIsAbsl=*/true, 0>())); - EXPECT_EQ(fn, (GetTypeErasedDerefAndApplyToSlotFn<absl::Hash<MyEnum>, MyEnum, - /*kIsAbsl=*/true, 0>())); - EXPECT_EQ(fn, (GetTypeErasedDerefAndApplyToSlotFn< - absl::TransparentHash<int32_t, int64_t>, int32_t, - /*kIsAbsl=*/true, 0>())); - EXPECT_NE(fn, - (GetTypeErasedDerefAndApplyToSlotFn<absl::Hash<int32_t>, int32_t, - /*kIsAbsl=*/false, 0>())); + int32_t, /*kIsAbsl=*/true, 0, + kDerefSlot>())); + EXPECT_NE( + fn, (GetTypeErasedApplyToSlotFn<absl::Hash<int32_t>, int32_t, + /*kIsAbsl=*/false, 0, kDerefSlot>())); + EXPECT_NE( + fn, (GetTypeErasedApplyToSlotFn<absl::Hash<int32_t>, int32_t, + /*kIsAbsl=*/true, 0, !kDerefSlot>())); MyEnum c = MyEnum::kTwo; MyEnum* c_ptr = &c; size_t seed = 100; - EXPECT_EQ(fn(nullptr, &c_ptr, seed), + void* slot = kDerefSlot ? static_cast<void*>(&c_ptr) : static_cast<void*>(&c); + EXPECT_EQ(fn(nullptr, slot, seed), (HashElement<absl::Hash<MyEnum>, /*kIsAbsl=*/true, 0>( absl::Hash<MyEnum>{}, seed)(c))); } +TEST(ApplyTest, GetTypeErasedApplyToSlotFnCanonicalizesTypes) { + ExpectGetTypeErasedApplyToSlotFnCanonicalizesTypes<false>(); + ExpectGetTypeErasedApplyToSlotFnCanonicalizesTypes<true>(); +} + TEST(HashKeyTest, NonCanonicalUsesHasher) { auto hash = [](int32_t v) { return static_cast<size_t>(v + 42); }; using HashKeyCustom = HashKey<decltype(hash), int32_t, /*kIsAbsl=*/false,
diff --git a/absl/container/node_hash_map.h b/absl/container/node_hash_map.h index 3512a1c..dd8431c 100644 --- a/absl/container/node_hash_map.h +++ b/absl/container/node_hash_map.h
@@ -686,7 +686,8 @@ template <class Hash, bool kIsAbsl, size_t kSeedShift> static constexpr HashSlotFn get_hash_slot_fn() { static_assert(memory_internal::OffsetOf<value_type>::kFirst == 0); - return GetTypeErasedDerefAndApplyToSlotFn<Hash, Key, kIsAbsl, kSeedShift>(); + return GetTypeErasedApplyToSlotFn<Hash, Key, kIsAbsl, kSeedShift, + /*kDerefSlot=*/true>(); } }; } // namespace container_internal
diff --git a/absl/container/node_hash_set.h b/absl/container/node_hash_set.h index e211e33..3bbdcb0 100644 --- a/absl/container/node_hash_set.h +++ b/absl/container/node_hash_set.h
@@ -586,7 +586,8 @@ template <class Hash, bool kIsAbsl, size_t kSeedShift> static constexpr HashSlotFn get_hash_slot_fn() { - return GetTypeErasedDerefAndApplyToSlotFn<Hash, T, kIsAbsl, kSeedShift>(); + return GetTypeErasedApplyToSlotFn<Hash, T, kIsAbsl, kSeedShift, + /*kDerefSlot=*/true>(); } }; } // namespace container_internal