Cache the result of "is_repeated" on its own bit to avoid having to read and

compare the label.
This query is very common when handling descriptors.

PiperOrigin-RevId: 623857268
pull/16477/head
Protobuf Team Bot 2024-04-11 09:57:28 -07:00 committed by Copybara-Service
parent 88e4ec9ea8
commit de5e7b6b8e
2 changed files with 4 additions and 1 deletions

View File

@ -6500,6 +6500,7 @@ void DescriptorBuilder::BuildFieldOrExtension(const FieldDescriptorProto& proto,
absl::implicit_cast<int>(proto.type()));
result->label_ = static_cast<FieldDescriptor::Label>(
absl::implicit_cast<int>(proto.label()));
result->is_repeated_ = result->label_ == FieldDescriptor::LABEL_REPEATED;
if (result->label() == FieldDescriptor::LABEL_REQUIRED) {
// An extension cannot have a required field (b/13365836).

View File

@ -1084,6 +1084,7 @@ class PROTOBUF_EXPORT FieldDescriptor : private internal::SymbolBase,
bool has_json_name_ : 1;
bool is_extension_ : 1;
bool is_oneof_ : 1;
bool is_repeated_ : 1; // Redundant with label_, but it is queried a lot.
// Actually a `Label` but stored as uint8_t to save space.
uint8_t label_ : 2;
@ -2686,7 +2687,8 @@ inline bool FieldDescriptor::is_optional() const {
}
inline bool FieldDescriptor::is_repeated() const {
return label() == LABEL_REPEATED;
ABSL_DCHECK_EQ(is_repeated_, label() == LABEL_REPEATED);
return is_repeated_;
}
inline bool FieldDescriptor::is_packable() const {