diff options
author | Abseil Team <absl-team@google.com> | 2024-02-01 11:43:33 -0800 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2024-02-01 11:44:43 -0800 |
commit | 7339447a7f457f1d8efa6322c971e71afc304d32 (patch) | |
tree | 8e7116a29f274315e0257d33e79720a29363165a /absl/container/internal/raw_hash_set_test.cc | |
parent | a3ee6ce2e6eee06941a1e9479720cecbd898f4ca (diff) |
Optimize raw_hash_set destructor.
There are three optimizations here:
1. Early exit in case all slots were destroyed. especially useful for empty tables.
2. MatchFull is used in order to iterate over all full slots.
3. Portable group is used for `MatchFull` in the case of small table.
PiperOrigin-RevId: 603434899
Change-Id: I40bc90d17331d579cfbb1b4e0693f0913e5c38e4
Diffstat (limited to 'absl/container/internal/raw_hash_set_test.cc')
-rw-r--r-- | absl/container/internal/raw_hash_set_test.cc | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/absl/container/internal/raw_hash_set_test.cc b/absl/container/internal/raw_hash_set_test.cc index 7ec72b22..5852904f 100644 --- a/absl/container/internal/raw_hash_set_test.cc +++ b/absl/container/internal/raw_hash_set_test.cc @@ -64,6 +64,10 @@ namespace container_internal { struct RawHashSetTestOnlyAccess { template <typename C> + static auto GetCommon(const C& c) -> decltype(c.common()) { + return c.common(); + } + template <typename C> static auto GetSlots(const C& c) -> decltype(c.slot_array()) { return c.slot_array(); } @@ -2741,6 +2745,37 @@ TEST(Table, CountedHash) { } } +TEST(Table, IterateOverFullSlotsEmpty) { + IntTable t; + auto fail_if_any = [](int64_t* i) { FAIL() << "expected no slots " << i; }; + container_internal::IterateOverFullSlots( + RawHashSetTestOnlyAccess::GetCommon(t), + RawHashSetTestOnlyAccess::GetSlots(t), fail_if_any); + for (size_t i = 0; i < 256; ++i) { + t.reserve(i); + container_internal::IterateOverFullSlots( + RawHashSetTestOnlyAccess::GetCommon(t), + RawHashSetTestOnlyAccess::GetSlots(t), fail_if_any); + } +} + +TEST(Table, IterateOverFullSlotsFull) { + IntTable t; + + std::vector<int64_t> expected_slots; + for (int64_t i = 0; i < 128; ++i) { + t.insert(i); + expected_slots.push_back(i); + + std::vector<int64_t> slots; + container_internal::IterateOverFullSlots( + RawHashSetTestOnlyAccess::GetCommon(t), + RawHashSetTestOnlyAccess::GetSlots(t), + [&slots](int64_t* i) { slots.push_back(*i); }); + EXPECT_THAT(slots, testing::UnorderedElementsAreArray(expected_slots)); + } +} + } // namespace } // namespace container_internal ABSL_NAMESPACE_END |