Reduce the cost of CheckTypeAndMergeFrom in LITE_RUNTIME objects when icf is

enabled.
DownCastToGenerated injects the strong reference to T, which makes all instances of CheckTypeAndMergeFrom unique and not candidates for identical code folding.
The strong reference is not needed here in the member function.

PiperOrigin-RevId: 634033477
pull/16845/head
Protobuf Team Bot 6 months ago committed by Copybara-Service
parent 6a2e4be010
commit ca995da263
  1. 13
      src/google/protobuf/compiler/cpp/message.cc
  2. 5
      src/google/protobuf/message_lite.h

@ -3744,11 +3744,14 @@ void MessageGenerator::GenerateMergeFrom(io::Printer* p) {
if (!HasDescriptorMethods(descriptor_->file(), options_)) {
// Generate CheckTypeAndMergeFrom().
format(
"void $classname$::CheckTypeAndMergeFrom(\n"
" const ::$proto_ns$::MessageLite& from) {\n"
" MergeFrom(::$proto_ns$::DownCastToGenerated<$classname$>(from));\n"
"}\n");
// We pass `*this` instead of `default_instance()` to allow for ICF.
p->Emit(R"cc(
void $classname$::CheckTypeAndMergeFrom(
const ::$proto_ns$::MessageLite& from) {
$pbi$::AssertDownCast(from, *this);
MergeFrom(static_cast<const $classname$&>(from));
}
)cc");
}
}

@ -863,6 +863,11 @@ T* OnShutdownDelete(T* p) {
return p;
}
inline void AssertDownCast(const MessageLite& from, const MessageLite& to) {
ABSL_DCHECK(internal::TypeId::Get(from) == internal::TypeId::Get(to))
<< "Cannot downcast " << from.GetTypeName() << " to " << to.GetTypeName();
}
} // namespace internal
std::string ShortFormat(const MessageLite& message_lite);

Loading…
Cancel
Save