Skip to content

Commit

Permalink
Change FieldDescriptor::type() to not have the call_once behavior.
Browse files Browse the repository at this point in the history
This behavior is for lazily build descriptors, and those are for generated code
which always have this field set.
The `type()` function is called extensively in reflection based code and
removing the call_once has significant performance impact.

PiperOrigin-RevId: 625328922
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Apr 16, 2024
1 parent 55696ad commit 4a7b51b
Show file tree
Hide file tree
Showing 3 changed files with 182 additions and 117 deletions.
40 changes: 23 additions & 17 deletions src/google/protobuf/descriptor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2808,7 +2808,7 @@ bool DescriptorPool::TryFindExtensionInFallbackDatabase(
// ===================================================================

bool FieldDescriptor::is_map_message_type() const {
return type_descriptor_.message_type->options().map_entry();
return message_type()->options().map_entry();
}

std::string FieldDescriptor::DefaultValueAsString(
Expand Down Expand Up @@ -6494,12 +6494,8 @@ void DescriptorBuilder::BuildFieldOrExtension(const FieldDescriptorProto& proto,

result->has_json_name_ = proto.has_json_name();

// Some compilers do not allow static_cast directly between two enum types,
// so we must cast to int first.
result->type_ = static_cast<FieldDescriptor::Type>(
absl::implicit_cast<int>(proto.type()));
result->label_ = static_cast<FieldDescriptor::Label>(
absl::implicit_cast<int>(proto.label()));
result->type_ = proto.type();
result->label_ = proto.label();
result->is_repeated_ = result->label_ == FieldDescriptor::LABEL_REPEATED;

if (result->label() == FieldDescriptor::LABEL_REQUIRED) {
Expand Down Expand Up @@ -7379,6 +7375,10 @@ void DescriptorBuilder::CrossLinkField(FieldDescriptor* field,

if (type.IsNull()) {
if (is_lazy) {
ABSL_CHECK(field->type_ == FieldDescriptor::TYPE_MESSAGE ||
field->type_ == FieldDescriptor::TYPE_GROUP ||
field->type_ == FieldDescriptor::TYPE_ENUM)
<< proto;
// Save the symbol names for later for lookup, and allocate the once
// object needed for the accessors.
const std::string& name = proto.type_name();
Expand Down Expand Up @@ -9524,10 +9524,12 @@ void FieldDescriptor::InternalTypeOnceInit() const {
Symbol result = file()->pool()->CrossLinkOnDemandHelper(
lazy_type_name, type_ == FieldDescriptor::TYPE_ENUM);
if (result.type() == Symbol::MESSAGE) {
type_ = FieldDescriptor::TYPE_MESSAGE;
ABSL_CHECK(type_ == FieldDescriptor::TYPE_MESSAGE ||
type_ == FieldDescriptor::TYPE_GROUP)
<< full_name();
type_descriptor_.message_type = result.descriptor();
} else if (result.type() == Symbol::ENUM) {
type_ = FieldDescriptor::TYPE_ENUM;
ABSL_CHECK_EQ(type_, +FieldDescriptor::TYPE_ENUM);
enum_type = type_descriptor_.enum_type = result.enum_descriptor();
}

Expand Down Expand Up @@ -9566,19 +9568,23 @@ void FieldDescriptor::TypeOnceInit(const FieldDescriptor* to_init) {
// all share the same absl::call_once init path to do lazy
// import building and cross linking of a field of a message.
const Descriptor* FieldDescriptor::message_type() const {
if (type_once_) {
absl::call_once(*type_once_, FieldDescriptor::TypeOnceInit, this);
if (type_ == TYPE_MESSAGE || type_ == TYPE_GROUP) {
if (type_once_) {
absl::call_once(*type_once_, FieldDescriptor::TypeOnceInit, this);
}
return type_descriptor_.message_type;
}
return type_ == TYPE_MESSAGE || type_ == TYPE_GROUP
? type_descriptor_.message_type
: nullptr;
return nullptr;
}

const EnumDescriptor* FieldDescriptor::enum_type() const {
if (type_once_) {
absl::call_once(*type_once_, FieldDescriptor::TypeOnceInit, this);
if (type_ == TYPE_ENUM) {
if (type_once_) {
absl::call_once(*type_once_, FieldDescriptor::TypeOnceInit, this);
}
return type_descriptor_.enum_type;
}
return type_ == TYPE_ENUM ? type_descriptor_.enum_type : nullptr;
return nullptr;
}

const EnumValueDescriptor* FieldDescriptor::default_value_enum() const {
Expand Down
5 changes: 1 addition & 4 deletions src/google/protobuf/descriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -1090,7 +1090,7 @@ class PROTOBUF_EXPORT FieldDescriptor : private internal::SymbolBase,
uint8_t label_ : 2;

// Actually a `Type`, but stored as uint8_t to save space.
mutable uint8_t type_;
uint8_t type_;

// Logically:
// all_names_ = [name, full_name, lower, camel, json]
Expand Down Expand Up @@ -2676,9 +2676,6 @@ inline FieldDescriptor::Label FieldDescriptor::label() const {
}

inline FieldDescriptor::Type FieldDescriptor::type() const {
if (type_once_) {
absl::call_once(*type_once_, &FieldDescriptor::TypeOnceInit, this);
}
return static_cast<Type>(type_);
}

Expand Down
Loading

0 comments on commit 4a7b51b

Please sign in to comment.