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
pull/1272/merge
Abseil Team 2 years ago committed by Copybara-Service
parent 3a35cb3dff
commit 1b976982d8
  1. 24
      absl/container/internal/raw_hash_set.h

@ -1364,7 +1364,7 @@ class raw_hash_set {
typename AllocTraits::propagate_on_container_move_assignment()); typename AllocTraits::propagate_on_container_move_assignment());
} }
~raw_hash_set() { destroy_slots(); } ~raw_hash_set() { destroy_slots(/*reset=*/false); }
iterator begin() { iterator begin() {
auto it = iterator_at(0); auto it = iterator_at(0);
@ -1394,7 +1394,7 @@ class raw_hash_set {
// largest bucket_count() threshold for which iteration is still fast and // largest bucket_count() threshold for which iteration is still fast and
// past that we simply deallocate the array. // past that we simply deallocate the array.
if (capacity_ > 127) { if (capacity_ > 127) {
destroy_slots(); destroy_slots(/*reset=*/true);
infoz().RecordClearedReservation(); infoz().RecordClearedReservation();
} else if (capacity_) { } else if (capacity_) {
@ -1707,7 +1707,7 @@ class raw_hash_set {
void rehash(size_t n) { void rehash(size_t n) {
if (n == 0 && capacity_ == 0) return; if (n == 0 && capacity_ == 0) return;
if (n == 0 && size_ == 0) { if (n == 0 && size_ == 0) {
destroy_slots(); destroy_slots(/*reset=*/true);
infoz().RecordStorageChanged(0, 0); infoz().RecordStorageChanged(0, 0);
infoz().RecordClearedReservation(); infoz().RecordClearedReservation();
return; return;
@ -1985,11 +1985,11 @@ class raw_hash_set {
infoz().RecordStorageChanged(size_, capacity_); infoz().RecordStorageChanged(size_, capacity_);
} }
// Destroys all slots in the backing array, frees the backing array, and // Destroys all slots in the backing array, frees the backing array,
// clears all top-level book-keeping data. // If reset is true, also clears all top-level book-keeping data.
// //
// This essentially implements `map = raw_hash_set();`. // This essentially implements `map = raw_hash_set();`.
void destroy_slots() { void destroy_slots(bool reset) {
if (!capacity_) return; if (!capacity_) return;
for (size_t i = 0; i != capacity_; ++i) { for (size_t i = 0; i != capacity_; ++i) {
if (IsFull(ctrl_[i])) { if (IsFull(ctrl_[i])) {
@ -2002,11 +2002,13 @@ class raw_hash_set {
Deallocate<alignof(slot_type)>( Deallocate<alignof(slot_type)>(
&alloc_ref(), ctrl_, &alloc_ref(), ctrl_,
AllocSize(capacity_, sizeof(slot_type), alignof(slot_type))); AllocSize(capacity_, sizeof(slot_type), alignof(slot_type)));
ctrl_ = EmptyGroup(); if (reset) {
slots_ = nullptr; ctrl_ = EmptyGroup();
size_ = 0; slots_ = nullptr;
capacity_ = 0; size_ = 0;
growth_left() = 0; capacity_ = 0;
growth_left() = 0;
}
} }
void resize(size_t new_capacity) { void resize(size_t new_capacity) {

Loading…
Cancel
Save