Use CanonicalHashKey for heterogeneous keys hashed directly by Abseil hashers. Enable kUseCanonical in HashKeyFor for heterogeneous lookup keys that share the table default hasher or are listed in absl::TransparentHash<Ts...>. PiperOrigin-RevId: 1024935256
diff --git a/absl/container/internal/raw_hash_set.h b/absl/container/internal/raw_hash_set.h index 6ee6d3e..37cc2e4 100644 --- a/absl/container/internal/raw_hash_set.h +++ b/absl/container/internal/raw_hash_set.h
@@ -2836,10 +2836,17 @@ is_instance_of<Hash, absl::hash_internal::TransparentHash>::value; } + template <class Hash, class K> + struct IsInTransparentHash : std::false_type {}; + template <class... Ts, class K> + struct IsInTransparentHash<absl::hash_internal::TransparentHash<Ts...>, K> + : std::bool_constant<(std::is_same_v<K, Ts> || ...)> {}; + // Hashing parameters shared by `raw_hash_set` and the type-erased // `find_or_prepare_insert_large`. template <class Hash, class SlotKey> struct HashParams { + using RawSlotKey = absl::remove_cvref_t<SlotKey>; static constexpr bool kIsAbsl = IsAbslHash<Hash, SlotKey>(); // For non-default hashers the seed is xor'ed with the hash value rather // than being used as a seed for the hash function. Because we require @@ -2848,15 +2855,28 @@ // value can be passed directly without a right shift. static constexpr size_t kSeedShift = kIsAbsl ? 0 : HashtableInlineData::kSeedShift; - // When `kIsAbsl` is true, `Hash` hashes `SlotKey` identically to - // `absl::Hash<SlotKey>` (see `GetTypeErasedApplyToSlotFn`), but may convert - // other lookup keys first (e.g. `const char*` for `StringHash`). - template <class K> + template <class K, class NonConstK = absl::remove_cvref_t<K>> using HashKeyFor = - HashKey<Hash, absl::remove_cvref_t<K>, kIsAbsl, kSeedShift, + HashKey<Hash, NonConstK, kIsAbsl, kSeedShift, + // Use canonical hashing only when the hasher is an Abseil + // hasher and does not convert the lookup key before hashing. /*kUseCanonical=*/kIsAbsl && - std::is_same_v<absl::remove_cvref_t<K>, - absl::remove_cvref_t<SlotKey>>>; + // The stored key type is hashed directly without + // conversion. + (std::is_same_v<NonConstK, RawSlotKey> || + // Pointers differing only in pointee constness have the + // same address, unlike derived-to-base conversions. + (std::is_pointer_v<NonConstK> && + std::is_pointer_v<RawSlotKey> && + std::is_same_v< + std::remove_const_t<std::remove_pointer_t<NonConstK>>, + std::remove_const_t< + std::remove_pointer_t<RawSlotKey>>>) || + // Heterogeneous keys sharing the same default hasher are + // also hashed directly without conversion. + std::is_same_v<Hash, hash_default_hash<NonConstK>> || + // TransparentHash hashes each listed type directly. + IsInTransparentHash<Hash, NonConstK>::value)>; }; template <class SlotKey, class Hash, size_t kHasherOffset, class K>
diff --git a/absl/container/internal/raw_hash_set_test.cc b/absl/container/internal/raw_hash_set_test.cc index 970420e..46dc59a 100644 --- a/absl/container/internal/raw_hash_set_test.cc +++ b/absl/container/internal/raw_hash_set_test.cc
@@ -79,6 +79,8 @@ namespace container_internal { struct RawHashSetTestOnlyAccess { + template <typename C, typename K> + using HashKeyFor = typename C::raw_hash_set::template HashKeyFor<K>; template <typename C> static auto GetCommon(C&& c) -> decltype(std::forward<C>(c).common()) { return std::forward<C>(c).common(); @@ -4183,6 +4185,55 @@ (std::is_same_v<node_type::allocator_type, StringTable::allocator_type>)); } +TEST(Table, HeterogeneousDefaultHashUsesCanonicalHashKey) { + using StringSet = absl::flat_hash_set<std::string>; + EXPECT_TRUE( + (std::is_base_of_v< + absl::hash_internal::CanonicalHashKey<absl::string_view>, + RawHashSetTestOnlyAccess::HashKeyFor<StringSet, absl::string_view>>)); + EXPECT_TRUE((std::is_base_of_v< + absl::hash_internal::CanonicalHashKey<absl::Cord>, + RawHashSetTestOnlyAccess::HashKeyFor<StringSet, absl::Cord>>)); + EXPECT_FALSE((std::is_base_of_v< + absl::hash_internal::CanonicalHashKey<const char*>, + RawHashSetTestOnlyAccess::HashKeyFor<StringSet, const char*>>)); + + using UniquePtrSet = absl::flat_hash_set<std::unique_ptr<int>>; + EXPECT_TRUE((std::is_base_of_v< + absl::hash_internal::CanonicalHashKey<int*>, + RawHashSetTestOnlyAccess::HashKeyFor<UniquePtrSet, int*>>)); + + struct Base1 { + int a; + }; + struct Base2 { + int b; + }; + struct Derived : Base1, Base2 {}; + using PtrSet = absl::flat_hash_set<Base2*>; + using ConstPtrSet = absl::flat_hash_set<const Base2*>; + EXPECT_TRUE((std::is_base_of_v< + absl::hash_internal::CanonicalHashKey<const Base2*>, + RawHashSetTestOnlyAccess::HashKeyFor<PtrSet, const Base2*>>)); + EXPECT_TRUE((std::is_base_of_v< + absl::hash_internal::CanonicalHashKey<Base2*>, + RawHashSetTestOnlyAccess::HashKeyFor<ConstPtrSet, Base2*>>)); + EXPECT_FALSE((std::is_base_of_v< + absl::hash_internal::CanonicalHashKey<Derived*>, + RawHashSetTestOnlyAccess::HashKeyFor<ConstPtrSet, Derived*>>)); + + using TransparentIntSet = + absl::flat_hash_set<int64_t, absl::TransparentHash<int64_t, int>, + std::equal_to<>>; + EXPECT_TRUE((std::is_base_of_v< + absl::hash_internal::CanonicalHashKey<int>, + RawHashSetTestOnlyAccess::HashKeyFor<TransparentIntSet, int>>)); + EXPECT_FALSE( + (std::is_base_of_v< + absl::hash_internal::CanonicalHashKey<int16_t>, + RawHashSetTestOnlyAccess::HashKeyFor<TransparentIntSet, int16_t>>)); +} + TEST(Nodes, ExtractInsert) { constexpr char k0[] = "Very long string zero."; constexpr char k1[] = "Very long string one.";