Include pointer types in `absl::hash_internal::is_uniquely_represented` and disallow custom pointer `AbslHashValue` overloads. Including `T*` in `is_uniquely_represented<T>` hashes raw pointers via `UniquelyRepresentedProbe` and causes `CanonicalHasher<T*>()` to return `Hash<uintptr_t>()` so pointer hash tables share the canonical pointer/integer slot and key hash functions, policy function tables, and growth transfer functions. Also add a `static_assert` in `UniquelyRepresentedProbe::Invoke` disallowing custom `AbslHashValue` overloads on pointer types, as pointers are always hashed by address. PiperOrigin-RevId: 1011612119
diff --git a/absl/container/internal/raw_hash_set_test.cc b/absl/container/internal/raw_hash_set_test.cc index 2a71416..62f46c2 100644 --- a/absl/container/internal/raw_hash_set_test.cc +++ b/absl/container/internal/raw_hash_set_test.cc
@@ -5615,6 +5615,9 @@ EXPECT_EQ( intptr_pair_policy, (GetPolicyFunctionsPtr<absl::flat_hash_map<EnumPtr, const void*>>())); + EXPECT_EQ( + intptr_pair_policy, + (GetPolicyFunctionsPtr<absl::flat_hash_map<const void*, intptr_t>>())); // Different slot size. EXPECT_NE(intptr_pair_policy,
diff --git a/absl/hash/hash_test.cc b/absl/hash/hash_test.cc index e42e1a0..7bca928 100644 --- a/absl/hash/hash_test.cc +++ b/absl/hash/hash_test.cc
@@ -930,6 +930,19 @@ t.value + static_cast<int>(InvokeTag::kHashValue)); } +struct NonTriviallyCopyableUniquelyRepresented { + uintptr_t v; + explicit NonTriviallyCopyableUniquelyRepresented(uintptr_t v) : v(v) {} + // NOLINTNEXTLINE(modernize-use-equals-default) + NonTriviallyCopyableUniquelyRepresented( + const NonTriviallyCopyableUniquelyRepresented& other) + : v(other.v) {} + friend bool operator==(const NonTriviallyCopyableUniquelyRepresented& a, + const NonTriviallyCopyableUniquelyRepresented& b) { + return a.v == b.v; + } +}; + } // namespace namespace absl { @@ -940,6 +953,10 @@ CustomHashType<Tags...>, typename EnableIfContained<InvokeTag::kUniquelyRepresented, Tags...>::type> : std::true_type {}; + +template <> +struct is_uniquely_represented<NonTriviallyCopyableUniquelyRepresented> + : std::true_type {}; } // namespace hash_internal ABSL_NAMESPACE_END } // namespace absl @@ -1118,7 +1135,15 @@ EXPECT_TRUE(is_uniquely_represented<unsigned char>::value); EXPECT_TRUE(is_uniquely_represented<int>::value); EXPECT_FALSE(is_uniquely_represented<bool>::value); - EXPECT_FALSE(is_uniquely_represented<int*>::value); + EXPECT_TRUE(is_uniquely_represented<int*>::value); +} + +TEST(IsUniquelyRepresentedTest, NonTriviallyCopyableInFlatHashMap) { + absl::flat_hash_map<NonTriviallyCopyableUniquelyRepresented, int> m; + m[NonTriviallyCopyableUniquelyRepresented{1}] = 10; + m.emplace(NonTriviallyCopyableUniquelyRepresented{2}, 20); + EXPECT_EQ(m.find(NonTriviallyCopyableUniquelyRepresented{1})->second, 10); + EXPECT_EQ(m[NonTriviallyCopyableUniquelyRepresented{2}], 20); } struct IntAndString {
diff --git a/absl/hash/internal/canonical_hash.h b/absl/hash/internal/canonical_hash.h index fe98837..5a467ce 100644 --- a/absl/hash/internal/canonical_hash.h +++ b/absl/hash/internal/canonical_hash.h
@@ -38,12 +38,22 @@ std::conditional_t<N == 4, uint32_t, std::conditional_t<N == 8, uint64_t, void>>>>; +// True if `T` can be copied as raw bytes without running a copy constructor or +// destructor. Unlike `std::is_trivially_copyable`, this does not require a +// trivial `operator=`, so it is true for `std::pair<int, int>` while still +// excluding Objective-C ARC pointers. +template <typename T> +using IsTriviallyCopyConstructibleAndDestructible = + std::bool_constant<std::is_trivially_copy_constructible_v<T> && + std::is_trivially_destructible_v<T>>; + // Returns a canonical `absl::Hash` for `T`. Types with the exact same hash // implementation (such as `is_uniquely_represented` types of the same size) // return the same hasher type, which is useful for type erasure. template <typename T> constexpr auto CanonicalHasher() { if constexpr (is_uniquely_represented<T>::value && + IsTriviallyCopyConstructibleAndDestructible<T>::value && FitsIn64BitsPowerOf2<T>::value) { return Hash<UintOfSizeT<sizeof(T)>>(); } else { @@ -59,9 +69,7 @@ static constexpr bool ShouldPassByValue() { return FitsIn64BitsPowerOf2<T>::value && sizeof(T) <= sizeof(bytes) && alignof(T) <= alignof(EncodedHashKey) && - // Required for `memcpy` (false for types with non-trivial - // `operator=` or destructor, such as `std::pair`). - std::is_trivially_copyable_v<T>; + IsTriviallyCopyConstructibleAndDestructible<T>::value; } template <class T>
diff --git a/absl/hash/internal/canonical_hash_test.cc b/absl/hash/internal/canonical_hash_test.cc index c9e2f71..4c6c669 100644 --- a/absl/hash/internal/canonical_hash_test.cc +++ b/absl/hash/internal/canonical_hash_test.cc
@@ -30,6 +30,18 @@ namespace absl { ABSL_NAMESPACE_BEGIN namespace hash_internal { + +struct NonTriviallyCopyableKey { + uintptr_t v; + explicit NonTriviallyCopyableKey(uintptr_t v) : v(v) {} + // NOLINTNEXTLINE(modernize-use-equals-default) + NonTriviallyCopyableKey(const NonTriviallyCopyableKey& other) : v(other.v) {} +}; + +template <> +struct is_uniquely_represented<NonTriviallyCopyableKey> // NOLINT + : std::true_type {}; + namespace { TEST(CanonicalHasherTest, UniquelyRepresentedTypesShareHasher) { @@ -69,12 +81,20 @@ EXPECT_TRUE((std::is_same_v<decltype(CanonicalHasher<Enum64>()), absl::Hash<uint64_t>>)); + EXPECT_TRUE((std::is_same_v<decltype(CanonicalHasher<double*>()), + decltype(CanonicalHasher<uintptr_t>())>)); + EXPECT_TRUE((std::is_same_v<decltype(CanonicalHasher<const int*>()), + decltype(CanonicalHasher<uintptr_t>())>)); + EXPECT_TRUE((std::is_same_v<decltype(CanonicalHasher<std::string>()), absl::Hash<std::string>>)); EXPECT_TRUE((std::is_same_v<decltype(CanonicalHasher<std::array<char, 3>>()), absl::Hash<std::array<char, 3>>>)); EXPECT_TRUE((std::is_same_v<decltype(CanonicalHasher<std::array<char, 5>>()), absl::Hash<std::array<char, 5>>>)); + EXPECT_TRUE( + (std::is_same_v<decltype(CanonicalHasher<NonTriviallyCopyableKey>()), + absl::Hash<NonTriviallyCopyableKey>>)); } TEST(CanonicalHashKeyTest, UniquelyRepresentedTypesShareHashFn) { @@ -84,6 +104,8 @@ EXPECT_EQ(HashKeyInt32::get_hash_fn(), HashKeyEnum::get_hash_fn()); EXPECT_NE(HashKeyInt32::get_hash_fn(), CanonicalHashKey<int8_t>::get_hash_fn()); + EXPECT_EQ(CanonicalHashKey<int*>::get_hash_fn(), + CanonicalHashKey<uintptr_t>::get_hash_fn()); const MyEnum c = MyEnum::kTwo; const size_t seed = 100; @@ -108,6 +130,15 @@ std::string s = "hello"; CanonicalHashKey<std::string> hk_str{s}; EXPECT_EQ(hk_str.data().ToPtr<std::string>(), &s); + + // Non-trivially-copyable uniquely-represented keys are passed by address. + NonTriviallyCopyableKey ntc{123}; + CanonicalHashKey<NonTriviallyCopyableKey> hk_ntc{ntc}; + EXPECT_EQ(hk_ntc.data().ToPtr<NonTriviallyCopyableKey>(), &ntc); + EXPECT_EQ( + CanonicalHashKey<NonTriviallyCopyableKey>::get_hash_fn()(hk_ntc.data(), + 100), + HashWithSeed().hash(absl::Hash<NonTriviallyCopyableKey>{}, ntc, 100)); } struct NonDefaultConstructibleSmallKey { @@ -132,6 +163,7 @@ // Disengaged std::optional has uninitialized payload and padding bytes. check(std::optional<int16_t>(std::nullopt)); check(NonDefaultConstructibleSmallKey{123}); + check(std::pair<int32_t, int16_t>{1, 2}); } } // namespace
diff --git a/absl/hash/internal/hash.h b/absl/hash/internal/hash.h index 861e18e..8aef023 100644 --- a/absl/hash/internal/hash.h +++ b/absl/hash/internal/hash.h
@@ -338,7 +338,7 @@ // `is_uniquely_represented` is like `std::has_unique_object_representations`, // but with a few differences for legacy reasons that predate cl/989071457, such -// as disallowing pointers or Booleans. +// as disallowing Booleans. // // A type is "uniquely represented" if two equal values of that type are // guaranteed to have the same bytes in their underlying storage. In other @@ -370,10 +370,15 @@ // to support certain SFINAE techniques when defining specializations. template <typename T, typename Enable = void> struct is_uniquely_represented - : std::conditional_t<(std::is_integral_v<T> && !std::is_same_v<bool, T>) || - std::is_enum_v<T>, - std::has_unique_object_representations<T>, - std::false_type> {}; + : std::conditional_t< + (std::is_integral_v<T> && !std::is_same_v<bool, T>) || + std::is_enum_v<T> || std::is_pointer_v<T>, + // Use `void*` for pointers because Clang's + // `has_unique_object_representations` returns false for Objective-C + // object pointers. + std::has_unique_object_representations< + std::conditional_t<std::is_pointer_v<T>, void*, T>>, + std::false_type> {}; #ifdef ABSL_HAVE_INTRINSIC_INT128 // Specialize the trait for GNU extension types. @@ -527,16 +532,10 @@ return hash_state; } -// AbslHashValue() for hashing pointers +// This overload prevents `char*` from matching the `absl::string_view` +// overload. template <typename H, typename T> -std::enable_if_t<std::is_pointer_v<T>, H> AbslHashValue(H hash_state, T ptr) { - auto v = reinterpret_cast<uintptr_t>(ptr); - // Due to alignment, pointers tend to have low bits as zero, and the next few - // bits follow a pattern since they are also multiples of some base value. - // The PointerAlignment test verifies that our mixing is good enough to handle - // these cases. - return H::combine(std::move(hash_state), v); -} +void AbslHashValue(H hash_state, const T* ptr); // AbslHashValue() for hashing nullptr_t template <typename H> @@ -1389,6 +1388,16 @@ struct UniquelyRepresentedProbe { template <typename H, typename T> static H Invoke(H state, const T& value) { + if constexpr (std::is_pointer_v<T>) { + static_assert( + !std::is_same_v<State, + typename AbslHashValueResultOrVoid< + State, std::remove_const_t< + std::remove_pointer_t<T>>*>::type>, + "Pointer types cannot have custom AbslHashValue overloads. " + "All pointers are hashed by address; wrap the pointer in a struct " + "or use a custom hash functor to hash the pointee."); + } return hash_internal::hash_bytes(std::move(state), value); } };