From 1b976982d8cdbfd8320f34209b065bae7adefb09 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Fri, 11 Nov 2022 13:12:21 -0800 Subject: Stop unnecessary clearing of fields in ~raw_hash_set. Previously, ~raw_hash_set() would change *this to have the same representation as an empty hash table. This is unnecessary since nobody should be touching a destroyed hash table, and prevents future optimizations/changes that might not be able to preserve this behavior. PiperOrigin-RevId: 487899950 Change-Id: I2d4470677bdd411c2e48ef511187e39f4e7fc2f4 --- absl/container/internal/raw_hash_set.h | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) (limited to 'absl/container/internal/raw_hash_set.h') diff --git a/absl/container/internal/raw_hash_set.h b/absl/container/internal/raw_hash_set.h index 1aa89204..de455d6c 100644 --- a/absl/container/internal/raw_hash_set.h +++ b/absl/container/internal/raw_hash_set.h @@ -1364,7 +1364,7 @@ class raw_hash_set { typename AllocTraits::propagate_on_container_move_assignment()); } - ~raw_hash_set() { destroy_slots(); } + ~raw_hash_set() { destroy_slots(/*reset=*/false); } iterator begin() { auto it = iterator_at(0); @@ -1394,7 +1394,7 @@ class raw_hash_set { // largest bucket_count() threshold for which iteration is still fast and // past that we simply deallocate the array. if (capacity_ > 127) { - destroy_slots(); + destroy_slots(/*reset=*/true); infoz().RecordClearedReservation(); } else if (capacity_) { @@ -1707,7 +1707,7 @@ class raw_hash_set { void rehash(size_t n) { if (n == 0 && capacity_ == 0) return; if (n == 0 && size_ == 0) { - destroy_slots(); + destroy_slots(/*reset=*/true); infoz().RecordStorageChanged(0, 0); infoz().RecordClearedReservation(); return; @@ -1985,11 +1985,11 @@ class raw_hash_set { infoz().RecordStorageChanged(size_, capacity_); } - // Destroys all slots in the backing array, frees the backing array, and - // clears all top-level book-keeping data. + // Destroys all slots in the backing array, frees the backing array, + // If reset is true, also clears all top-level book-keeping data. // // This essentially implements `map = raw_hash_set();`. - void destroy_slots() { + void destroy_slots(bool reset) { if (!capacity_) return; for (size_t i = 0; i != capacity_; ++i) { if (IsFull(ctrl_[i])) { @@ -2002,11 +2002,13 @@ class raw_hash_set { Deallocate( &alloc_ref(), ctrl_, AllocSize(capacity_, sizeof(slot_type), alignof(slot_type))); - ctrl_ = EmptyGroup(); - slots_ = nullptr; - size_ = 0; - capacity_ = 0; - growth_left() = 0; + if (reset) { + ctrl_ = EmptyGroup(); + slots_ = nullptr; + size_ = 0; + capacity_ = 0; + growth_left() = 0; + } } void resize(size_t new_capacity) { -- cgit v1.2.3