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 7 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_)) { if (!HasDescriptorMethods(descriptor_->file(), options_)) {
// Generate CheckTypeAndMergeFrom(). // Generate CheckTypeAndMergeFrom().
format( // We pass `*this` instead of `default_instance()` to allow for ICF.
"void $classname$::CheckTypeAndMergeFrom(\n" p->Emit(R"cc(
" const ::$proto_ns$::MessageLite& from) {\n" void $classname$::CheckTypeAndMergeFrom(
" MergeFrom(::$proto_ns$::DownCastToGenerated<$classname$>(from));\n" const ::$proto_ns$::MessageLite& from) {
"}\n"); $pbi$::AssertDownCast(from, *this);
MergeFrom(static_cast<const $classname$&>(from));
}
)cc");
} }
} }

@ -863,6 +863,11 @@ T* OnShutdownDelete(T* p) {
return 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 } // namespace internal
std::string ShortFormat(const MessageLite& message_lite); std::string ShortFormat(const MessageLite& message_lite);

Loading…
Cancel
Save