From dcc96982bc0938e77048b72f39bc7e0c792a1c35 Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Mon, 8 May 2023 14:05:51 -0700 Subject: [PATCH] Refactor weak repeated message field code in order to support split repeated fields. - Make WeakRepeatedPtrField arena constructible/destructor skippable. - Add _internal_weak_$name$()/_internal_mutable_weak_$name$(). PiperOrigin-RevId: 530402675 --- .../cpp/field_generators/message_field.cc | 67 ++++++++++++------- src/google/protobuf/compiler/plugin.pb.cc | 4 +- src/google/protobuf/descriptor.pb.cc | 56 ++++++++-------- src/google/protobuf/implicit_weak_message.h | 3 + 4 files changed, 75 insertions(+), 55 deletions(-) diff --git a/src/google/protobuf/compiler/cpp/field_generators/message_field.cc b/src/google/protobuf/compiler/cpp/field_generators/message_field.cc index e0d30f8e43..2e669f6cf3 100644 --- a/src/google/protobuf/compiler/cpp/field_generators/message_field.cc +++ b/src/google/protobuf/compiler/cpp/field_generators/message_field.cc @@ -734,11 +734,7 @@ class RepeatedMessage : public FieldGeneratorBase { }; void RepeatedMessage::GeneratePrivateMembers(io::Printer* p) const { - if (weak_) { - p->Emit("$pb$::WeakRepeatedPtrField< $Submsg$ > $name$_;\n"); - } else { - p->Emit("$pb$::RepeatedPtrField< $Submsg$ > $name$_;\n"); - } + p->Emit("$pb$::$Weak$RepeatedPtrField< $Submsg$ > $name$_;\n"); } void RepeatedMessage::GenerateAccessorDeclarations(io::Printer* p) const { @@ -752,10 +748,18 @@ void RepeatedMessage::GenerateAccessorDeclarations(io::Printer* p) const { format( "private:\n" "const $pb$::RepeatedPtrField<$Submsg$>& _internal_$name$() const;\n" - "$pb$::RepeatedPtrField<$Submsg$>* _internal_mutable_$name$();\n" - "public:\n"); - format("$DEPRECATED$ const $Submsg$& ${1$$name$$}$(int index) const;\n", - field_); + "$pb$::RepeatedPtrField<$Submsg$>* _internal_mutable_$name$();\n"); + if (weak_) { + format( + "const $pb$::WeakRepeatedPtrField<$Submsg$>& _internal_weak_$name$() " + "const;\n" + "$pb$::WeakRepeatedPtrField<$Submsg$>* " + "_internal_mutable_weak_$name$();\n"); + } + format( + "public:\n" + "$DEPRECATED$ const $Submsg$& ${1$$name$$}$(int index) const;\n", + field_); format("$DEPRECATED$ $Submsg$* ${1$add_$name$$}$();\n", std::make_tuple(field_, GeneratedCodeInfo::Annotation::SET)); format( @@ -810,15 +814,28 @@ void RepeatedMessage::GenerateInlineAccessorDefinitions(io::Printer* p) const { " return _internal_$name$();\n" "}\n"); - p->Emit( - "inline const $pb$::RepeatedPtrField<$Submsg$>&\n" - "$classname$::_internal_$name$() const {\n" - " return $field$$.weak$;\n" - "}\n" - "inline $pb$::RepeatedPtrField<$Submsg$>*\n" - "$classname$::_internal_mutable_$name$() {\n" - " return &$field$$.weak$;\n" - "}\n"); + p->Emit(R"cc( + inline const $pb$::RepeatedPtrField<$Submsg$>& + $classname$::_internal_$name$() const { + return $field$$.weak$; + } + inline $pb$::RepeatedPtrField<$Submsg$>* + $classname$::_internal_mutable_$name$() { + return &$field$$.weak$; + } + )cc"); + if (weak_) { + p->Emit(R"cc( + inline const $pb$::WeakRepeatedPtrField<$Submsg$>& + $Msg$::_internal_weak_$name$() const { + return $field$; + } + inline $pb$::WeakRepeatedPtrField<$Submsg$>* + $Msg$::_internal_mutable_weak_$name$() { + return &$field$; + } + )cc"); + } } void RepeatedMessage::GenerateClearingCode(io::Printer* p) const { @@ -831,7 +848,9 @@ void RepeatedMessage::GenerateClearingCode(io::Printer* p) const { void RepeatedMessage::GenerateMergingCode(io::Printer* p) const { if (weak_) { - p->Emit("_this->$field_$.MergeFrom(from.$field_$);\n"); + p->Emit( + "_this->_internal_mutable_weak_$name$()->MergeFrom(from._internal_weak_" + "$name$());\n"); } else { p->Emit( "_this->_internal_mutable_$name$()->MergeFrom(" @@ -841,7 +860,9 @@ void RepeatedMessage::GenerateMergingCode(io::Printer* p) const { void RepeatedMessage::GenerateSwappingCode(io::Printer* p) const { if (weak_) { - p->Emit("$field_$.InternalSwap(&other->$field_$);\n"); + p->Emit( + "_internal_mutable_weak_$name$()->InternalSwap(other->_internal_" + "mutable_weak_$name$());\n"); } else { p->Emit( "_internal_mutable_$name$()->InternalSwap(other->_internal_mutable_" @@ -854,11 +875,7 @@ void RepeatedMessage::GenerateConstructorCode(io::Printer* p) const { } void RepeatedMessage::GenerateDestructorCode(io::Printer* p) const { - if (weak_) { - p->Emit("$field_$.~WeakRepeatedPtrField();\n"); - } else { - p->Emit("_internal_mutable_$name$()->~RepeatedPtrField();\n"); - } + p->Emit("$field_$.~$Weak$RepeatedPtrField();\n"); } void RepeatedMessage::GenerateSerializeWithCachedSizesToArray( diff --git a/src/google/protobuf/compiler/plugin.pb.cc b/src/google/protobuf/compiler/plugin.pb.cc index e20b094744..f4d69dcf7e 100644 --- a/src/google/protobuf/compiler/plugin.pb.cc +++ b/src/google/protobuf/compiler/plugin.pb.cc @@ -706,7 +706,7 @@ CodeGeneratorRequest::~CodeGeneratorRequest() { inline void CodeGeneratorRequest::SharedDtor() { ABSL_DCHECK(GetArenaForAllocation() == nullptr); _internal_mutable_file_to_generate()->~RepeatedPtrField(); - _internal_mutable_proto_file()->~RepeatedPtrField(); + _impl_.proto_file_.~RepeatedPtrField(); _impl_.parameter_.Destroy(); if (this != internal_default_instance()) delete _impl_.compiler_version_; } @@ -1422,7 +1422,7 @@ CodeGeneratorResponse::~CodeGeneratorResponse() { inline void CodeGeneratorResponse::SharedDtor() { ABSL_DCHECK(GetArenaForAllocation() == nullptr); - _internal_mutable_file()->~RepeatedPtrField(); + _impl_.file_.~RepeatedPtrField(); _impl_.error_.Destroy(); } diff --git a/src/google/protobuf/descriptor.pb.cc b/src/google/protobuf/descriptor.pb.cc index bb4cbc87eb..a67c00aa7a 100644 --- a/src/google/protobuf/descriptor.pb.cc +++ b/src/google/protobuf/descriptor.pb.cc @@ -1875,7 +1875,7 @@ FileDescriptorSet::~FileDescriptorSet() { inline void FileDescriptorSet::SharedDtor() { ABSL_DCHECK(GetArenaForAllocation() == nullptr); - _internal_mutable_file()->~RepeatedPtrField(); + _impl_.file_.~RepeatedPtrField(); } void FileDescriptorSet::SetCachedSize(int size) const { @@ -2177,10 +2177,10 @@ FileDescriptorProto::~FileDescriptorProto() { inline void FileDescriptorProto::SharedDtor() { ABSL_DCHECK(GetArenaForAllocation() == nullptr); _internal_mutable_dependency()->~RepeatedPtrField(); - _internal_mutable_message_type()->~RepeatedPtrField(); - _internal_mutable_enum_type()->~RepeatedPtrField(); - _internal_mutable_service()->~RepeatedPtrField(); - _internal_mutable_extension()->~RepeatedPtrField(); + _impl_.message_type_.~RepeatedPtrField(); + _impl_.enum_type_.~RepeatedPtrField(); + _impl_.service_.~RepeatedPtrField(); + _impl_.extension_.~RepeatedPtrField(); _impl_.public_dependency_.~RepeatedField(); _impl_.weak_dependency_.~RepeatedField(); _impl_.name_.Destroy(); @@ -3389,13 +3389,13 @@ DescriptorProto::~DescriptorProto() { inline void DescriptorProto::SharedDtor() { ABSL_DCHECK(GetArenaForAllocation() == nullptr); - _internal_mutable_field()->~RepeatedPtrField(); - _internal_mutable_nested_type()->~RepeatedPtrField(); - _internal_mutable_enum_type()->~RepeatedPtrField(); - _internal_mutable_extension_range()->~RepeatedPtrField(); - _internal_mutable_extension()->~RepeatedPtrField(); - _internal_mutable_oneof_decl()->~RepeatedPtrField(); - _internal_mutable_reserved_range()->~RepeatedPtrField(); + _impl_.field_.~RepeatedPtrField(); + _impl_.nested_type_.~RepeatedPtrField(); + _impl_.enum_type_.~RepeatedPtrField(); + _impl_.extension_range_.~RepeatedPtrField(); + _impl_.extension_.~RepeatedPtrField(); + _impl_.oneof_decl_.~RepeatedPtrField(); + _impl_.reserved_range_.~RepeatedPtrField(); _internal_mutable_reserved_name()->~RepeatedPtrField(); _impl_.name_.Destroy(); if (this != internal_default_instance()) delete _impl_.options_; @@ -4346,8 +4346,8 @@ ExtensionRangeOptions::~ExtensionRangeOptions() { inline void ExtensionRangeOptions::SharedDtor() { ABSL_DCHECK(GetArenaForAllocation() == nullptr); _impl_._extensions_.~ExtensionSet(); - _internal_mutable_declaration()->~RepeatedPtrField(); - _internal_mutable_uninterpreted_option()->~RepeatedPtrField(); + _impl_.declaration_.~RepeatedPtrField(); + _impl_.uninterpreted_option_.~RepeatedPtrField(); } void ExtensionRangeOptions::SetCachedSize(int size) const { @@ -5844,8 +5844,8 @@ EnumDescriptorProto::~EnumDescriptorProto() { inline void EnumDescriptorProto::SharedDtor() { ABSL_DCHECK(GetArenaForAllocation() == nullptr); - _internal_mutable_value()->~RepeatedPtrField(); - _internal_mutable_reserved_range()->~RepeatedPtrField(); + _impl_.value_.~RepeatedPtrField(); + _impl_.reserved_range_.~RepeatedPtrField(); _internal_mutable_reserved_name()->~RepeatedPtrField(); _impl_.name_.Destroy(); if (this != internal_default_instance()) delete _impl_.options_; @@ -6533,7 +6533,7 @@ ServiceDescriptorProto::~ServiceDescriptorProto() { inline void ServiceDescriptorProto::SharedDtor() { ABSL_DCHECK(GetArenaForAllocation() == nullptr); - _internal_mutable_method()->~RepeatedPtrField(); + _impl_.method_.~RepeatedPtrField(); _impl_.name_.Destroy(); if (this != internal_default_instance()) delete _impl_.options_; } @@ -7521,7 +7521,7 @@ FileOptions::~FileOptions() { inline void FileOptions::SharedDtor() { ABSL_DCHECK(GetArenaForAllocation() == nullptr); _impl_._extensions_.~ExtensionSet(); - _internal_mutable_uninterpreted_option()->~RepeatedPtrField(); + _impl_.uninterpreted_option_.~RepeatedPtrField(); _impl_.java_package_.Destroy(); _impl_.java_outer_classname_.Destroy(); _impl_.go_package_.Destroy(); @@ -8431,7 +8431,7 @@ MessageOptions::~MessageOptions() { inline void MessageOptions::SharedDtor() { ABSL_DCHECK(GetArenaForAllocation() == nullptr); _impl_._extensions_.~ExtensionSet(); - _internal_mutable_uninterpreted_option()->~RepeatedPtrField(); + _impl_.uninterpreted_option_.~RepeatedPtrField(); } void MessageOptions::SetCachedSize(int size) const { @@ -8869,7 +8869,7 @@ inline void FieldOptions::SharedDtor() { ABSL_DCHECK(GetArenaForAllocation() == nullptr); _impl_._extensions_.~ExtensionSet(); _internal_mutable_targets()->~RepeatedField(); - _internal_mutable_uninterpreted_option()->~RepeatedPtrField(); + _impl_.uninterpreted_option_.~RepeatedPtrField(); } void FieldOptions::SetCachedSize(int size) const { @@ -9426,7 +9426,7 @@ OneofOptions::~OneofOptions() { inline void OneofOptions::SharedDtor() { ABSL_DCHECK(GetArenaForAllocation() == nullptr); _impl_._extensions_.~ExtensionSet(); - _internal_mutable_uninterpreted_option()->~RepeatedPtrField(); + _impl_.uninterpreted_option_.~RepeatedPtrField(); } void OneofOptions::SetCachedSize(int size) const { @@ -9661,7 +9661,7 @@ EnumOptions::~EnumOptions() { inline void EnumOptions::SharedDtor() { ABSL_DCHECK(GetArenaForAllocation() == nullptr); _impl_._extensions_.~ExtensionSet(); - _internal_mutable_uninterpreted_option()->~RepeatedPtrField(); + _impl_.uninterpreted_option_.~RepeatedPtrField(); } void EnumOptions::SetCachedSize(int size) const { @@ -9976,7 +9976,7 @@ EnumValueOptions::~EnumValueOptions() { inline void EnumValueOptions::SharedDtor() { ABSL_DCHECK(GetArenaForAllocation() == nullptr); _impl_._extensions_.~ExtensionSet(); - _internal_mutable_uninterpreted_option()->~RepeatedPtrField(); + _impl_.uninterpreted_option_.~RepeatedPtrField(); } void EnumValueOptions::SetCachedSize(int size) const { @@ -10228,7 +10228,7 @@ ServiceOptions::~ServiceOptions() { inline void ServiceOptions::SharedDtor() { ABSL_DCHECK(GetArenaForAllocation() == nullptr); _impl_._extensions_.~ExtensionSet(); - _internal_mutable_uninterpreted_option()->~RepeatedPtrField(); + _impl_.uninterpreted_option_.~RepeatedPtrField(); } void ServiceOptions::SetCachedSize(int size) const { @@ -10489,7 +10489,7 @@ MethodOptions::~MethodOptions() { inline void MethodOptions::SharedDtor() { ABSL_DCHECK(GetArenaForAllocation() == nullptr); _impl_._extensions_.~ExtensionSet(); - _internal_mutable_uninterpreted_option()->~RepeatedPtrField(); + _impl_.uninterpreted_option_.~RepeatedPtrField(); } void MethodOptions::SetCachedSize(int size) const { @@ -11127,7 +11127,7 @@ UninterpretedOption::~UninterpretedOption() { inline void UninterpretedOption::SharedDtor() { ABSL_DCHECK(GetArenaForAllocation() == nullptr); - _internal_mutable_name()->~RepeatedPtrField(); + _impl_.name_.~RepeatedPtrField(); _impl_.identifier_value_.Destroy(); _impl_.string_value_.Destroy(); _impl_.aggregate_value_.Destroy(); @@ -11925,7 +11925,7 @@ SourceCodeInfo::~SourceCodeInfo() { inline void SourceCodeInfo::SharedDtor() { ABSL_DCHECK(GetArenaForAllocation() == nullptr); - _internal_mutable_location()->~RepeatedPtrField(); + _impl_.location_.~RepeatedPtrField(); } void SourceCodeInfo::SetCachedSize(int size) const { @@ -12490,7 +12490,7 @@ GeneratedCodeInfo::~GeneratedCodeInfo() { inline void GeneratedCodeInfo::SharedDtor() { ABSL_DCHECK(GetArenaForAllocation() == nullptr); - _internal_mutable_annotation()->~RepeatedPtrField(); + _impl_.annotation_.~RepeatedPtrField(); } void GeneratedCodeInfo::SetCachedSize(int size) const { diff --git a/src/google/protobuf/implicit_weak_message.h b/src/google/protobuf/implicit_weak_message.h index 979c89750c..5cb36ddda4 100644 --- a/src/google/protobuf/implicit_weak_message.h +++ b/src/google/protobuf/implicit_weak_message.h @@ -152,6 +152,9 @@ class ImplicitWeakTypeHandler { template struct WeakRepeatedPtrField { + using InternalArenaConstructable_ = void; + using DestructorSkippable_ = void; + using TypeHandler = internal::ImplicitWeakTypeHandler; constexpr WeakRepeatedPtrField() : weak() {} explicit WeakRepeatedPtrField(Arena* arena) : weak(arena) {}