From f293bde817d97586691b207447c5343218db9066 Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Wed, 17 Apr 2024 08:06:37 -0700 Subject: [PATCH] Devirtualize CheckTypeAndMergeFrom and use the existing function from ClassData. This removes a virtual function and reduces binary size. PiperOrigin-RevId: 625685976 --- src/google/protobuf/compiler/cpp/message.cc | 45 +++++-------- .../compiler/java/java_features.pb.cc | 2 +- src/google/protobuf/compiler/plugin.pb.cc | 8 +-- src/google/protobuf/cpp_features.pb.cc | 2 +- src/google/protobuf/descriptor.pb.cc | 66 +++++++++---------- src/google/protobuf/dynamic_message.cc | 2 +- src/google/protobuf/implicit_weak_message.cc | 10 +++ src/google/protobuf/implicit_weak_message.h | 9 +-- src/google/protobuf/message.cc | 4 -- src/google/protobuf/message.h | 1 - src/google/protobuf/message_lite.cc | 10 +++ src/google/protobuf/message_lite.h | 21 ++---- 12 files changed, 82 insertions(+), 98 deletions(-) diff --git a/src/google/protobuf/compiler/cpp/message.cc b/src/google/protobuf/compiler/cpp/message.cc index 647f804d2043..c8468456526f 100644 --- a/src/google/protobuf/compiler/cpp/message.cc +++ b/src/google/protobuf/compiler/cpp/message.cc @@ -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"); } @@ -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$, @@ -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, }, @@ -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(\n" - " &from));\n" - "}\n"); - } + // DO NOT SUBMIT: REMOVE } bool MessageGenerator::RequiresArena(GeneratorFunction function) const { @@ -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(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(from_msg);\n"); format.Indent(); if (RequiresArena(GeneratorFunction::kMergeFrom)) { p->Emit(R"cc( diff --git a/src/google/protobuf/compiler/java/java_features.pb.cc b/src/google/protobuf/compiler/java/java_features.pb.cc index 799299210bc5..588d9f7596a6 100644 --- a/src/google/protobuf/compiler/java/java_features.pb.cc +++ b/src/google/protobuf/compiler/java/java_features.pb.cc @@ -191,10 +191,10 @@ JavaFeatures::GetClassData() const { &_table_.header, nullptr, // OnDemandRegisterArenaDtor nullptr, // IsInitialized + &JavaFeatures::MergeImpl, PROTOBUF_FIELD_OFFSET(JavaFeatures, _impl_._cached_size_), false, }, - &JavaFeatures::MergeImpl, &JavaFeatures::kDescriptorMethods, &descriptor_table_google_2fprotobuf_2fcompiler_2fjava_2fjava_5ffeatures_2eproto, nullptr, // tracker diff --git a/src/google/protobuf/compiler/plugin.pb.cc b/src/google/protobuf/compiler/plugin.pb.cc index 61d8c33097e5..afefc17595df 100644 --- a/src/google/protobuf/compiler/plugin.pb.cc +++ b/src/google/protobuf/compiler/plugin.pb.cc @@ -367,10 +367,10 @@ Version::GetClassData() const { &_table_.header, nullptr, // OnDemandRegisterArenaDtor nullptr, // IsInitialized + &Version::MergeImpl, PROTOBUF_FIELD_OFFSET(Version, _impl_._cached_size_), false, }, - &Version::MergeImpl, &Version::kDescriptorMethods, &descriptor_table_google_2fprotobuf_2fcompiler_2fplugin_2eproto, nullptr, // tracker @@ -675,10 +675,10 @@ CodeGeneratorRequest::GetClassData() const { &_table_.header, nullptr, // OnDemandRegisterArenaDtor CodeGeneratorRequest::IsInitializedImpl, + &CodeGeneratorRequest::MergeImpl, PROTOBUF_FIELD_OFFSET(CodeGeneratorRequest, _impl_._cached_size_), false, }, - &CodeGeneratorRequest::MergeImpl, &CodeGeneratorRequest::kDescriptorMethods, &descriptor_table_google_2fprotobuf_2fcompiler_2fplugin_2eproto, nullptr, // tracker @@ -1026,10 +1026,10 @@ CodeGeneratorResponse_File::GetClassData() const { &_table_.header, nullptr, // OnDemandRegisterArenaDtor nullptr, // IsInitialized + &CodeGeneratorResponse_File::MergeImpl, PROTOBUF_FIELD_OFFSET(CodeGeneratorResponse_File, _impl_._cached_size_), false, }, - &CodeGeneratorResponse_File::MergeImpl, &CodeGeneratorResponse_File::kDescriptorMethods, &descriptor_table_google_2fprotobuf_2fcompiler_2fplugin_2eproto, nullptr, // tracker @@ -1343,10 +1343,10 @@ CodeGeneratorResponse::GetClassData() const { &_table_.header, nullptr, // OnDemandRegisterArenaDtor nullptr, // IsInitialized + &CodeGeneratorResponse::MergeImpl, PROTOBUF_FIELD_OFFSET(CodeGeneratorResponse, _impl_._cached_size_), false, }, - &CodeGeneratorResponse::MergeImpl, &CodeGeneratorResponse::kDescriptorMethods, &descriptor_table_google_2fprotobuf_2fcompiler_2fplugin_2eproto, nullptr, // tracker diff --git a/src/google/protobuf/cpp_features.pb.cc b/src/google/protobuf/cpp_features.pb.cc index 0e82110847a4..d3c3dc8e3424 100644 --- a/src/google/protobuf/cpp_features.pb.cc +++ b/src/google/protobuf/cpp_features.pb.cc @@ -186,10 +186,10 @@ CppFeatures::GetClassData() const { &_table_.header, nullptr, // OnDemandRegisterArenaDtor nullptr, // IsInitialized + &CppFeatures::MergeImpl, PROTOBUF_FIELD_OFFSET(CppFeatures, _impl_._cached_size_), false, }, - &CppFeatures::MergeImpl, &CppFeatures::kDescriptorMethods, &descriptor_table_google_2fprotobuf_2fcpp_5ffeatures_2eproto, nullptr, // tracker diff --git a/src/google/protobuf/descriptor.pb.cc b/src/google/protobuf/descriptor.pb.cc index ef5e9900baf6..4825614ce7b1 100644 --- a/src/google/protobuf/descriptor.pb.cc +++ b/src/google/protobuf/descriptor.pb.cc @@ -2451,10 +2451,10 @@ FileDescriptorSet::GetClassData() const { &_table_.header, nullptr, // OnDemandRegisterArenaDtor FileDescriptorSet::IsInitializedImpl, + &FileDescriptorSet::MergeImpl, PROTOBUF_FIELD_OFFSET(FileDescriptorSet, _impl_._cached_size_), false, }, - &FileDescriptorSet::MergeImpl, &FileDescriptorSet::kDescriptorMethods, &descriptor_table_google_2fprotobuf_2fdescriptor_2eproto, nullptr, // tracker @@ -2687,10 +2687,10 @@ FileDescriptorProto::GetClassData() const { &_table_.header, nullptr, // OnDemandRegisterArenaDtor FileDescriptorProto::IsInitializedImpl, + &FileDescriptorProto::MergeImpl, PROTOBUF_FIELD_OFFSET(FileDescriptorProto, _impl_._cached_size_), false, }, - &FileDescriptorProto::MergeImpl, &FileDescriptorProto::kDescriptorMethods, &descriptor_table_google_2fprotobuf_2fdescriptor_2eproto, nullptr, // tracker @@ -3267,10 +3267,10 @@ DescriptorProto_ExtensionRange::GetClassData() const { &_table_.header, nullptr, // OnDemandRegisterArenaDtor DescriptorProto_ExtensionRange::IsInitializedImpl, + &DescriptorProto_ExtensionRange::MergeImpl, PROTOBUF_FIELD_OFFSET(DescriptorProto_ExtensionRange, _impl_._cached_size_), false, }, - &DescriptorProto_ExtensionRange::MergeImpl, &DescriptorProto_ExtensionRange::kDescriptorMethods, &descriptor_table_google_2fprotobuf_2fdescriptor_2eproto, nullptr, // tracker @@ -3531,10 +3531,10 @@ DescriptorProto_ReservedRange::GetClassData() const { &_table_.header, nullptr, // OnDemandRegisterArenaDtor nullptr, // IsInitialized + &DescriptorProto_ReservedRange::MergeImpl, PROTOBUF_FIELD_OFFSET(DescriptorProto_ReservedRange, _impl_._cached_size_), false, }, - &DescriptorProto_ReservedRange::MergeImpl, &DescriptorProto_ReservedRange::kDescriptorMethods, &descriptor_table_google_2fprotobuf_2fdescriptor_2eproto, nullptr, // tracker @@ -3786,10 +3786,10 @@ DescriptorProto::GetClassData() const { &_table_.header, nullptr, // OnDemandRegisterArenaDtor DescriptorProto::IsInitializedImpl, + &DescriptorProto::MergeImpl, PROTOBUF_FIELD_OFFSET(DescriptorProto, _impl_._cached_size_), false, }, - &DescriptorProto::MergeImpl, &DescriptorProto::kDescriptorMethods, &descriptor_table_google_2fprotobuf_2fdescriptor_2eproto, nullptr, // tracker @@ -4286,10 +4286,10 @@ ExtensionRangeOptions_Declaration::GetClassData() const { &_table_.header, nullptr, // OnDemandRegisterArenaDtor nullptr, // IsInitialized + &ExtensionRangeOptions_Declaration::MergeImpl, PROTOBUF_FIELD_OFFSET(ExtensionRangeOptions_Declaration, _impl_._cached_size_), false, }, - &ExtensionRangeOptions_Declaration::MergeImpl, &ExtensionRangeOptions_Declaration::kDescriptorMethods, &descriptor_table_google_2fprotobuf_2fdescriptor_2eproto, nullptr, // tracker @@ -4616,10 +4616,10 @@ ExtensionRangeOptions::GetClassData() const { &_table_.header, nullptr, // OnDemandRegisterArenaDtor ExtensionRangeOptions::IsInitializedImpl, + &ExtensionRangeOptions::MergeImpl, PROTOBUF_FIELD_OFFSET(ExtensionRangeOptions, _impl_._cached_size_), false, }, - &ExtensionRangeOptions::MergeImpl, &ExtensionRangeOptions::kDescriptorMethods, &descriptor_table_google_2fprotobuf_2fdescriptor_2eproto, nullptr, // tracker @@ -4971,10 +4971,10 @@ FieldDescriptorProto::GetClassData() const { &_table_.header, nullptr, // OnDemandRegisterArenaDtor FieldDescriptorProto::IsInitializedImpl, + &FieldDescriptorProto::MergeImpl, PROTOBUF_FIELD_OFFSET(FieldDescriptorProto, _impl_._cached_size_), false, }, - &FieldDescriptorProto::MergeImpl, &FieldDescriptorProto::kDescriptorMethods, &descriptor_table_google_2fprotobuf_2fdescriptor_2eproto, nullptr, // tracker @@ -5475,10 +5475,10 @@ OneofDescriptorProto::GetClassData() const { &_table_.header, nullptr, // OnDemandRegisterArenaDtor OneofDescriptorProto::IsInitializedImpl, + &OneofDescriptorProto::MergeImpl, PROTOBUF_FIELD_OFFSET(OneofDescriptorProto, _impl_._cached_size_), false, }, - &OneofDescriptorProto::MergeImpl, &OneofDescriptorProto::kDescriptorMethods, &descriptor_table_google_2fprotobuf_2fdescriptor_2eproto, nullptr, // tracker @@ -5718,10 +5718,10 @@ EnumDescriptorProto_EnumReservedRange::GetClassData() const { &_table_.header, nullptr, // OnDemandRegisterArenaDtor nullptr, // IsInitialized + &EnumDescriptorProto_EnumReservedRange::MergeImpl, PROTOBUF_FIELD_OFFSET(EnumDescriptorProto_EnumReservedRange, _impl_._cached_size_), false, }, - &EnumDescriptorProto_EnumReservedRange::MergeImpl, &EnumDescriptorProto_EnumReservedRange::kDescriptorMethods, &descriptor_table_google_2fprotobuf_2fdescriptor_2eproto, nullptr, // tracker @@ -5963,10 +5963,10 @@ EnumDescriptorProto::GetClassData() const { &_table_.header, nullptr, // OnDemandRegisterArenaDtor EnumDescriptorProto::IsInitializedImpl, + &EnumDescriptorProto::MergeImpl, PROTOBUF_FIELD_OFFSET(EnumDescriptorProto, _impl_._cached_size_), false, }, - &EnumDescriptorProto::MergeImpl, &EnumDescriptorProto::kDescriptorMethods, &descriptor_table_google_2fprotobuf_2fdescriptor_2eproto, nullptr, // tracker @@ -6311,10 +6311,10 @@ EnumValueDescriptorProto::GetClassData() const { &_table_.header, nullptr, // OnDemandRegisterArenaDtor EnumValueDescriptorProto::IsInitializedImpl, + &EnumValueDescriptorProto::MergeImpl, PROTOBUF_FIELD_OFFSET(EnumValueDescriptorProto, _impl_._cached_size_), false, }, - &EnumValueDescriptorProto::MergeImpl, &EnumValueDescriptorProto::kDescriptorMethods, &descriptor_table_google_2fprotobuf_2fdescriptor_2eproto, nullptr, // tracker @@ -6601,10 +6601,10 @@ ServiceDescriptorProto::GetClassData() const { &_table_.header, nullptr, // OnDemandRegisterArenaDtor ServiceDescriptorProto::IsInitializedImpl, + &ServiceDescriptorProto::MergeImpl, PROTOBUF_FIELD_OFFSET(ServiceDescriptorProto, _impl_._cached_size_), false, }, - &ServiceDescriptorProto::MergeImpl, &ServiceDescriptorProto::kDescriptorMethods, &descriptor_table_google_2fprotobuf_2fdescriptor_2eproto, nullptr, // tracker @@ -6908,10 +6908,10 @@ MethodDescriptorProto::GetClassData() const { &_table_.header, nullptr, // OnDemandRegisterArenaDtor MethodDescriptorProto::IsInitializedImpl, + &MethodDescriptorProto::MergeImpl, PROTOBUF_FIELD_OFFSET(MethodDescriptorProto, _impl_._cached_size_), false, }, - &MethodDescriptorProto::MergeImpl, &MethodDescriptorProto::kDescriptorMethods, &descriptor_table_google_2fprotobuf_2fdescriptor_2eproto, nullptr, // tracker @@ -7321,10 +7321,10 @@ FileOptions::GetClassData() const { &_table_.header, nullptr, // OnDemandRegisterArenaDtor FileOptions::IsInitializedImpl, + &FileOptions::MergeImpl, PROTOBUF_FIELD_OFFSET(FileOptions, _impl_._cached_size_), false, }, - &FileOptions::MergeImpl, &FileOptions::kDescriptorMethods, &descriptor_table_google_2fprotobuf_2fdescriptor_2eproto, nullptr, // tracker @@ -8111,10 +8111,10 @@ MessageOptions::GetClassData() const { &_table_.header, nullptr, // OnDemandRegisterArenaDtor MessageOptions::IsInitializedImpl, + &MessageOptions::MergeImpl, PROTOBUF_FIELD_OFFSET(MessageOptions, _impl_._cached_size_), false, }, - &MessageOptions::MergeImpl, &MessageOptions::kDescriptorMethods, &descriptor_table_google_2fprotobuf_2fdescriptor_2eproto, nullptr, // tracker @@ -8487,10 +8487,10 @@ FieldOptions_EditionDefault::GetClassData() const { &_table_.header, nullptr, // OnDemandRegisterArenaDtor nullptr, // IsInitialized + &FieldOptions_EditionDefault::MergeImpl, PROTOBUF_FIELD_OFFSET(FieldOptions_EditionDefault, _impl_._cached_size_), false, }, - &FieldOptions_EditionDefault::MergeImpl, &FieldOptions_EditionDefault::kDescriptorMethods, &descriptor_table_google_2fprotobuf_2fdescriptor_2eproto, nullptr, // tracker @@ -8734,10 +8734,10 @@ FieldOptions_FeatureSupport::GetClassData() const { &_table_.header, nullptr, // OnDemandRegisterArenaDtor nullptr, // IsInitialized + &FieldOptions_FeatureSupport::MergeImpl, PROTOBUF_FIELD_OFFSET(FieldOptions_FeatureSupport, _impl_._cached_size_), false, }, - &FieldOptions_FeatureSupport::MergeImpl, &FieldOptions_FeatureSupport::kDescriptorMethods, &descriptor_table_google_2fprotobuf_2fdescriptor_2eproto, nullptr, // tracker @@ -9051,10 +9051,10 @@ FieldOptions::GetClassData() const { &_table_.header, nullptr, // OnDemandRegisterArenaDtor FieldOptions::IsInitializedImpl, + &FieldOptions::MergeImpl, PROTOBUF_FIELD_OFFSET(FieldOptions, _impl_._cached_size_), false, }, - &FieldOptions::MergeImpl, &FieldOptions::kDescriptorMethods, &descriptor_table_google_2fprotobuf_2fdescriptor_2eproto, nullptr, // tracker @@ -9620,10 +9620,10 @@ OneofOptions::GetClassData() const { &_table_.header, nullptr, // OnDemandRegisterArenaDtor OneofOptions::IsInitializedImpl, + &OneofOptions::MergeImpl, PROTOBUF_FIELD_OFFSET(OneofOptions, _impl_._cached_size_), false, }, - &OneofOptions::MergeImpl, &OneofOptions::kDescriptorMethods, &descriptor_table_google_2fprotobuf_2fdescriptor_2eproto, nullptr, // tracker @@ -9900,10 +9900,10 @@ EnumOptions::GetClassData() const { &_table_.header, nullptr, // OnDemandRegisterArenaDtor EnumOptions::IsInitializedImpl, + &EnumOptions::MergeImpl, PROTOBUF_FIELD_OFFSET(EnumOptions, _impl_._cached_size_), false, }, - &EnumOptions::MergeImpl, &EnumOptions::kDescriptorMethods, &descriptor_table_google_2fprotobuf_2fdescriptor_2eproto, nullptr, // tracker @@ -10254,10 +10254,10 @@ EnumValueOptions::GetClassData() const { &_table_.header, nullptr, // OnDemandRegisterArenaDtor EnumValueOptions::IsInitializedImpl, + &EnumValueOptions::MergeImpl, PROTOBUF_FIELD_OFFSET(EnumValueOptions, _impl_._cached_size_), false, }, - &EnumValueOptions::MergeImpl, &EnumValueOptions::kDescriptorMethods, &descriptor_table_google_2fprotobuf_2fdescriptor_2eproto, nullptr, // tracker @@ -10584,10 +10584,10 @@ ServiceOptions::GetClassData() const { &_table_.header, nullptr, // OnDemandRegisterArenaDtor ServiceOptions::IsInitializedImpl, + &ServiceOptions::MergeImpl, PROTOBUF_FIELD_OFFSET(ServiceOptions, _impl_._cached_size_), false, }, - &ServiceOptions::MergeImpl, &ServiceOptions::kDescriptorMethods, &descriptor_table_google_2fprotobuf_2fdescriptor_2eproto, nullptr, // tracker @@ -10895,10 +10895,10 @@ MethodOptions::GetClassData() const { &_table_.header, nullptr, // OnDemandRegisterArenaDtor MethodOptions::IsInitializedImpl, + &MethodOptions::MergeImpl, PROTOBUF_FIELD_OFFSET(MethodOptions, _impl_._cached_size_), false, }, - &MethodOptions::MergeImpl, &MethodOptions::kDescriptorMethods, &descriptor_table_google_2fprotobuf_2fdescriptor_2eproto, nullptr, // tracker @@ -11221,10 +11221,10 @@ UninterpretedOption_NamePart::GetClassData() const { &_table_.header, nullptr, // OnDemandRegisterArenaDtor UninterpretedOption_NamePart::IsInitializedImpl, + &UninterpretedOption_NamePart::MergeImpl, PROTOBUF_FIELD_OFFSET(UninterpretedOption_NamePart, _impl_._cached_size_), false, }, - &UninterpretedOption_NamePart::MergeImpl, &UninterpretedOption_NamePart::kDescriptorMethods, &descriptor_table_google_2fprotobuf_2fdescriptor_2eproto, nullptr, // tracker @@ -11483,10 +11483,10 @@ UninterpretedOption::GetClassData() const { &_table_.header, nullptr, // OnDemandRegisterArenaDtor UninterpretedOption::IsInitializedImpl, + &UninterpretedOption::MergeImpl, PROTOBUF_FIELD_OFFSET(UninterpretedOption, _impl_._cached_size_), false, }, - &UninterpretedOption::MergeImpl, &UninterpretedOption::kDescriptorMethods, &descriptor_table_google_2fprotobuf_2fdescriptor_2eproto, nullptr, // tracker @@ -11871,10 +11871,10 @@ FeatureSet::GetClassData() const { &_table_.header, nullptr, // OnDemandRegisterArenaDtor FeatureSet::IsInitializedImpl, + &FeatureSet::MergeImpl, PROTOBUF_FIELD_OFFSET(FeatureSet, _impl_._cached_size_), false, }, - &FeatureSet::MergeImpl, &FeatureSet::kDescriptorMethods, &descriptor_table_google_2fprotobuf_2fdescriptor_2eproto, nullptr, // tracker @@ -12229,10 +12229,10 @@ FeatureSetDefaults_FeatureSetEditionDefault::GetClassData() const { &_table_.header, nullptr, // OnDemandRegisterArenaDtor FeatureSetDefaults_FeatureSetEditionDefault::IsInitializedImpl, + &FeatureSetDefaults_FeatureSetEditionDefault::MergeImpl, PROTOBUF_FIELD_OFFSET(FeatureSetDefaults_FeatureSetEditionDefault, _impl_._cached_size_), false, }, - &FeatureSetDefaults_FeatureSetEditionDefault::MergeImpl, &FeatureSetDefaults_FeatureSetEditionDefault::kDescriptorMethods, &descriptor_table_google_2fprotobuf_2fdescriptor_2eproto, nullptr, // tracker @@ -12527,10 +12527,10 @@ FeatureSetDefaults::GetClassData() const { &_table_.header, nullptr, // OnDemandRegisterArenaDtor FeatureSetDefaults::IsInitializedImpl, + &FeatureSetDefaults::MergeImpl, PROTOBUF_FIELD_OFFSET(FeatureSetDefaults, _impl_._cached_size_), false, }, - &FeatureSetDefaults::MergeImpl, &FeatureSetDefaults::kDescriptorMethods, &descriptor_table_google_2fprotobuf_2fdescriptor_2eproto, nullptr, // tracker @@ -12805,10 +12805,10 @@ SourceCodeInfo_Location::GetClassData() const { &_table_.header, nullptr, // OnDemandRegisterArenaDtor nullptr, // IsInitialized + &SourceCodeInfo_Location::MergeImpl, PROTOBUF_FIELD_OFFSET(SourceCodeInfo_Location, _impl_._cached_size_), false, }, - &SourceCodeInfo_Location::MergeImpl, &SourceCodeInfo_Location::kDescriptorMethods, &descriptor_table_google_2fprotobuf_2fdescriptor_2eproto, nullptr, // tracker @@ -13128,10 +13128,10 @@ SourceCodeInfo::GetClassData() const { &_table_.header, nullptr, // OnDemandRegisterArenaDtor nullptr, // IsInitialized + &SourceCodeInfo::MergeImpl, PROTOBUF_FIELD_OFFSET(SourceCodeInfo, _impl_._cached_size_), false, }, - &SourceCodeInfo::MergeImpl, &SourceCodeInfo::kDescriptorMethods, &descriptor_table_google_2fprotobuf_2fdescriptor_2eproto, nullptr, // tracker @@ -13338,10 +13338,10 @@ GeneratedCodeInfo_Annotation::GetClassData() const { &_table_.header, nullptr, // OnDemandRegisterArenaDtor nullptr, // IsInitialized + &GeneratedCodeInfo_Annotation::MergeImpl, PROTOBUF_FIELD_OFFSET(GeneratedCodeInfo_Annotation, _impl_._cached_size_), false, }, - &GeneratedCodeInfo_Annotation::MergeImpl, &GeneratedCodeInfo_Annotation::kDescriptorMethods, &descriptor_table_google_2fprotobuf_2fdescriptor_2eproto, nullptr, // tracker @@ -13653,10 +13653,10 @@ GeneratedCodeInfo::GetClassData() const { &_table_.header, nullptr, // OnDemandRegisterArenaDtor nullptr, // IsInitialized + &GeneratedCodeInfo::MergeImpl, PROTOBUF_FIELD_OFFSET(GeneratedCodeInfo, _impl_._cached_size_), false, }, - &GeneratedCodeInfo::MergeImpl, &GeneratedCodeInfo::kDescriptorMethods, &descriptor_table_google_2fprotobuf_2fdescriptor_2eproto, nullptr, // tracker diff --git a/src/google/protobuf/dynamic_message.cc b/src/google/protobuf/dynamic_message.cc index 792710781513..c3f607f2d634 100644 --- a/src/google/protobuf/dynamic_message.cc +++ b/src/google/protobuf/dynamic_message.cc @@ -273,10 +273,10 @@ struct DynamicMessageFactory::TypeInfo { nullptr, // tc_table nullptr, // on_demand_register_arena_dtor DynamicMessage::IsInitializedImpl, + &DynamicMessage::MergeImpl, PROTOBUF_FIELD_OFFSET(DynamicMessage, cached_byte_size_), false, }, - &DynamicMessage::MergeImpl, &DynamicMessage::kDescriptorMethods, nullptr, // descriptor_table nullptr, // get_metadata_tracker diff --git a/src/google/protobuf/implicit_weak_message.cc b/src/google/protobuf/implicit_weak_message.cc index e4182864a34b..dc4311f246ed 100644 --- a/src/google/protobuf/implicit_weak_message.cc +++ b/src/google/protobuf/implicit_weak_message.cc @@ -25,6 +25,15 @@ const char* ImplicitWeakMessage::ParseImpl(ImplicitWeakMessage* msg, return ctx->AppendString(ptr, msg->data_); } +void ImplicitWeakMessage::MergeImpl(MessageLite& self, + const MessageLite& other) { + const std::string* other_data = + static_cast(other).data_; + if (other_data != nullptr) { + static_cast(self).data_->append(*other_data); + } +} + struct ImplicitWeakMessageDefaultType { constexpr ImplicitWeakMessageDefaultType() : instance(ConstantInitialized{}) {} @@ -55,6 +64,7 @@ const MessageLite::ClassData* ImplicitWeakMessage::GetClassData() const { &table.header, nullptr, // on_demand_register_arena_dtor nullptr, // is_initialized (always true) + MergeImpl, PROTOBUF_FIELD_OFFSET(ImplicitWeakMessage, cached_size_), true, }, diff --git a/src/google/protobuf/implicit_weak_message.h b/src/google/protobuf/implicit_weak_message.h index 2585eb802481..88330fcf2422 100644 --- a/src/google/protobuf/implicit_weak_message.h +++ b/src/google/protobuf/implicit_weak_message.h @@ -64,14 +64,6 @@ class PROTOBUF_EXPORT ImplicitWeakMessage : public MessageLite { void Clear() override { data_->clear(); } - void CheckTypeAndMergeFrom(const MessageLite& other) override { - const std::string* other_data = - static_cast(other).data_; - if (other_data != nullptr) { - data_->append(*other_data); - } - } - size_t ByteSizeLong() const override { size_t size = data_ == nullptr ? 0 : data_->size(); cached_size_.Set(internal::ToCachedSize(size)); @@ -92,6 +84,7 @@ class PROTOBUF_EXPORT ImplicitWeakMessage : public MessageLite { private: static const char* ParseImpl(ImplicitWeakMessage* msg, const char* ptr, ParseContext* ctx); + static void MergeImpl(MessageLite&, const MessageLite&); // This std::string is allocated on the heap, but we use a raw pointer so that // the default instance can be constant-initialized. In the const methods, we diff --git a/src/google/protobuf/message.cc b/src/google/protobuf/message.cc index caf941ad5b4a..0b2ad590b19f 100644 --- a/src/google/protobuf/message.cc +++ b/src/google/protobuf/message.cc @@ -81,10 +81,6 @@ void Message::MergeFrom(const Message& from) { } } -void Message::CheckTypeAndMergeFrom(const MessageLite& other) { - MergeFrom(*DownCast(&other)); -} - void Message::CopyFrom(const Message& from) { if (&from == this) return; diff --git a/src/google/protobuf/message.h b/src/google/protobuf/message.h index 0354c9e9d7e7..a197cf706732 100644 --- a/src/google/protobuf/message.h +++ b/src/google/protobuf/message.h @@ -345,7 +345,6 @@ class PROTOBUF_EXPORT Message : public MessageLite { void Clear() override; - void CheckTypeAndMergeFrom(const MessageLite& other) override; size_t ByteSizeLong() const override; uint8_t* _InternalSerialize(uint8_t* target, io::EpsCopyOutputStream* stream) const override; diff --git a/src/google/protobuf/message_lite.cc b/src/google/protobuf/message_lite.cc index 103549e7d95f..25e2d7d928ef 100644 --- a/src/google/protobuf/message_lite.cc +++ b/src/google/protobuf/message_lite.cc @@ -45,6 +45,16 @@ namespace google { namespace protobuf { +void MessageLite::CheckTypeAndMergeFrom(const MessageLite& other) { + auto* data = GetClassData(); + auto* other_data = other.GetClassData(); + + ABSL_CHECK_EQ(data, other_data) + << "Invalid call to CheckTypeAndMergeFrom between types " << GetTypeName() + << " and " << other.GetTypeName(); + data->merge_to_from(*this, other); +} + bool MessageLite::IsInitialized() const { auto* data = GetClassData(); return data->is_initialized != nullptr ? data->is_initialized(*this) : true; diff --git a/src/google/protobuf/message_lite.h b/src/google/protobuf/message_lite.h index 1b85aa0c6517..e6e2db30be56 100644 --- a/src/google/protobuf/message_lite.h +++ b/src/google/protobuf/message_lite.h @@ -249,7 +249,7 @@ class PROTOBUF_EXPORT MessageLite { // If |other| is the exact same class as this, calls MergeFrom(). Otherwise, // results are undefined (probably crash). - virtual void CheckTypeAndMergeFrom(const MessageLite& other) = 0; + void CheckTypeAndMergeFrom(const MessageLite& other); // These methods return a human-readable summary of the message. Note that // since the MessageLite interface does not support reflection, there is very @@ -552,6 +552,7 @@ class PROTOBUF_EXPORT MessageLite { const internal::TcParseTableBase* tc_table; void (*on_demand_register_arena_dtor)(MessageLite& msg, Arena& arena); bool (*is_initialized)(const MessageLite&); + void (*merge_to_from)(MessageLite& to, const MessageLite& from_msg); // Offset of the CachedSize member. uint32_t cached_size_offset; @@ -559,25 +560,17 @@ class PROTOBUF_EXPORT MessageLite { // char[] just beyond the ClassData. bool is_lite; - // XXX REMOVE XXX - constexpr ClassData(const internal::TcParseTableBase* tc_table, - void (*on_demand_register_arena_dtor)(MessageLite&, - Arena&), - uint32_t cached_size_offset, bool is_lite) - : tc_table(tc_table), - on_demand_register_arena_dtor(on_demand_register_arena_dtor), - is_initialized(nullptr), - cached_size_offset(cached_size_offset), - is_lite(is_lite) {} - constexpr ClassData(const internal::TcParseTableBase* tc_table, void (*on_demand_register_arena_dtor)(MessageLite&, Arena&), bool (*is_initialized)(const MessageLite&), + void (*merge_to_from)(MessageLite& to, + const MessageLite& from_msg), uint32_t cached_size_offset, bool is_lite) : tc_table(tc_table), on_demand_register_arena_dtor(on_demand_register_arena_dtor), is_initialized(is_initialized), + merge_to_from(merge_to_from), cached_size_offset(cached_size_offset), is_lite(is_lite) {} @@ -595,13 +588,10 @@ class PROTOBUF_EXPORT MessageLite { }; struct ClassDataFull : ClassData { constexpr ClassDataFull(ClassData base, - void (*merge_to_from)(MessageLite& to, - const MessageLite& from_msg), const DescriptorMethods* descriptor_methods, const internal::DescriptorTable* descriptor_table, void (*get_metadata_tracker)()) : ClassData(base), - merge_to_from(merge_to_from), descriptor_methods(descriptor_methods), descriptor_table(descriptor_table), reflection(), @@ -610,7 +600,6 @@ class PROTOBUF_EXPORT MessageLite { constexpr const ClassData* base() const { return this; } - void (*merge_to_from)(MessageLite& to, const MessageLite& from_msg); const DescriptorMethods* descriptor_methods; // Codegen types will provide a DescriptorTable to do lazy