Skip to content

Commit

Permalink
Refactor swisstable moves.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 683715628
Change-Id: If73080e74c69523a458b2992c1f3740879ff097d
  • Loading branch information
ezbr authored and copybara-github committed Oct 8, 2024
1 parent 03b8d6e commit 8634e35
Showing 1 changed file with 4 additions and 18 deletions.
22 changes: 4 additions & 18 deletions absl/container/internal/raw_hash_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -1247,8 +1247,6 @@ struct soo_tag_t {};
struct full_soo_tag_t {};
// Sentinel type to indicate non-SOO CommonFields construction.
struct non_soo_tag_t {};
// Sentinel value to indicate non-SOO construction for moved-from values.
struct moved_from_non_soo_tag_t {};
// Sentinel value to indicate an uninitialized CommonFields for use in swapping.
struct uninitialized_tag_t {};

Expand Down Expand Up @@ -1337,8 +1335,6 @@ class CommonFields : public CommonFieldsGenerationInfo {
: capacity_(SooCapacity()), size_(size_t{1} << HasInfozShift()) {}
explicit CommonFields(non_soo_tag_t)
: capacity_(0), size_(0), heap_or_soo_(EmptyGroup()) {}
// For non-SOO moved-from values, we only need to initialize capacity_.
explicit CommonFields(moved_from_non_soo_tag_t) : capacity_(0) {}
// For use in swapping.
explicit CommonFields(uninitialized_tag_t) {}

Expand All @@ -1355,10 +1351,6 @@ class CommonFields : public CommonFieldsGenerationInfo {
return kSooEnabled ? CommonFields{soo_tag_t{}}
: CommonFields{non_soo_tag_t{}};
}
template <bool kSooEnabled>
static CommonFields CreateMovedFrom() {
return CreateDefault<kSooEnabled>();
}

// The inline data for SOO is written on top of control_/slots_.
const void* soo_data() const { return heap_or_soo_.get_soo_data(); }
Expand Down Expand Up @@ -1460,12 +1452,6 @@ class CommonFields : public CommonFieldsGenerationInfo {
.alloc_size(slot_size);
}

// Initialize fields that are left uninitialized by moved-from constructor.
void reinitialize_moved_from_non_soo() {
size_ = 0;
heap_or_soo_ = HeapOrSoo(EmptyGroup());
}

// Move fields other than heap_or_soo_.
void move_non_heap_or_soo_fields(CommonFields& that) {
static_cast<CommonFieldsGenerationInfo&>(*this) =
Expand Down Expand Up @@ -2849,7 +2835,7 @@ class raw_hash_set {
if (!PolicyTraits::transfer_uses_memcpy() && that.is_full_soo()) {
transfer(soo_slot(), that.soo_slot());
}
that.common() = CommonFields::CreateMovedFrom<SooEnabled()>();
that.common() = CommonFields::CreateDefault<SooEnabled()>();
annotate_for_bug_detection_on_move(that);
}

Expand Down Expand Up @@ -2952,7 +2938,7 @@ class raw_hash_set {
// past that we simply deallocate the array.
const size_t cap = capacity();
if (cap == 0) {
common().reinitialize_moved_from_non_soo();
// Already guaranteed to be empty; so nothing to do.
} else if (is_soo()) {
if (!empty()) destroy(soo_slot());
common().set_empty_soo();
Expand Down Expand Up @@ -3834,7 +3820,7 @@ class raw_hash_set {
eq_ref() = that.eq_ref();
CopyAlloc(alloc_ref(), that.alloc_ref(),
std::integral_constant<bool, propagate_alloc>());
that.common() = CommonFields::CreateMovedFrom<SooEnabled()>();
that.common() = CommonFields::CreateDefault<SooEnabled()>();
annotate_for_bug_detection_on_move(that);
return *this;
}
Expand All @@ -3848,7 +3834,7 @@ class raw_hash_set {
that.destroy(it.slot());
}
if (!that.is_soo()) that.dealloc();
that.common() = CommonFields::CreateMovedFrom<SooEnabled()>();
that.common() = CommonFields::CreateDefault<SooEnabled()>();
annotate_for_bug_detection_on_move(that);
return *this;
}
Expand Down

0 comments on commit 8634e35

Please sign in to comment.