Skip to content

Commit

Permalink
Add an explicit tag for non-SOO CommonFields (removing default ctor) …
Browse files Browse the repository at this point in the history
…and add a small optimization for early return in AssertNotDebugCapacity.

PiperOrigin-RevId: 675640181
Change-Id: I5ab4cc6eaa0b54932ed70e42ed654f912cf1b099
  • Loading branch information
ezbr authored and copybara-github committed Sep 17, 2024
1 parent 857fa4f commit 9a18cc1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion absl/container/internal/raw_hash_set.cc
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ void ClearBackingArray(CommonFields& c, const PolicyFunctions& policy,
c.infoz().RecordClearedReservation();
c.infoz().RecordStorageChanged(0, soo_enabled ? SooCapacity() : 0);
(*policy.dealloc)(c, policy);
c = soo_enabled ? CommonFields{soo_tag_t{}} : CommonFields{};
c = soo_enabled ? CommonFields{soo_tag_t{}} : CommonFields{non_soo_tag_t{}};
}
}

Expand Down
12 changes: 10 additions & 2 deletions absl/container/internal/raw_hash_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -1245,6 +1245,8 @@ constexpr size_t SooCapacity() { return 1; }
struct soo_tag_t {};
// Sentinel type to indicate SOO CommonFields construction with full size.
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.
Expand Down Expand Up @@ -1330,10 +1332,11 @@ union HeapOrSoo {
// of this state to helper functions as a single argument.
class CommonFields : public CommonFieldsGenerationInfo {
public:
CommonFields() : capacity_(0), size_(0), heap_or_soo_(EmptyGroup()) {}
explicit CommonFields(soo_tag_t) : capacity_(SooCapacity()), size_(0) {}
explicit CommonFields(full_soo_tag_t)
: 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.
Expand All @@ -1349,7 +1352,8 @@ class CommonFields : public CommonFieldsGenerationInfo {

template <bool kSooEnabled>
static CommonFields CreateDefault() {
return kSooEnabled ? CommonFields{soo_tag_t{}} : CommonFields{};
return kSooEnabled ? CommonFields{soo_tag_t{}}
: CommonFields{non_soo_tag_t{}};
}
template <bool kSooEnabled>
static CommonFields CreateMovedFrom() {
Expand Down Expand Up @@ -3932,6 +3936,10 @@ class raw_hash_set {

// Asserts that the capacity is not a sentinel invalid value.
void AssertNotDebugCapacity() const {
if (ABSL_PREDICT_TRUE(capacity() <
InvalidCapacity::kAboveMaxValidCapacity)) {
return;
}
assert(capacity() != InvalidCapacity::kReentrance &&
"Reentrant container access during element construction/destruction "
"is not allowed.");
Expand Down

0 comments on commit 9a18cc1

Please sign in to comment.