From 9a50a839700f675a12e3255c0d08dabdb148c5e6 Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Wed, 13 Dec 2023 10:11:53 -0800 Subject: [PATCH] Move the descriptor message part of ClassData into a subclass and keep a bool on the base. This reduces the cost for Lite messages, while keeping the type the same size of descriptor messages. PiperOrigin-RevId: 590637288 --- src/google/protobuf/compiler/cpp/message.cc | 79 ++-- .../compiler/java/java_features.pb.cc | 13 +- src/google/protobuf/compiler/plugin.pb.cc | 52 ++- src/google/protobuf/cpp_features.pb.cc | 13 +- src/google/protobuf/descriptor.pb.cc | 416 +++++++++++------- src/google/protobuf/dynamic_message.cc | 9 +- .../protobuf/generated_message_bases.cc | 9 +- src/google/protobuf/implicit_weak_message.h | 3 +- src/google/protobuf/map_entry.h | 9 +- src/google/protobuf/message.cc | 4 +- src/google/protobuf/message_lite.cc | 8 +- src/google/protobuf/message_lite.h | 49 ++- 12 files changed, 389 insertions(+), 275 deletions(-) diff --git a/src/google/protobuf/compiler/cpp/message.cc b/src/google/protobuf/compiler/cpp/message.cc index 765935ae8e..2372b36364 100644 --- a/src/google/protobuf/compiler/cpp/message.cc +++ b/src/google/protobuf/compiler/cpp/message.cc @@ -3497,63 +3497,35 @@ void MessageGenerator::GenerateClassData(io::Printer* p) { Formatter format(p); if (HasSimpleBaseClass(descriptor_, options_)) return; - const auto class_data_members = [&] { - p->Emit( - { - {"merge_impl", - [&] { - // TODO: This check is not needed once we migrate - // CheckTypeAndMergeFrom to ClassData fully. - if (HasDescriptorMethods(descriptor_->file(), options_)) { - p->Emit(R"cc( - $classname$::MergeImpl, - )cc"); - } else { - p->Emit(R"cc( - nullptr, // MergeImpl - )cc"); - } - }}, - {"on_demand_register_arena_dtor", - [&] { - if (NeedsArenaDestructor() == ArenaDtorNeeds::kOnDemand) { - p->Emit(R"cc( - $classname$::OnDemandRegisterArenaDtor, - )cc"); - } else { - p->Emit(R"cc( - nullptr, // OnDemandRegisterArenaDtor - )cc"); - } - }}, - {"descriptor_methods", - [&] { - if (HasDescriptorMethods(descriptor_->file(), options_)) { - p->Emit(R"cc( - &::$proto_ns$::Message::kDescriptorMethods, - )cc"); - } else { - p->Emit(R"cc( - nullptr, // DescriptorMethods - )cc"); - } - }}, - }, - R"cc( - $merge_impl$, $on_demand_register_arena_dtor$, $descriptor_methods$, - PROTOBUF_FIELD_OFFSET($classname$, $cached_size$), - )cc"); + const auto on_demand_register_arena_dtor = [&] { + if (NeedsArenaDestructor() == ArenaDtorNeeds::kOnDemand) { + p->Emit(R"cc( + $classname$::OnDemandRegisterArenaDtor, + )cc"); + } else { + p->Emit(R"cc( + nullptr, // OnDemandRegisterArenaDtor + )cc"); + } }; if (HasDescriptorMethods(descriptor_->file(), options_)) { p->Emit( - {{"class_data_members", class_data_members}}, + { + {"on_demand_register_arena_dtor", on_demand_register_arena_dtor}, + }, R"cc( const ::$proto_ns$::MessageLite::ClassData* $classname$::GetClassData() const { - PROTOBUF_CONSTINIT static const ::$proto_ns$::MessageLite::ClassData - _data_ = { - $class_data_members$, + PROTOBUF_CONSTINIT static const ::$proto_ns$::MessageLite:: + ClassDataFull _data_ = { + { + $on_demand_register_arena_dtor$, + PROTOBUF_FIELD_OFFSET($classname$, $cached_size$), + false, + }, + &$classname$::MergeImpl, + &$classname$::kDescriptorMethods, }; return &_data_; } @@ -3561,8 +3533,8 @@ void MessageGenerator::GenerateClassData(io::Printer* p) { } else { p->Emit( { - {"class_data_members", class_data_members}, {"type_size", descriptor_->full_name().size() + 1}, + {"on_demand_register_arena_dtor", on_demand_register_arena_dtor}, }, R"cc( const ::$proto_ns$::MessageLite::ClassData* @@ -3571,9 +3543,12 @@ void MessageGenerator::GenerateClassData(io::Printer* p) { ::$proto_ns$::MessageLite::ClassData header; char type_name[$type_size$]; }; + PROTOBUF_CONSTINIT static const ClassData_ _data_ = { { - $class_data_members$, + $on_demand_register_arena_dtor$, + PROTOBUF_FIELD_OFFSET($classname$, $cached_size$), + true, }, "$full_name$", }; diff --git a/src/google/protobuf/compiler/java/java_features.pb.cc b/src/google/protobuf/compiler/java/java_features.pb.cc index 2a632e7eb4..aa2052a891 100644 --- a/src/google/protobuf/compiler/java/java_features.pb.cc +++ b/src/google/protobuf/compiler/java/java_features.pb.cc @@ -190,12 +190,15 @@ inline void JavaFeatures::SharedDtor() { const ::google::protobuf::MessageLite::ClassData* JavaFeatures::GetClassData() const { - PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite::ClassData - _data_ = { - JavaFeatures::MergeImpl, - nullptr, // OnDemandRegisterArenaDtor - &::google::protobuf::Message::kDescriptorMethods, + PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite:: + ClassDataFull _data_ = { + { + nullptr, // OnDemandRegisterArenaDtor PROTOBUF_FIELD_OFFSET(JavaFeatures, _impl_._cached_size_), + false, + }, + &JavaFeatures::MergeImpl, + &JavaFeatures::kDescriptorMethods, }; return &_data_; } diff --git a/src/google/protobuf/compiler/plugin.pb.cc b/src/google/protobuf/compiler/plugin.pb.cc index 3c1ac59a0d..788f52fbbe 100644 --- a/src/google/protobuf/compiler/plugin.pb.cc +++ b/src/google/protobuf/compiler/plugin.pb.cc @@ -376,12 +376,15 @@ inline void Version::SharedDtor() { const ::google::protobuf::MessageLite::ClassData* Version::GetClassData() const { - PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite::ClassData - _data_ = { - Version::MergeImpl, - nullptr, // OnDemandRegisterArenaDtor - &::google::protobuf::Message::kDescriptorMethods, + PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite:: + ClassDataFull _data_ = { + { + nullptr, // OnDemandRegisterArenaDtor PROTOBUF_FIELD_OFFSET(Version, _impl_._cached_size_), + false, + }, + &Version::MergeImpl, + &Version::kDescriptorMethods, }; return &_data_; } @@ -684,12 +687,15 @@ inline void CodeGeneratorRequest::SharedDtor() { const ::google::protobuf::MessageLite::ClassData* CodeGeneratorRequest::GetClassData() const { - PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite::ClassData - _data_ = { - CodeGeneratorRequest::MergeImpl, - nullptr, // OnDemandRegisterArenaDtor - &::google::protobuf::Message::kDescriptorMethods, + PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite:: + ClassDataFull _data_ = { + { + nullptr, // OnDemandRegisterArenaDtor PROTOBUF_FIELD_OFFSET(CodeGeneratorRequest, _impl_._cached_size_), + false, + }, + &CodeGeneratorRequest::MergeImpl, + &CodeGeneratorRequest::kDescriptorMethods, }; return &_data_; } @@ -1026,12 +1032,15 @@ inline void CodeGeneratorResponse_File::SharedDtor() { const ::google::protobuf::MessageLite::ClassData* CodeGeneratorResponse_File::GetClassData() const { - PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite::ClassData - _data_ = { - CodeGeneratorResponse_File::MergeImpl, - nullptr, // OnDemandRegisterArenaDtor - &::google::protobuf::Message::kDescriptorMethods, + PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite:: + ClassDataFull _data_ = { + { + nullptr, // OnDemandRegisterArenaDtor PROTOBUF_FIELD_OFFSET(CodeGeneratorResponse_File, _impl_._cached_size_), + false, + }, + &CodeGeneratorResponse_File::MergeImpl, + &CodeGeneratorResponse_File::kDescriptorMethods, }; return &_data_; } @@ -1343,12 +1352,15 @@ inline void CodeGeneratorResponse::SharedDtor() { const ::google::protobuf::MessageLite::ClassData* CodeGeneratorResponse::GetClassData() const { - PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite::ClassData - _data_ = { - CodeGeneratorResponse::MergeImpl, - nullptr, // OnDemandRegisterArenaDtor - &::google::protobuf::Message::kDescriptorMethods, + PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite:: + ClassDataFull _data_ = { + { + nullptr, // OnDemandRegisterArenaDtor PROTOBUF_FIELD_OFFSET(CodeGeneratorResponse, _impl_._cached_size_), + false, + }, + &CodeGeneratorResponse::MergeImpl, + &CodeGeneratorResponse::kDescriptorMethods, }; return &_data_; } diff --git a/src/google/protobuf/cpp_features.pb.cc b/src/google/protobuf/cpp_features.pb.cc index 0d38ae0ace..9ed6b80a99 100644 --- a/src/google/protobuf/cpp_features.pb.cc +++ b/src/google/protobuf/cpp_features.pb.cc @@ -157,12 +157,15 @@ inline void CppFeatures::SharedDtor() { const ::google::protobuf::MessageLite::ClassData* CppFeatures::GetClassData() const { - PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite::ClassData - _data_ = { - CppFeatures::MergeImpl, - nullptr, // OnDemandRegisterArenaDtor - &::google::protobuf::Message::kDescriptorMethods, + PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite:: + ClassDataFull _data_ = { + { + nullptr, // OnDemandRegisterArenaDtor PROTOBUF_FIELD_OFFSET(CppFeatures, _impl_._cached_size_), + false, + }, + &CppFeatures::MergeImpl, + &CppFeatures::kDescriptorMethods, }; return &_data_; } diff --git a/src/google/protobuf/descriptor.pb.cc b/src/google/protobuf/descriptor.pb.cc index 269a8dc448..951acb9338 100644 --- a/src/google/protobuf/descriptor.pb.cc +++ b/src/google/protobuf/descriptor.pb.cc @@ -2400,12 +2400,15 @@ inline void FileDescriptorSet::SharedDtor() { const ::google::protobuf::MessageLite::ClassData* FileDescriptorSet::GetClassData() const { - PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite::ClassData - _data_ = { - FileDescriptorSet::MergeImpl, - nullptr, // OnDemandRegisterArenaDtor - &::google::protobuf::Message::kDescriptorMethods, + PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite:: + ClassDataFull _data_ = { + { + nullptr, // OnDemandRegisterArenaDtor PROTOBUF_FIELD_OFFSET(FileDescriptorSet, _impl_._cached_size_), + false, + }, + &FileDescriptorSet::MergeImpl, + &FileDescriptorSet::kDescriptorMethods, }; return &_data_; } @@ -2629,12 +2632,15 @@ inline void FileDescriptorProto::SharedDtor() { const ::google::protobuf::MessageLite::ClassData* FileDescriptorProto::GetClassData() const { - PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite::ClassData - _data_ = { - FileDescriptorProto::MergeImpl, - nullptr, // OnDemandRegisterArenaDtor - &::google::protobuf::Message::kDescriptorMethods, + PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite:: + ClassDataFull _data_ = { + { + nullptr, // OnDemandRegisterArenaDtor PROTOBUF_FIELD_OFFSET(FileDescriptorProto, _impl_._cached_size_), + false, + }, + &FileDescriptorProto::MergeImpl, + &FileDescriptorProto::kDescriptorMethods, }; return &_data_; } @@ -3196,12 +3202,15 @@ inline void DescriptorProto_ExtensionRange::SharedDtor() { const ::google::protobuf::MessageLite::ClassData* DescriptorProto_ExtensionRange::GetClassData() const { - PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite::ClassData - _data_ = { - DescriptorProto_ExtensionRange::MergeImpl, - nullptr, // OnDemandRegisterArenaDtor - &::google::protobuf::Message::kDescriptorMethods, + PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite:: + ClassDataFull _data_ = { + { + nullptr, // OnDemandRegisterArenaDtor PROTOBUF_FIELD_OFFSET(DescriptorProto_ExtensionRange, _impl_._cached_size_), + false, + }, + &DescriptorProto_ExtensionRange::MergeImpl, + &DescriptorProto_ExtensionRange::kDescriptorMethods, }; return &_data_; } @@ -3455,12 +3464,15 @@ inline void DescriptorProto_ReservedRange::SharedDtor() { const ::google::protobuf::MessageLite::ClassData* DescriptorProto_ReservedRange::GetClassData() const { - PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite::ClassData - _data_ = { - DescriptorProto_ReservedRange::MergeImpl, - nullptr, // OnDemandRegisterArenaDtor - &::google::protobuf::Message::kDescriptorMethods, + PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite:: + ClassDataFull _data_ = { + { + nullptr, // OnDemandRegisterArenaDtor PROTOBUF_FIELD_OFFSET(DescriptorProto_ReservedRange, _impl_._cached_size_), + false, + }, + &DescriptorProto_ReservedRange::MergeImpl, + &DescriptorProto_ReservedRange::kDescriptorMethods, }; return &_data_; } @@ -3710,12 +3722,15 @@ inline void DescriptorProto::SharedDtor() { const ::google::protobuf::MessageLite::ClassData* DescriptorProto::GetClassData() const { - PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite::ClassData - _data_ = { - DescriptorProto::MergeImpl, - nullptr, // OnDemandRegisterArenaDtor - &::google::protobuf::Message::kDescriptorMethods, + PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite:: + ClassDataFull _data_ = { + { + nullptr, // OnDemandRegisterArenaDtor PROTOBUF_FIELD_OFFSET(DescriptorProto, _impl_._cached_size_), + false, + }, + &DescriptorProto::MergeImpl, + &DescriptorProto::kDescriptorMethods, }; return &_data_; } @@ -4191,12 +4206,15 @@ inline void ExtensionRangeOptions_Declaration::SharedDtor() { const ::google::protobuf::MessageLite::ClassData* ExtensionRangeOptions_Declaration::GetClassData() const { - PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite::ClassData - _data_ = { - ExtensionRangeOptions_Declaration::MergeImpl, - nullptr, // OnDemandRegisterArenaDtor - &::google::protobuf::Message::kDescriptorMethods, + PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite:: + ClassDataFull _data_ = { + { + nullptr, // OnDemandRegisterArenaDtor PROTOBUF_FIELD_OFFSET(ExtensionRangeOptions_Declaration, _impl_._cached_size_), + false, + }, + &ExtensionRangeOptions_Declaration::MergeImpl, + &ExtensionRangeOptions_Declaration::kDescriptorMethods, }; return &_data_; } @@ -4521,12 +4539,15 @@ inline void ExtensionRangeOptions::SharedDtor() { const ::google::protobuf::MessageLite::ClassData* ExtensionRangeOptions::GetClassData() const { - PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite::ClassData - _data_ = { - ExtensionRangeOptions::MergeImpl, - nullptr, // OnDemandRegisterArenaDtor - &::google::protobuf::Message::kDescriptorMethods, + PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite:: + ClassDataFull _data_ = { + { + nullptr, // OnDemandRegisterArenaDtor PROTOBUF_FIELD_OFFSET(ExtensionRangeOptions, _impl_._cached_size_), + false, + }, + &ExtensionRangeOptions::MergeImpl, + &ExtensionRangeOptions::kDescriptorMethods, }; return &_data_; } @@ -4866,12 +4887,15 @@ inline void FieldDescriptorProto::SharedDtor() { const ::google::protobuf::MessageLite::ClassData* FieldDescriptorProto::GetClassData() const { - PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite::ClassData - _data_ = { - FieldDescriptorProto::MergeImpl, - nullptr, // OnDemandRegisterArenaDtor - &::google::protobuf::Message::kDescriptorMethods, + PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite:: + ClassDataFull _data_ = { + { + nullptr, // OnDemandRegisterArenaDtor PROTOBUF_FIELD_OFFSET(FieldDescriptorProto, _impl_._cached_size_), + false, + }, + &FieldDescriptorProto::MergeImpl, + &FieldDescriptorProto::kDescriptorMethods, }; return &_data_; } @@ -5365,12 +5389,15 @@ inline void OneofDescriptorProto::SharedDtor() { const ::google::protobuf::MessageLite::ClassData* OneofDescriptorProto::GetClassData() const { - PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite::ClassData - _data_ = { - OneofDescriptorProto::MergeImpl, - nullptr, // OnDemandRegisterArenaDtor - &::google::protobuf::Message::kDescriptorMethods, + PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite:: + ClassDataFull _data_ = { + { + nullptr, // OnDemandRegisterArenaDtor PROTOBUF_FIELD_OFFSET(OneofDescriptorProto, _impl_._cached_size_), + false, + }, + &OneofDescriptorProto::MergeImpl, + &OneofDescriptorProto::kDescriptorMethods, }; return &_data_; } @@ -5603,12 +5630,15 @@ inline void EnumDescriptorProto_EnumReservedRange::SharedDtor() { const ::google::protobuf::MessageLite::ClassData* EnumDescriptorProto_EnumReservedRange::GetClassData() const { - PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite::ClassData - _data_ = { - EnumDescriptorProto_EnumReservedRange::MergeImpl, - nullptr, // OnDemandRegisterArenaDtor - &::google::protobuf::Message::kDescriptorMethods, + PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite:: + ClassDataFull _data_ = { + { + nullptr, // OnDemandRegisterArenaDtor PROTOBUF_FIELD_OFFSET(EnumDescriptorProto_EnumReservedRange, _impl_._cached_size_), + false, + }, + &EnumDescriptorProto_EnumReservedRange::MergeImpl, + &EnumDescriptorProto_EnumReservedRange::kDescriptorMethods, }; return &_data_; } @@ -5848,12 +5878,15 @@ inline void EnumDescriptorProto::SharedDtor() { const ::google::protobuf::MessageLite::ClassData* EnumDescriptorProto::GetClassData() const { - PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite::ClassData - _data_ = { - EnumDescriptorProto::MergeImpl, - nullptr, // OnDemandRegisterArenaDtor - &::google::protobuf::Message::kDescriptorMethods, + PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite:: + ClassDataFull _data_ = { + { + nullptr, // OnDemandRegisterArenaDtor PROTOBUF_FIELD_OFFSET(EnumDescriptorProto, _impl_._cached_size_), + false, + }, + &EnumDescriptorProto::MergeImpl, + &EnumDescriptorProto::kDescriptorMethods, }; return &_data_; } @@ -6187,12 +6220,15 @@ inline void EnumValueDescriptorProto::SharedDtor() { const ::google::protobuf::MessageLite::ClassData* EnumValueDescriptorProto::GetClassData() const { - PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite::ClassData - _data_ = { - EnumValueDescriptorProto::MergeImpl, - nullptr, // OnDemandRegisterArenaDtor - &::google::protobuf::Message::kDescriptorMethods, + PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite:: + ClassDataFull _data_ = { + { + nullptr, // OnDemandRegisterArenaDtor PROTOBUF_FIELD_OFFSET(EnumValueDescriptorProto, _impl_._cached_size_), + false, + }, + &EnumValueDescriptorProto::MergeImpl, + &EnumValueDescriptorProto::kDescriptorMethods, }; return &_data_; } @@ -6472,12 +6508,15 @@ inline void ServiceDescriptorProto::SharedDtor() { const ::google::protobuf::MessageLite::ClassData* ServiceDescriptorProto::GetClassData() const { - PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite::ClassData - _data_ = { - ServiceDescriptorProto::MergeImpl, - nullptr, // OnDemandRegisterArenaDtor - &::google::protobuf::Message::kDescriptorMethods, + PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite:: + ClassDataFull _data_ = { + { + nullptr, // OnDemandRegisterArenaDtor PROTOBUF_FIELD_OFFSET(ServiceDescriptorProto, _impl_._cached_size_), + false, + }, + &ServiceDescriptorProto::MergeImpl, + &ServiceDescriptorProto::kDescriptorMethods, }; return &_data_; } @@ -6772,12 +6811,15 @@ inline void MethodDescriptorProto::SharedDtor() { const ::google::protobuf::MessageLite::ClassData* MethodDescriptorProto::GetClassData() const { - PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite::ClassData - _data_ = { - MethodDescriptorProto::MergeImpl, - nullptr, // OnDemandRegisterArenaDtor - &::google::protobuf::Message::kDescriptorMethods, + PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite:: + ClassDataFull _data_ = { + { + nullptr, // OnDemandRegisterArenaDtor PROTOBUF_FIELD_OFFSET(MethodDescriptorProto, _impl_._cached_size_), + false, + }, + &MethodDescriptorProto::MergeImpl, + &MethodDescriptorProto::kDescriptorMethods, }; return &_data_; } @@ -7180,12 +7222,15 @@ inline void FileOptions::SharedDtor() { const ::google::protobuf::MessageLite::ClassData* FileOptions::GetClassData() const { - PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite::ClassData - _data_ = { - FileOptions::MergeImpl, - nullptr, // OnDemandRegisterArenaDtor - &::google::protobuf::Message::kDescriptorMethods, + PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite:: + ClassDataFull _data_ = { + { + nullptr, // OnDemandRegisterArenaDtor PROTOBUF_FIELD_OFFSET(FileOptions, _impl_._cached_size_), + false, + }, + &FileOptions::MergeImpl, + &FileOptions::kDescriptorMethods, }; return &_data_; } @@ -7982,12 +8027,15 @@ inline void MessageOptions::SharedDtor() { const ::google::protobuf::MessageLite::ClassData* MessageOptions::GetClassData() const { - PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite::ClassData - _data_ = { - MessageOptions::MergeImpl, - nullptr, // OnDemandRegisterArenaDtor - &::google::protobuf::Message::kDescriptorMethods, + PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite:: + ClassDataFull _data_ = { + { + nullptr, // OnDemandRegisterArenaDtor PROTOBUF_FIELD_OFFSET(MessageOptions, _impl_._cached_size_), + false, + }, + &MessageOptions::MergeImpl, + &MessageOptions::kDescriptorMethods, }; return &_data_; } @@ -8350,12 +8398,15 @@ inline void FieldOptions_EditionDefault::SharedDtor() { const ::google::protobuf::MessageLite::ClassData* FieldOptions_EditionDefault::GetClassData() const { - PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite::ClassData - _data_ = { - FieldOptions_EditionDefault::MergeImpl, - nullptr, // OnDemandRegisterArenaDtor - &::google::protobuf::Message::kDescriptorMethods, + PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite:: + ClassDataFull _data_ = { + { + nullptr, // OnDemandRegisterArenaDtor PROTOBUF_FIELD_OFFSET(FieldOptions_EditionDefault, _impl_._cached_size_), + false, + }, + &FieldOptions_EditionDefault::MergeImpl, + &FieldOptions_EditionDefault::kDescriptorMethods, }; return &_data_; } @@ -8608,12 +8659,15 @@ inline void FieldOptions::SharedDtor() { const ::google::protobuf::MessageLite::ClassData* FieldOptions::GetClassData() const { - PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite::ClassData - _data_ = { - FieldOptions::MergeImpl, - nullptr, // OnDemandRegisterArenaDtor - &::google::protobuf::Message::kDescriptorMethods, + PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite:: + ClassDataFull _data_ = { + { + nullptr, // OnDemandRegisterArenaDtor PROTOBUF_FIELD_OFFSET(FieldOptions, _impl_._cached_size_), + false, + }, + &FieldOptions::MergeImpl, + &FieldOptions::kDescriptorMethods, }; return &_data_; } @@ -9136,12 +9190,15 @@ inline void OneofOptions::SharedDtor() { const ::google::protobuf::MessageLite::ClassData* OneofOptions::GetClassData() const { - PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite::ClassData - _data_ = { - OneofOptions::MergeImpl, - nullptr, // OnDemandRegisterArenaDtor - &::google::protobuf::Message::kDescriptorMethods, + PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite:: + ClassDataFull _data_ = { + { + nullptr, // OnDemandRegisterArenaDtor PROTOBUF_FIELD_OFFSET(OneofOptions, _impl_._cached_size_), + false, + }, + &OneofOptions::MergeImpl, + &OneofOptions::kDescriptorMethods, }; return &_data_; } @@ -9408,12 +9465,15 @@ inline void EnumOptions::SharedDtor() { const ::google::protobuf::MessageLite::ClassData* EnumOptions::GetClassData() const { - PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite::ClassData - _data_ = { - EnumOptions::MergeImpl, - nullptr, // OnDemandRegisterArenaDtor - &::google::protobuf::Message::kDescriptorMethods, + PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite:: + ClassDataFull _data_ = { + { + nullptr, // OnDemandRegisterArenaDtor PROTOBUF_FIELD_OFFSET(EnumOptions, _impl_._cached_size_), + false, + }, + &EnumOptions::MergeImpl, + &EnumOptions::kDescriptorMethods, }; return &_data_; } @@ -9754,12 +9814,15 @@ inline void EnumValueOptions::SharedDtor() { const ::google::protobuf::MessageLite::ClassData* EnumValueOptions::GetClassData() const { - PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite::ClassData - _data_ = { - EnumValueOptions::MergeImpl, - nullptr, // OnDemandRegisterArenaDtor - &::google::protobuf::Message::kDescriptorMethods, + PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite:: + ClassDataFull _data_ = { + { + nullptr, // OnDemandRegisterArenaDtor PROTOBUF_FIELD_OFFSET(EnumValueOptions, _impl_._cached_size_), + false, + }, + &EnumValueOptions::MergeImpl, + &EnumValueOptions::kDescriptorMethods, }; return &_data_; } @@ -10076,12 +10139,15 @@ inline void ServiceOptions::SharedDtor() { const ::google::protobuf::MessageLite::ClassData* ServiceOptions::GetClassData() const { - PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite::ClassData - _data_ = { - ServiceOptions::MergeImpl, - nullptr, // OnDemandRegisterArenaDtor - &::google::protobuf::Message::kDescriptorMethods, + PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite:: + ClassDataFull _data_ = { + { + nullptr, // OnDemandRegisterArenaDtor PROTOBUF_FIELD_OFFSET(ServiceOptions, _impl_._cached_size_), + false, + }, + &ServiceOptions::MergeImpl, + &ServiceOptions::kDescriptorMethods, }; return &_data_; } @@ -10379,12 +10445,15 @@ inline void MethodOptions::SharedDtor() { const ::google::protobuf::MessageLite::ClassData* MethodOptions::GetClassData() const { - PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite::ClassData - _data_ = { - MethodOptions::MergeImpl, - nullptr, // OnDemandRegisterArenaDtor - &::google::protobuf::Message::kDescriptorMethods, + PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite:: + ClassDataFull _data_ = { + { + nullptr, // OnDemandRegisterArenaDtor PROTOBUF_FIELD_OFFSET(MethodOptions, _impl_._cached_size_), + false, + }, + &MethodOptions::MergeImpl, + &MethodOptions::kDescriptorMethods, }; return &_data_; } @@ -10697,12 +10766,15 @@ inline void UninterpretedOption_NamePart::SharedDtor() { const ::google::protobuf::MessageLite::ClassData* UninterpretedOption_NamePart::GetClassData() const { - PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite::ClassData - _data_ = { - UninterpretedOption_NamePart::MergeImpl, - nullptr, // OnDemandRegisterArenaDtor - &::google::protobuf::Message::kDescriptorMethods, + PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite:: + ClassDataFull _data_ = { + { + nullptr, // OnDemandRegisterArenaDtor PROTOBUF_FIELD_OFFSET(UninterpretedOption_NamePart, _impl_._cached_size_), + false, + }, + &UninterpretedOption_NamePart::MergeImpl, + &UninterpretedOption_NamePart::kDescriptorMethods, }; return &_data_; } @@ -10954,12 +11026,15 @@ inline void UninterpretedOption::SharedDtor() { const ::google::protobuf::MessageLite::ClassData* UninterpretedOption::GetClassData() const { - PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite::ClassData - _data_ = { - UninterpretedOption::MergeImpl, - nullptr, // OnDemandRegisterArenaDtor - &::google::protobuf::Message::kDescriptorMethods, + PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite:: + ClassDataFull _data_ = { + { + nullptr, // OnDemandRegisterArenaDtor PROTOBUF_FIELD_OFFSET(UninterpretedOption, _impl_._cached_size_), + false, + }, + &UninterpretedOption::MergeImpl, + &UninterpretedOption::kDescriptorMethods, }; return &_data_; } @@ -11335,12 +11410,15 @@ inline void FeatureSet::SharedDtor() { const ::google::protobuf::MessageLite::ClassData* FeatureSet::GetClassData() const { - PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite::ClassData - _data_ = { - FeatureSet::MergeImpl, - nullptr, // OnDemandRegisterArenaDtor - &::google::protobuf::Message::kDescriptorMethods, + PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite:: + ClassDataFull _data_ = { + { + nullptr, // OnDemandRegisterArenaDtor PROTOBUF_FIELD_OFFSET(FeatureSet, _impl_._cached_size_), + false, + }, + &FeatureSet::MergeImpl, + &FeatureSet::kDescriptorMethods, }; return &_data_; } @@ -11683,12 +11761,15 @@ inline void FeatureSetDefaults_FeatureSetEditionDefault::SharedDtor() { const ::google::protobuf::MessageLite::ClassData* FeatureSetDefaults_FeatureSetEditionDefault::GetClassData() const { - PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite::ClassData - _data_ = { - FeatureSetDefaults_FeatureSetEditionDefault::MergeImpl, - nullptr, // OnDemandRegisterArenaDtor - &::google::protobuf::Message::kDescriptorMethods, + PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite:: + ClassDataFull _data_ = { + { + nullptr, // OnDemandRegisterArenaDtor PROTOBUF_FIELD_OFFSET(FeatureSetDefaults_FeatureSetEditionDefault, _impl_._cached_size_), + false, + }, + &FeatureSetDefaults_FeatureSetEditionDefault::MergeImpl, + &FeatureSetDefaults_FeatureSetEditionDefault::kDescriptorMethods, }; return &_data_; } @@ -11938,12 +12019,15 @@ inline void FeatureSetDefaults::SharedDtor() { const ::google::protobuf::MessageLite::ClassData* FeatureSetDefaults::GetClassData() const { - PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite::ClassData - _data_ = { - FeatureSetDefaults::MergeImpl, - nullptr, // OnDemandRegisterArenaDtor - &::google::protobuf::Message::kDescriptorMethods, + PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite:: + ClassDataFull _data_ = { + { + nullptr, // OnDemandRegisterArenaDtor PROTOBUF_FIELD_OFFSET(FeatureSetDefaults, _impl_._cached_size_), + false, + }, + &FeatureSetDefaults::MergeImpl, + &FeatureSetDefaults::kDescriptorMethods, }; return &_data_; } @@ -12209,12 +12293,15 @@ inline void SourceCodeInfo_Location::SharedDtor() { const ::google::protobuf::MessageLite::ClassData* SourceCodeInfo_Location::GetClassData() const { - PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite::ClassData - _data_ = { - SourceCodeInfo_Location::MergeImpl, - nullptr, // OnDemandRegisterArenaDtor - &::google::protobuf::Message::kDescriptorMethods, + PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite:: + ClassDataFull _data_ = { + { + nullptr, // OnDemandRegisterArenaDtor PROTOBUF_FIELD_OFFSET(SourceCodeInfo_Location, _impl_._cached_size_), + false, + }, + &SourceCodeInfo_Location::MergeImpl, + &SourceCodeInfo_Location::kDescriptorMethods, }; return &_data_; } @@ -12533,12 +12620,15 @@ inline void SourceCodeInfo::SharedDtor() { const ::google::protobuf::MessageLite::ClassData* SourceCodeInfo::GetClassData() const { - PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite::ClassData - _data_ = { - SourceCodeInfo::MergeImpl, - nullptr, // OnDemandRegisterArenaDtor - &::google::protobuf::Message::kDescriptorMethods, + PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite:: + ClassDataFull _data_ = { + { + nullptr, // OnDemandRegisterArenaDtor PROTOBUF_FIELD_OFFSET(SourceCodeInfo, _impl_._cached_size_), + false, + }, + &SourceCodeInfo::MergeImpl, + &SourceCodeInfo::kDescriptorMethods, }; return &_data_; } @@ -12741,12 +12831,15 @@ inline void GeneratedCodeInfo_Annotation::SharedDtor() { const ::google::protobuf::MessageLite::ClassData* GeneratedCodeInfo_Annotation::GetClassData() const { - PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite::ClassData - _data_ = { - GeneratedCodeInfo_Annotation::MergeImpl, - nullptr, // OnDemandRegisterArenaDtor - &::google::protobuf::Message::kDescriptorMethods, + PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite:: + ClassDataFull _data_ = { + { + nullptr, // OnDemandRegisterArenaDtor PROTOBUF_FIELD_OFFSET(GeneratedCodeInfo_Annotation, _impl_._cached_size_), + false, + }, + &GeneratedCodeInfo_Annotation::MergeImpl, + &GeneratedCodeInfo_Annotation::kDescriptorMethods, }; return &_data_; } @@ -13057,12 +13150,15 @@ inline void GeneratedCodeInfo::SharedDtor() { const ::google::protobuf::MessageLite::ClassData* GeneratedCodeInfo::GetClassData() const { - PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite::ClassData - _data_ = { - GeneratedCodeInfo::MergeImpl, - nullptr, // OnDemandRegisterArenaDtor - &::google::protobuf::Message::kDescriptorMethods, + PROTOBUF_CONSTINIT static const ::google::protobuf::MessageLite:: + ClassDataFull _data_ = { + { + nullptr, // OnDemandRegisterArenaDtor PROTOBUF_FIELD_OFFSET(GeneratedCodeInfo, _impl_._cached_size_), + false, + }, + &GeneratedCodeInfo::MergeImpl, + &GeneratedCodeInfo::kDescriptorMethods, }; return &_data_; } diff --git a/src/google/protobuf/dynamic_message.cc b/src/google/protobuf/dynamic_message.cc index d1f6ca6b7b..0c4ee1b7e0 100644 --- a/src/google/protobuf/dynamic_message.cc +++ b/src/google/protobuf/dynamic_message.cc @@ -203,11 +203,14 @@ class DynamicMessage final : public Message { Message* New(Arena* arena) const override; const ClassData* GetClassData() const final { - ABSL_CONST_INIT static const ClassData data = { + ABSL_CONST_INIT static const ClassDataFull data = { + { + nullptr, // on_demand_register_arena_dtor + PROTOBUF_FIELD_OFFSET(DynamicMessage, cached_byte_size_), + false, + }, &MergeImpl, - nullptr, // on_demand_register_arena_dtor &kDescriptorMethods, - PROTOBUF_FIELD_OFFSET(DynamicMessage, cached_byte_size_), }; return &data; } diff --git a/src/google/protobuf/generated_message_bases.cc b/src/google/protobuf/generated_message_bases.cc index 90a7921eb7..9ebd820c12 100644 --- a/src/google/protobuf/generated_message_bases.cc +++ b/src/google/protobuf/generated_message_bases.cc @@ -98,11 +98,14 @@ void ZeroFieldsBase::InternalSwap(ZeroFieldsBase* other) { } const Message::ClassData* ZeroFieldsBase::GetClassData() const { - ABSL_CONST_INIT static const ClassData data = { + ABSL_CONST_INIT static const ClassDataFull data = { + { + nullptr, // on_demand_register_arena_dtor + PROTOBUF_FIELD_OFFSET(ZeroFieldsBase, _cached_size_), + false, + }, &MergeImpl, - nullptr, // on_demand_register_arena_dtor &kDescriptorMethods, - PROTOBUF_FIELD_OFFSET(ZeroFieldsBase, _cached_size_), }; return &data; } diff --git a/src/google/protobuf/implicit_weak_message.h b/src/google/protobuf/implicit_weak_message.h index ebd78f1a56..708f73a151 100644 --- a/src/google/protobuf/implicit_weak_message.h +++ b/src/google/protobuf/implicit_weak_message.h @@ -63,10 +63,9 @@ class PROTOBUF_EXPORT ImplicitWeakMessage : public MessageLite { }; static constexpr Data data = { { - nullptr, // merge_impl nullptr, // on_demand_register_arena_dtor - nullptr, // descriptor_methods PROTOBUF_FIELD_OFFSET(ImplicitWeakMessage, cached_size_), + true, }, ""}; return &data.header; diff --git a/src/google/protobuf/map_entry.h b/src/google/protobuf/map_entry.h index dead40ac02..3a1712bfad 100644 --- a/src/google/protobuf/map_entry.h +++ b/src/google/protobuf/map_entry.h @@ -50,11 +50,14 @@ class MapEntryBase : public Message { using Message::Message; const ClassData* GetClassData() const final { - ABSL_CONST_INIT static const ClassData data = { + ABSL_CONST_INIT static const ClassDataFull data = { + { + nullptr, // on_demand_register_arena_dtor + PROTOBUF_FIELD_OFFSET(MapEntryBase, _cached_size_), + false, + }, &MergeImpl, - nullptr, // on_demand_register_arena_dtor &kDescriptorMethods, - PROTOBUF_FIELD_OFFSET(MapEntryBase, _cached_size_), }; return &data; } diff --git a/src/google/protobuf/message.cc b/src/google/protobuf/message.cc index 12ceac1cdc..26f6efc283 100644 --- a/src/google/protobuf/message.cc +++ b/src/google/protobuf/message.cc @@ -73,7 +73,7 @@ void Message::MergeFrom(const Message& from) { if (class_to == nullptr || class_to != class_from) { ReflectionOps::Merge(from, this); } else { - class_to->merge_to_from(*this, from); + class_to->full().merge_to_from(*this, from); } } @@ -92,7 +92,7 @@ void Message::CopyFrom(const Message& from) { ABSL_DCHECK(!internal::IsDescendant(*this, from)) << "Source of CopyFrom cannot be a descendant of the target."; Clear(); - class_to->merge_to_from(*this, from); + class_to->full().merge_to_from(*this, from); } else { const Descriptor* descriptor = GetDescriptor(); ABSL_CHECK_EQ(from.GetDescriptor(), descriptor) diff --git a/src/google/protobuf/message_lite.cc b/src/google/protobuf/message_lite.cc index 8b5e83c32a..468ffc584d 100644 --- a/src/google/protobuf/message_lite.cc +++ b/src/google/protobuf/message_lite.cc @@ -48,9 +48,9 @@ std::string MessageLite::GetTypeName() const { auto* data = GetClassData(); ABSL_DCHECK(data != nullptr); - if (data->descriptor_methods != nullptr) { + if (!data->is_lite) { // For !LITE messages, we use the descriptor method function. - return data->descriptor_methods->get_type_name(*this); + return data->full().descriptor_methods->get_type_name(*this); } // For LITE messages, the type name is a char[] just beyond ClassData. @@ -71,9 +71,9 @@ std::string MessageLite::InitializationErrorString() const { auto* data = GetClassData(); ABSL_DCHECK(data != nullptr); - if (data->descriptor_methods != nullptr) { + if (!data->is_lite) { // For !LITE messages, we use the descriptor method function. - return data->descriptor_methods->initialization_error_string(*this); + return data->full().descriptor_methods->initialization_error_string(*this); } return "(cannot determine missing fields for lite message)"; diff --git a/src/google/protobuf/message_lite.h b/src/google/protobuf/message_lite.h index 71eb546044..c04cc48b9e 100644 --- a/src/google/protobuf/message_lite.h +++ b/src/google/protobuf/message_lite.h @@ -523,29 +523,46 @@ class PROTOBUF_EXPORT MessageLite { std::string (*get_type_name)(const MessageLite&); std::string (*initialization_error_string)(const MessageLite&); }; + struct ClassDataFull; + // Note: The order of arguments in the functions is chosen so that it has + // the same ABI as the member function that calls them. Eg the `this` + // pointer becomes the first argument in the free function. struct ClassData { - // Note: The order of arguments in the functions is chosen so that it has - // the same ABI as the member function that calls them. Eg the `this` - // pointer becomes the first argument in the free function. - void (*merge_to_from)(MessageLite& to, const MessageLite& from_msg); void (*on_demand_register_arena_dtor)(MessageLite& msg, Arena& arena); - // LITE objects (ie !descriptor_methods) collocate their name as a - // char[] just beyond the ClassData. - const DescriptorMethods* descriptor_methods; // Offset of the CachedSize member. uint32_t cached_size_offset; + // LITE objects (ie !descriptor_methods) collocate their name as a + // char[] just beyond the ClassData. + bool is_lite; - constexpr ClassData(void (*merge_to_from)(MessageLite& to, - const MessageLite&), - void (*on_demand_register_arena_dtor)(MessageLite&, + constexpr ClassData(void (*on_demand_register_arena_dtor)(MessageLite&, Arena&), - const DescriptorMethods* descriptor_methods, - uint32_t cached_size_offset) - : merge_to_from(merge_to_from), - on_demand_register_arena_dtor(on_demand_register_arena_dtor), - descriptor_methods(descriptor_methods), - cached_size_offset(cached_size_offset) {} + uint32_t cached_size_offset, bool is_lite) + : on_demand_register_arena_dtor(on_demand_register_arena_dtor), + cached_size_offset(cached_size_offset), + is_lite(is_lite) {} + + const ClassDataFull& full() const { + return *static_cast(this); + } + }; + template + struct ClassDataLite { + ClassData header; + const char type_name[N]; + }; + struct ClassDataFull : ClassData { + constexpr ClassDataFull(ClassData base, + void (*merge_to_from)(MessageLite& to, + const MessageLite& from_msg), + const DescriptorMethods* descriptor_methods) + : ClassData(base), + merge_to_from(merge_to_from), + descriptor_methods(descriptor_methods) {} + + void (*merge_to_from)(MessageLite& to, const MessageLite& from_msg); + const DescriptorMethods* descriptor_methods; }; // GetClassData() returns a pointer to a ClassData struct which