Commit 9a18cc1a by Evan Brown Committed by Copybara-Service

Add an explicit tag for non-SOO CommonFields (removing default ctor) and add a…

Add an explicit tag for non-SOO CommonFields (removing default ctor) and add a small optimization for early return in AssertNotDebugCapacity.

PiperOrigin-RevId: 675640181
Change-Id: I5ab4cc6eaa0b54932ed70e42ed654f912cf1b099
parent 857fa4fa
...@@ -338,7 +338,7 @@ void ClearBackingArray(CommonFields& c, const PolicyFunctions& policy, ...@@ -338,7 +338,7 @@ void ClearBackingArray(CommonFields& c, const PolicyFunctions& policy,
c.infoz().RecordClearedReservation(); c.infoz().RecordClearedReservation();
c.infoz().RecordStorageChanged(0, soo_enabled ? SooCapacity() : 0); c.infoz().RecordStorageChanged(0, soo_enabled ? SooCapacity() : 0);
(*policy.dealloc)(c, policy); (*policy.dealloc)(c, policy);
c = soo_enabled ? CommonFields{soo_tag_t{}} : CommonFields{}; c = soo_enabled ? CommonFields{soo_tag_t{}} : CommonFields{non_soo_tag_t{}};
} }
} }
......
...@@ -1245,6 +1245,8 @@ constexpr size_t SooCapacity() { return 1; } ...@@ -1245,6 +1245,8 @@ constexpr size_t SooCapacity() { return 1; }
struct soo_tag_t {}; struct soo_tag_t {};
// Sentinel type to indicate SOO CommonFields construction with full size. // Sentinel type to indicate SOO CommonFields construction with full size.
struct full_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. // Sentinel value to indicate non-SOO construction for moved-from values.
struct moved_from_non_soo_tag_t {}; struct moved_from_non_soo_tag_t {};
// Sentinel value to indicate an uninitialized CommonFields for use in swapping. // Sentinel value to indicate an uninitialized CommonFields for use in swapping.
...@@ -1330,10 +1332,11 @@ union HeapOrSoo { ...@@ -1330,10 +1332,11 @@ union HeapOrSoo {
// of this state to helper functions as a single argument. // of this state to helper functions as a single argument.
class CommonFields : public CommonFieldsGenerationInfo { class CommonFields : public CommonFieldsGenerationInfo {
public: public:
CommonFields() : capacity_(0), size_(0), heap_or_soo_(EmptyGroup()) {}
explicit CommonFields(soo_tag_t) : capacity_(SooCapacity()), size_(0) {} explicit CommonFields(soo_tag_t) : capacity_(SooCapacity()), size_(0) {}
explicit CommonFields(full_soo_tag_t) explicit CommonFields(full_soo_tag_t)
: capacity_(SooCapacity()), size_(size_t{1} << HasInfozShift()) {} : 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_. // For non-SOO moved-from values, we only need to initialize capacity_.
explicit CommonFields(moved_from_non_soo_tag_t) : capacity_(0) {} explicit CommonFields(moved_from_non_soo_tag_t) : capacity_(0) {}
// For use in swapping. // For use in swapping.
...@@ -1349,7 +1352,8 @@ class CommonFields : public CommonFieldsGenerationInfo { ...@@ -1349,7 +1352,8 @@ class CommonFields : public CommonFieldsGenerationInfo {
template <bool kSooEnabled> template <bool kSooEnabled>
static CommonFields CreateDefault() { static CommonFields CreateDefault() {
return kSooEnabled ? CommonFields{soo_tag_t{}} : CommonFields{}; return kSooEnabled ? CommonFields{soo_tag_t{}}
: CommonFields{non_soo_tag_t{}};
} }
template <bool kSooEnabled> template <bool kSooEnabled>
static CommonFields CreateMovedFrom() { static CommonFields CreateMovedFrom() {
...@@ -3932,6 +3936,10 @@ class raw_hash_set { ...@@ -3932,6 +3936,10 @@ class raw_hash_set {
// Asserts that the capacity is not a sentinel invalid value. // Asserts that the capacity is not a sentinel invalid value.
void AssertNotDebugCapacity() const { void AssertNotDebugCapacity() const {
if (ABSL_PREDICT_TRUE(capacity() <
InvalidCapacity::kAboveMaxValidCapacity)) {
return;
}
assert(capacity() != InvalidCapacity::kReentrance && assert(capacity() != InvalidCapacity::kReentrance &&
"Reentrant container access during element construction/destruction " "Reentrant container access during element construction/destruction "
"is not allowed."); "is not allowed.");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment