aboutsummaryrefslogtreecommitdiff
path: root/absl/container/flat_hash_map_test.cc
diff options
context:
space:
mode:
authorAbseil Team <absl-team@google.com>2023-11-20 14:57:43 -0800
committerCopybara-Service <copybara-worker@google.com>2023-11-20 14:58:49 -0800
commitdb5c79932e16e97e8b2f9ecd0e74f99f0e74e0d7 (patch)
treef97c66bd57931a32ba1cb4c7a222f7003d9f359d /absl/container/flat_hash_map_test.cc
parentf393335cb7410fc3a88f41dc5dd878c020213e0b (diff)
downloadabseil-db5c79932e16e97e8b2f9ecd0e74f99f0e74e0d7.tar.gz
abseil-db5c79932e16e97e8b2f9ecd0e74f99f0e74e0d7.tar.bz2
abseil-db5c79932e16e97e8b2f9ecd0e74f99f0e74e0d7.zip
Make `FlatHashMapPolicy` return `std::true_type` for relocatable objects.
This reduces produced binary size and can trigger even more optimizations in the future. PiperOrigin-RevId: 584136517 Change-Id: I3854833799f88f28b755ec53132925f0c3d468ab
Diffstat (limited to 'absl/container/flat_hash_map_test.cc')
-rw-r--r--absl/container/flat_hash_map_test.cc38
1 files changed, 35 insertions, 3 deletions
diff --git a/absl/container/flat_hash_map_test.cc b/absl/container/flat_hash_map_test.cc
index e6acbea2..d90fe9d5 100644
--- a/absl/container/flat_hash_map_test.cc
+++ b/absl/container/flat_hash_map_test.cc
@@ -14,14 +14,20 @@
#include "absl/container/flat_hash_map.h"
+#include <cstddef>
#include <memory>
+#include <type_traits>
+#include <utility>
+#include <vector>
+#include "gtest/gtest.h"
#include "absl/container/internal/hash_generator_testing.h"
#include "absl/container/internal/unordered_map_constructor_test.h"
#include "absl/container/internal/unordered_map_lookup_test.h"
#include "absl/container/internal/unordered_map_members_test.h"
#include "absl/container/internal/unordered_map_modifiers_test.h"
#include "absl/log/check.h"
+#include "absl/meta/type_traits.h"
#include "absl/types/any.h"
namespace absl {
@@ -102,6 +108,34 @@ TEST(FlatHashMap, StandardLayout) {
}
}
+TEST(FlatHashMap, Relocatability) {
+ static_assert(absl::is_trivially_relocatable<int>::value, "");
+ static_assert(
+ absl::is_trivially_relocatable<std::pair<const int, int>>::value, "");
+ static_assert(
+ std::is_same<decltype(absl::container_internal::FlatHashMapPolicy<
+ int, int>::transfer<std::allocator<char>>(nullptr,
+ nullptr,
+ nullptr)),
+ std::true_type>::value,
+ "");
+
+ struct NonRelocatable {
+ NonRelocatable() = default;
+ NonRelocatable(NonRelocatable&&) {}
+ NonRelocatable& operator=(NonRelocatable&&) { return *this; }
+ void* self = nullptr;
+ };
+
+ EXPECT_FALSE(absl::is_trivially_relocatable<NonRelocatable>::value);
+ EXPECT_TRUE(
+ (std::is_same<decltype(absl::container_internal::FlatHashMapPolicy<
+ int, NonRelocatable>::
+ transfer<std::allocator<char>>(nullptr, nullptr,
+ nullptr)),
+ std::false_type>::value));
+}
+
// gcc becomes unhappy if this is inside the method, so pull it out here.
struct balast {};
@@ -150,9 +184,7 @@ struct Hash {
struct Eq {
using is_transparent = void;
- bool operator()(size_t lhs, size_t rhs) const {
- return lhs == rhs;
- }
+ bool operator()(size_t lhs, size_t rhs) const { return lhs == rhs; }
bool operator()(size_t lhs, const LazyInt& rhs) const {
return lhs == rhs.value;
}