Skip to content

Commit

Permalink
Devirtualize CheckTypeAndMergeFrom and use the existing function from
Browse files Browse the repository at this point in the history
ClassData.
This removes a virtual function and reduces binary size.

PiperOrigin-RevId: 625685976
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Apr 19, 2024
1 parent 0cda26d commit f293bde
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 98 deletions.
45 changes: 16 additions & 29 deletions src/google/protobuf/compiler/cpp/message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1718,10 +1718,14 @@ void MessageGenerator::GenerateClassDefinition(io::Printer* p) {
}
} else {
p->Emit(R"cc(
void CheckTypeAndMergeFrom(
const ::$proto_ns$::MessageLite& from) final;
void CopyFrom(const $classname$& from);
void MergeFrom(const $classname$& from);
void MergeFrom(const $classname$& from) { $classname$::MergeImpl(*this, from); }
private:
static void MergeImpl(::$proto_ns$::MessageLite& to_msg,
const ::$proto_ns$::MessageLite& from_msg);
public:
)cc");
}

Expand Down Expand Up @@ -3703,10 +3707,10 @@ void MessageGenerator::GenerateClassData(io::Printer* p) {
$table$,
$on_demand_register_arena_dtor$,
$is_initialized$,
&$classname$::MergeImpl,
PROTOBUF_FIELD_OFFSET($classname$, $cached_size$),
false,
},
&$classname$::MergeImpl,
&$classname$::kDescriptorMethods,
&$desc_table$,
$tracker_on_get_metadata$,
Expand All @@ -3732,6 +3736,7 @@ void MessageGenerator::GenerateClassData(io::Printer* p) {
&_table_.header,
$on_demand_register_arena_dtor$,
$is_initialized$,
&$classname$::MergeImpl,
PROTOBUF_FIELD_OFFSET($classname$, $cached_size$),
true,
},
Expand All @@ -3745,17 +3750,7 @@ void MessageGenerator::GenerateClassData(io::Printer* p) {
}

void MessageGenerator::GenerateMergeFrom(io::Printer* p) {
Formatter format(p);

if (!HasDescriptorMethods(descriptor_->file(), options_)) {
// Generate CheckTypeAndMergeFrom().
format(
"void $classname$::CheckTypeAndMergeFrom(\n"
" const ::$proto_ns$::MessageLite& from) {\n"
" MergeFrom(*::_pbi::DownCast<const $classname$*>(\n"
" &from));\n"
"}\n");
}
// DO NOT SUBMIT: REMOVE
}

bool MessageGenerator::RequiresArena(GeneratorFunction function) const {
Expand All @@ -3772,20 +3767,12 @@ void MessageGenerator::GenerateClassSpecificMergeImpl(io::Printer* p) {
// Generate the class-specific MergeFrom, which avoids the ABSL_CHECK and
// cast.
Formatter format(p);
if (!HasDescriptorMethods(descriptor_->file(), options_)) {
// For messages that don't inherit from Message, just implement MergeFrom
// directly.
format(
"void $classname$::MergeFrom(const $classname$& from) {\n"
" $classname$* const _this = this;\n");
} else {
format(
"void $classname$::MergeImpl(::$proto_ns$::MessageLite& to_msg, const "
"::$proto_ns$::MessageLite& from_msg) {\n"
"$WeakDescriptorSelfPin$"
" auto* const _this = static_cast<$classname$*>(&to_msg);\n"
" auto& from = static_cast<const $classname$&>(from_msg);\n");
}
format(
"void $classname$::MergeImpl(::$proto_ns$::MessageLite& to_msg, const "
"::$proto_ns$::MessageLite& from_msg) {\n"
"$WeakDescriptorSelfPin$"
" auto* const _this = static_cast<$classname$*>(&to_msg);\n"
" auto& from = static_cast<const $classname$&>(from_msg);\n");
format.Indent();
if (RequiresArena(GeneratorFunction::kMergeFrom)) {
p->Emit(R"cc(
Expand Down
2 changes: 1 addition & 1 deletion src/google/protobuf/compiler/java/java_features.pb.cc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions src/google/protobuf/compiler/plugin.pb.cc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/google/protobuf/cpp_features.pb.cc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f293bde

Please sign in to comment.