diff --git a/src/google/protobuf/compiler/cpp/message.cc b/src/google/protobuf/compiler/cpp/message.cc index b033b06699..5411768d55 100644 --- a/src/google/protobuf/compiler/cpp/message.cc +++ b/src/google/protobuf/compiler/cpp/message.cc @@ -1558,14 +1558,16 @@ void MessageGenerator::GenerateClassDefinition(io::Printer* p) { } if (!HasSimpleBaseClass(descriptor_, options_)) { - format( - "int GetCachedSize() const final { return " - "$cached_size$.Get(); }" - "\n\nprivate:\n" - "void SharedCtor(::$proto_ns$::Arena* arena);\n" - "void SharedDtor();\n" - "void SetCachedSize(int size) const$ full_final$;\n" - "void InternalSwap($classname$* other);\n"); + p->Emit( + R"cc( + int GetCachedSize() const { return $cached_size$.Get(); } + + private: + $pbi$::CachedSize* AccessCachedSize() const final; + void SharedCtor(::$proto_ns$::Arena* arena); + void SharedDtor(); + void InternalSwap($classname$* other); + )cc"); } format( @@ -2100,6 +2102,14 @@ void MessageGenerator::GenerateClassMethods(io::Printer* p) { DefaultInstanceName(descriptor_, options_, /*split=*/false)); } + if (!HasSimpleBaseClass(descriptor_, options_)) { + p->Emit(R"cc( + ::_pbi::CachedSize* $classname$::AccessCachedSize() const { + return &$cached_size$; + } + )cc"); + } + GenerateVerify(p); GenerateSwap(p); @@ -3515,15 +3525,6 @@ void MessageGenerator::GenerateStructors(io::Printer* p) { if (NeedsArenaDestructor() > ArenaDtorNeeds::kNone) { GenerateArenaDestructorCode(p); } - - if (!HasSimpleBaseClass(descriptor_, options_)) { - // Generate SetCachedSize. - p->Emit(R"cc( - void $classname$::SetCachedSize(int size) const { - $cached_size$.Set(size); - } - )cc"); - } } void MessageGenerator::GenerateSourceInProto2Namespace(io::Printer* p) { @@ -4731,8 +4732,7 @@ void MessageGenerator::GenerateByteSize(io::Printer* p) { if ($have_unknown_fields$) { total_size += ::_pbi::ComputeUnknownMessageSetItemsSize($unknown_fields$); } - int cached_size = ::_pbi::ToCachedSize(total_size); - SetCachedSize(cached_size); + $cached_size$.Set(::_pbi::ToCachedSize(total_size)); return total_size; } )cc"); @@ -4900,10 +4900,10 @@ void MessageGenerator::GenerateByteSize(io::Printer* p) { // abstract away the underlying atomic. This makes it easier on platforms // where even relaxed memory order might have perf impact to replace it with // ordinary loads and stores. - format( - "int cached_size = ::_pbi::ToCachedSize(total_size);\n" - "SetCachedSize(cached_size);\n" - "return total_size;\n"); + p->Emit(R"cc( + $cached_size$.Set(::_pbi::ToCachedSize(total_size)); + return total_size; + )cc"); } format.Outdent(); diff --git a/src/google/protobuf/compiler/plugin.pb.cc b/src/google/protobuf/compiler/plugin.pb.cc index 83ae35b759..2c874eff87 100644 --- a/src/google/protobuf/compiler/plugin.pb.cc +++ b/src/google/protobuf/compiler/plugin.pb.cc @@ -375,9 +375,6 @@ inline void Version::SharedDtor() { ABSL_DCHECK(GetArenaForAllocation() == nullptr); _impl_.suffix_.Destroy(); } -void Version::SetCachedSize(int size) const { - _impl_._cached_size_.Set(size); -} PROTOBUF_NOINLINE void Version::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.compiler.Version) @@ -586,6 +583,9 @@ PROTOBUF_NOINLINE bool Version::IsInitialized() const { return true; } +::_pbi::CachedSize* Version::AccessCachedSize() const { + return &_impl_._cached_size_; +} void Version::InternalSwap(Version* other) { using std::swap; auto* lhs_arena = GetArenaForAllocation(); @@ -695,9 +695,6 @@ inline void CodeGeneratorRequest::SharedDtor() { _impl_.parameter_.Destroy(); if (this != internal_default_instance()) delete _impl_.compiler_version_; } -void CodeGeneratorRequest::SetCachedSize(int size) const { - _impl_._cached_size_.Set(size); -} PROTOBUF_NOINLINE void CodeGeneratorRequest::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.compiler.CodeGeneratorRequest) @@ -940,6 +937,9 @@ PROTOBUF_NOINLINE bool CodeGeneratorRequest::IsInitialized() const { return true; } +::_pbi::CachedSize* CodeGeneratorRequest::AccessCachedSize() const { + return &_impl_._cached_size_; +} void CodeGeneratorRequest::InternalSwap(CodeGeneratorRequest* other) { using std::swap; auto* lhs_arena = GetArenaForAllocation(); @@ -1069,9 +1069,6 @@ inline void CodeGeneratorResponse_File::SharedDtor() { _impl_.content_.Destroy(); if (this != internal_default_instance()) delete _impl_.generated_code_info_; } -void CodeGeneratorResponse_File::SetCachedSize(int size) const { - _impl_._cached_size_.Set(size); -} PROTOBUF_NOINLINE void CodeGeneratorResponse_File::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.compiler.CodeGeneratorResponse.File) @@ -1292,6 +1289,9 @@ PROTOBUF_NOINLINE bool CodeGeneratorResponse_File::IsInitialized() const { return true; } +::_pbi::CachedSize* CodeGeneratorResponse_File::AccessCachedSize() const { + return &_impl_._cached_size_; +} void CodeGeneratorResponse_File::InternalSwap(CodeGeneratorResponse_File* other) { using std::swap; auto* lhs_arena = GetArenaForAllocation(); @@ -1379,9 +1379,6 @@ inline void CodeGeneratorResponse::SharedDtor() { _impl_.file_.~RepeatedPtrField(); _impl_.error_.Destroy(); } -void CodeGeneratorResponse::SetCachedSize(int size) const { - _impl_._cached_size_.Set(size); -} PROTOBUF_NOINLINE void CodeGeneratorResponse::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.compiler.CodeGeneratorResponse) @@ -1566,6 +1563,9 @@ PROTOBUF_NOINLINE bool CodeGeneratorResponse::IsInitialized() const { return true; } +::_pbi::CachedSize* CodeGeneratorResponse::AccessCachedSize() const { + return &_impl_._cached_size_; +} void CodeGeneratorResponse::InternalSwap(CodeGeneratorResponse* other) { using std::swap; auto* lhs_arena = GetArenaForAllocation(); diff --git a/src/google/protobuf/compiler/plugin.pb.h b/src/google/protobuf/compiler/plugin.pb.h index 14f788a75a..d37d1ef779 100644 --- a/src/google/protobuf/compiler/plugin.pb.h +++ b/src/google/protobuf/compiler/plugin.pb.h @@ -226,12 +226,12 @@ class PROTOC_EXPORT Version final : const char* _InternalParse(const char* ptr, ::google::protobuf::internal::ParseContext* ctx) final; ::uint8_t* _InternalSerialize( ::uint8_t* target, ::google::protobuf::io::EpsCopyOutputStream* stream) const final; - int GetCachedSize() const final { return _impl_._cached_size_.Get(); } + int GetCachedSize() const { return _impl_._cached_size_.Get(); } private: + ::google::protobuf::internal::CachedSize* AccessCachedSize() const final; void SharedCtor(::google::protobuf::Arena* arena); void SharedDtor(); - void SetCachedSize(int size) const final; void InternalSwap(Version* other); private: @@ -440,12 +440,12 @@ class PROTOC_EXPORT CodeGeneratorResponse_File final : const char* _InternalParse(const char* ptr, ::google::protobuf::internal::ParseContext* ctx) final; ::uint8_t* _InternalSerialize( ::uint8_t* target, ::google::protobuf::io::EpsCopyOutputStream* stream) const final; - int GetCachedSize() const final { return _impl_._cached_size_.Get(); } + int GetCachedSize() const { return _impl_._cached_size_.Get(); } private: + ::google::protobuf::internal::CachedSize* AccessCachedSize() const final; void SharedCtor(::google::protobuf::Arena* arena); void SharedDtor(); - void SetCachedSize(int size) const final; void InternalSwap(CodeGeneratorResponse_File* other); private: @@ -670,12 +670,12 @@ class PROTOC_EXPORT CodeGeneratorResponse final : const char* _InternalParse(const char* ptr, ::google::protobuf::internal::ParseContext* ctx) final; ::uint8_t* _InternalSerialize( ::uint8_t* target, ::google::protobuf::io::EpsCopyOutputStream* stream) const final; - int GetCachedSize() const final { return _impl_._cached_size_.Get(); } + int GetCachedSize() const { return _impl_._cached_size_.Get(); } private: + ::google::protobuf::internal::CachedSize* AccessCachedSize() const final; void SharedCtor(::google::protobuf::Arena* arena); void SharedDtor(); - void SetCachedSize(int size) const final; void InternalSwap(CodeGeneratorResponse* other); private: @@ -901,12 +901,12 @@ class PROTOC_EXPORT CodeGeneratorRequest final : const char* _InternalParse(const char* ptr, ::google::protobuf::internal::ParseContext* ctx) final; ::uint8_t* _InternalSerialize( ::uint8_t* target, ::google::protobuf::io::EpsCopyOutputStream* stream) const final; - int GetCachedSize() const final { return _impl_._cached_size_.Get(); } + int GetCachedSize() const { return _impl_._cached_size_.Get(); } private: + ::google::protobuf::internal::CachedSize* AccessCachedSize() const final; void SharedCtor(::google::protobuf::Arena* arena); void SharedDtor(); - void SetCachedSize(int size) const final; void InternalSwap(CodeGeneratorRequest* other); private: diff --git a/src/google/protobuf/cpp_features.pb.cc b/src/google/protobuf/cpp_features.pb.cc index 2cfc851929..442d1e0561 100644 --- a/src/google/protobuf/cpp_features.pb.cc +++ b/src/google/protobuf/cpp_features.pb.cc @@ -183,9 +183,6 @@ CppFeatures::~CppFeatures() { inline void CppFeatures::SharedDtor() { ABSL_DCHECK(GetArenaForAllocation() == nullptr); } -void CppFeatures::SetCachedSize(int size) const { - _impl_._cached_size_.Set(size); -} PROTOBUF_NOINLINE void CppFeatures::Clear() { // @@protoc_insertion_point(message_clear_start:pb.CppFeatures) @@ -342,6 +339,9 @@ PROTOBUF_NOINLINE bool CppFeatures::IsInitialized() const { return true; } +::_pbi::CachedSize* CppFeatures::AccessCachedSize() const { + return &_impl_._cached_size_; +} void CppFeatures::InternalSwap(CppFeatures* other) { using std::swap; _internal_metadata_.InternalSwap(&other->_internal_metadata_); diff --git a/src/google/protobuf/cpp_features.pb.h b/src/google/protobuf/cpp_features.pb.h index 5ef9becdaa..3ff982684a 100644 --- a/src/google/protobuf/cpp_features.pb.h +++ b/src/google/protobuf/cpp_features.pb.h @@ -210,12 +210,12 @@ class PROTOBUF_EXPORT CppFeatures final : const char* _InternalParse(const char* ptr, ::google::protobuf::internal::ParseContext* ctx) final; ::uint8_t* _InternalSerialize( ::uint8_t* target, ::google::protobuf::io::EpsCopyOutputStream* stream) const final; - int GetCachedSize() const final { return _impl_._cached_size_.Get(); } + int GetCachedSize() const { return _impl_._cached_size_.Get(); } private: + ::google::protobuf::internal::CachedSize* AccessCachedSize() const final; void SharedCtor(::google::protobuf::Arena* arena); void SharedDtor(); - void SetCachedSize(int size) const final; void InternalSwap(CppFeatures* other); private: diff --git a/src/google/protobuf/descriptor.pb.cc b/src/google/protobuf/descriptor.pb.cc index c20b58d20a..5e6af5404b 100644 --- a/src/google/protobuf/descriptor.pb.cc +++ b/src/google/protobuf/descriptor.pb.cc @@ -2331,9 +2331,6 @@ inline void FileDescriptorSet::SharedDtor() { ABSL_DCHECK(GetArenaForAllocation() == nullptr); _impl_.file_.~RepeatedPtrField(); } -void FileDescriptorSet::SetCachedSize(int size) const { - _impl_._cached_size_.Set(size); -} PROTOBUF_NOINLINE void FileDescriptorSet::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.FileDescriptorSet) @@ -2456,6 +2453,9 @@ PROTOBUF_NOINLINE bool FileDescriptorSet::IsInitialized() const { return true; } +::_pbi::CachedSize* FileDescriptorSet::AccessCachedSize() const { + return &_impl_._cached_size_; +} void FileDescriptorSet::InternalSwap(FileDescriptorSet* other) { using std::swap; _internal_metadata_.InternalSwap(&other->_internal_metadata_); @@ -2623,9 +2623,6 @@ inline void FileDescriptorProto::SharedDtor() { if (this != internal_default_instance()) delete _impl_.options_; if (this != internal_default_instance()) delete _impl_.source_code_info_; } -void FileDescriptorProto::SetCachedSize(int size) const { - _impl_._cached_size_.Set(size); -} PROTOBUF_NOINLINE void FileDescriptorProto::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.FileDescriptorProto) @@ -3084,6 +3081,9 @@ PROTOBUF_NOINLINE bool FileDescriptorProto::IsInitialized() const { return true; } +::_pbi::CachedSize* FileDescriptorProto::AccessCachedSize() const { + return &_impl_._cached_size_; +} void FileDescriptorProto::InternalSwap(FileDescriptorProto* other) { using std::swap; auto* lhs_arena = GetArenaForAllocation(); @@ -3185,9 +3185,6 @@ inline void DescriptorProto_ExtensionRange::SharedDtor() { ABSL_DCHECK(GetArenaForAllocation() == nullptr); if (this != internal_default_instance()) delete _impl_.options_; } -void DescriptorProto_ExtensionRange::SetCachedSize(int size) const { - _impl_._cached_size_.Set(size); -} PROTOBUF_NOINLINE void DescriptorProto_ExtensionRange::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.DescriptorProto.ExtensionRange) @@ -3376,6 +3373,9 @@ PROTOBUF_NOINLINE bool DescriptorProto_ExtensionRange::IsInitialized() const { return true; } +::_pbi::CachedSize* DescriptorProto_ExtensionRange::AccessCachedSize() const { + return &_impl_._cached_size_; +} void DescriptorProto_ExtensionRange::InternalSwap(DescriptorProto_ExtensionRange* other) { using std::swap; _internal_metadata_.InternalSwap(&other->_internal_metadata_); @@ -3436,9 +3436,6 @@ DescriptorProto_ReservedRange::~DescriptorProto_ReservedRange() { inline void DescriptorProto_ReservedRange::SharedDtor() { ABSL_DCHECK(GetArenaForAllocation() == nullptr); } -void DescriptorProto_ReservedRange::SetCachedSize(int size) const { - _impl_._cached_size_.Set(size); -} PROTOBUF_NOINLINE void DescriptorProto_ReservedRange::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.DescriptorProto.ReservedRange) @@ -3595,6 +3592,9 @@ PROTOBUF_NOINLINE bool DescriptorProto_ReservedRange::IsInitialized() const { return true; } +::_pbi::CachedSize* DescriptorProto_ReservedRange::AccessCachedSize() const { + return &_impl_._cached_size_; +} void DescriptorProto_ReservedRange::InternalSwap(DescriptorProto_ReservedRange* other) { using std::swap; _internal_metadata_.InternalSwap(&other->_internal_metadata_); @@ -3707,9 +3707,6 @@ inline void DescriptorProto::SharedDtor() { _impl_.name_.Destroy(); if (this != internal_default_instance()) delete _impl_.options_; } -void DescriptorProto::SetCachedSize(int size) const { - _impl_._cached_size_.Set(size); -} PROTOBUF_NOINLINE void DescriptorProto::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.DescriptorProto) @@ -4087,6 +4084,9 @@ PROTOBUF_NOINLINE bool DescriptorProto::IsInitialized() const { return true; } +::_pbi::CachedSize* DescriptorProto::AccessCachedSize() const { + return &_impl_._cached_size_; +} void DescriptorProto::InternalSwap(DescriptorProto* other) { using std::swap; auto* lhs_arena = GetArenaForAllocation(); @@ -4204,9 +4204,6 @@ inline void ExtensionRangeOptions_Declaration::SharedDtor() { _impl_.full_name_.Destroy(); _impl_.type_.Destroy(); } -void ExtensionRangeOptions_Declaration::SetCachedSize(int size) const { - _impl_._cached_size_.Set(size); -} PROTOBUF_NOINLINE void ExtensionRangeOptions_Declaration::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.ExtensionRangeOptions.Declaration) @@ -4444,6 +4441,9 @@ PROTOBUF_NOINLINE bool ExtensionRangeOptions_Declaration::IsInitialized() const return true; } +::_pbi::CachedSize* ExtensionRangeOptions_Declaration::AccessCachedSize() const { + return &_impl_._cached_size_; +} void ExtensionRangeOptions_Declaration::InternalSwap(ExtensionRangeOptions_Declaration* other) { using std::swap; auto* lhs_arena = GetArenaForAllocation(); @@ -4538,9 +4538,6 @@ inline void ExtensionRangeOptions::SharedDtor() { _impl_.uninterpreted_option_.~RepeatedPtrField(); if (this != internal_default_instance()) delete _impl_.features_; } -void ExtensionRangeOptions::SetCachedSize(int size) const { - _impl_._cached_size_.Set(size); -} PROTOBUF_NOINLINE void ExtensionRangeOptions::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.ExtensionRangeOptions) @@ -4770,6 +4767,9 @@ PROTOBUF_NOINLINE bool ExtensionRangeOptions::IsInitialized() const { return true; } +::_pbi::CachedSize* ExtensionRangeOptions::AccessCachedSize() const { + return &_impl_._cached_size_; +} void ExtensionRangeOptions::InternalSwap(ExtensionRangeOptions* other) { using std::swap; _impl_._extensions_.InternalSwap(&other->_impl_._extensions_); @@ -4957,9 +4957,6 @@ inline void FieldDescriptorProto::SharedDtor() { _impl_.json_name_.Destroy(); if (this != internal_default_instance()) delete _impl_.options_; } -void FieldDescriptorProto::SetCachedSize(int size) const { - _impl_._cached_size_.Set(size); -} PROTOBUF_NOINLINE void FieldDescriptorProto::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.FieldDescriptorProto) @@ -5366,6 +5363,9 @@ PROTOBUF_NOINLINE bool FieldDescriptorProto::IsInitialized() const { return true; } +::_pbi::CachedSize* FieldDescriptorProto::AccessCachedSize() const { + return &_impl_._cached_size_; +} void FieldDescriptorProto::InternalSwap(FieldDescriptorProto* other) { using std::swap; auto* lhs_arena = GetArenaForAllocation(); @@ -5466,9 +5466,6 @@ inline void OneofDescriptorProto::SharedDtor() { _impl_.name_.Destroy(); if (this != internal_default_instance()) delete _impl_.options_; } -void OneofDescriptorProto::SetCachedSize(int size) const { - _impl_._cached_size_.Set(size); -} PROTOBUF_NOINLINE void OneofDescriptorProto::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.OneofDescriptorProto) @@ -5637,6 +5634,9 @@ PROTOBUF_NOINLINE bool OneofDescriptorProto::IsInitialized() const { return true; } +::_pbi::CachedSize* OneofDescriptorProto::AccessCachedSize() const { + return &_impl_._cached_size_; +} void OneofDescriptorProto::InternalSwap(OneofDescriptorProto* other) { using std::swap; auto* lhs_arena = GetArenaForAllocation(); @@ -5696,9 +5696,6 @@ EnumDescriptorProto_EnumReservedRange::~EnumDescriptorProto_EnumReservedRange() inline void EnumDescriptorProto_EnumReservedRange::SharedDtor() { ABSL_DCHECK(GetArenaForAllocation() == nullptr); } -void EnumDescriptorProto_EnumReservedRange::SetCachedSize(int size) const { - _impl_._cached_size_.Set(size); -} PROTOBUF_NOINLINE void EnumDescriptorProto_EnumReservedRange::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.EnumDescriptorProto.EnumReservedRange) @@ -5855,6 +5852,9 @@ PROTOBUF_NOINLINE bool EnumDescriptorProto_EnumReservedRange::IsInitialized() co return true; } +::_pbi::CachedSize* EnumDescriptorProto_EnumReservedRange::AccessCachedSize() const { + return &_impl_._cached_size_; +} void EnumDescriptorProto_EnumReservedRange::InternalSwap(EnumDescriptorProto_EnumReservedRange* other) { using std::swap; _internal_metadata_.InternalSwap(&other->_internal_metadata_); @@ -5952,9 +5952,6 @@ inline void EnumDescriptorProto::SharedDtor() { _impl_.name_.Destroy(); if (this != internal_default_instance()) delete _impl_.options_; } -void EnumDescriptorProto::SetCachedSize(int size) const { - _impl_._cached_size_.Set(size); -} PROTOBUF_NOINLINE void EnumDescriptorProto::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.EnumDescriptorProto) @@ -6199,6 +6196,9 @@ PROTOBUF_NOINLINE bool EnumDescriptorProto::IsInitialized() const { return true; } +::_pbi::CachedSize* EnumDescriptorProto::AccessCachedSize() const { + return &_impl_._cached_size_; +} void EnumDescriptorProto::InternalSwap(EnumDescriptorProto* other) { using std::swap; auto* lhs_arena = GetArenaForAllocation(); @@ -6295,9 +6295,6 @@ inline void EnumValueDescriptorProto::SharedDtor() { _impl_.name_.Destroy(); if (this != internal_default_instance()) delete _impl_.options_; } -void EnumValueDescriptorProto::SetCachedSize(int size) const { - _impl_._cached_size_.Set(size); -} PROTOBUF_NOINLINE void EnumValueDescriptorProto::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.EnumValueDescriptorProto) @@ -6491,6 +6488,9 @@ PROTOBUF_NOINLINE bool EnumValueDescriptorProto::IsInitialized() const { return true; } +::_pbi::CachedSize* EnumValueDescriptorProto::AccessCachedSize() const { + return &_impl_._cached_size_; +} void EnumValueDescriptorProto::InternalSwap(EnumValueDescriptorProto* other) { using std::swap; auto* lhs_arena = GetArenaForAllocation(); @@ -6586,9 +6586,6 @@ inline void ServiceDescriptorProto::SharedDtor() { _impl_.name_.Destroy(); if (this != internal_default_instance()) delete _impl_.options_; } -void ServiceDescriptorProto::SetCachedSize(int size) const { - _impl_._cached_size_.Set(size); -} PROTOBUF_NOINLINE void ServiceDescriptorProto::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.ServiceDescriptorProto) @@ -6784,6 +6781,9 @@ PROTOBUF_NOINLINE bool ServiceDescriptorProto::IsInitialized() const { return true; } +::_pbi::CachedSize* ServiceDescriptorProto::AccessCachedSize() const { + return &_impl_._cached_size_; +} void ServiceDescriptorProto::InternalSwap(ServiceDescriptorProto* other) { using std::swap; auto* lhs_arena = GetArenaForAllocation(); @@ -6919,9 +6919,6 @@ inline void MethodDescriptorProto::SharedDtor() { _impl_.output_type_.Destroy(); if (this != internal_default_instance()) delete _impl_.options_; } -void MethodDescriptorProto::SetCachedSize(int size) const { - _impl_._cached_size_.Set(size); -} PROTOBUF_NOINLINE void MethodDescriptorProto::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.MethodDescriptorProto) @@ -7192,6 +7189,9 @@ PROTOBUF_NOINLINE bool MethodDescriptorProto::IsInitialized() const { return true; } +::_pbi::CachedSize* MethodDescriptorProto::AccessCachedSize() const { + return &_impl_._cached_size_; +} void MethodDescriptorProto::InternalSwap(MethodDescriptorProto* other) { using std::swap; auto* lhs_arena = GetArenaForAllocation(); @@ -7502,9 +7502,6 @@ inline void FileOptions::SharedDtor() { _impl_.ruby_package_.Destroy(); if (this != internal_default_instance()) delete _impl_.features_; } -void FileOptions::SetCachedSize(int size) const { - _impl_._cached_size_.Set(size); -} PROTOBUF_NOINLINE void FileOptions::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.FileOptions) @@ -8193,6 +8190,9 @@ PROTOBUF_NOINLINE bool FileOptions::IsInitialized() const { return true; } +::_pbi::CachedSize* FileOptions::AccessCachedSize() const { + return &_impl_._cached_size_; +} void FileOptions::InternalSwap(FileOptions* other) { using std::swap; _impl_._extensions_.InternalSwap(&other->_impl_._extensions_); @@ -8324,9 +8324,6 @@ inline void MessageOptions::SharedDtor() { _impl_.uninterpreted_option_.~RepeatedPtrField(); if (this != internal_default_instance()) delete _impl_.features_; } -void MessageOptions::SetCachedSize(int size) const { - _impl_._cached_size_.Set(size); -} PROTOBUF_NOINLINE void MessageOptions::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.MessageOptions) @@ -8610,6 +8607,9 @@ PROTOBUF_NOINLINE bool MessageOptions::IsInitialized() const { return true; } +::_pbi::CachedSize* MessageOptions::AccessCachedSize() const { + return &_impl_._cached_size_; +} void MessageOptions::InternalSwap(MessageOptions* other) { using std::swap; _impl_._extensions_.InternalSwap(&other->_impl_._extensions_); @@ -8704,9 +8704,6 @@ inline void FieldOptions_EditionDefault::SharedDtor() { _impl_.edition_.Destroy(); _impl_.value_.Destroy(); } -void FieldOptions_EditionDefault::SetCachedSize(int size) const { - _impl_._cached_size_.Set(size); -} PROTOBUF_NOINLINE void FieldOptions_EditionDefault::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.FieldOptions.EditionDefault) @@ -8871,6 +8868,9 @@ PROTOBUF_NOINLINE bool FieldOptions_EditionDefault::IsInitialized() const { return true; } +::_pbi::CachedSize* FieldOptions_EditionDefault::AccessCachedSize() const { + return &_impl_._cached_size_; +} void FieldOptions_EditionDefault::InternalSwap(FieldOptions_EditionDefault* other) { using std::swap; auto* lhs_arena = GetArenaForAllocation(); @@ -9004,9 +9004,6 @@ inline void FieldOptions::SharedDtor() { _impl_.uninterpreted_option_.~RepeatedPtrField(); if (this != internal_default_instance()) delete _impl_.features_; } -void FieldOptions::SetCachedSize(int size) const { - _impl_._cached_size_.Set(size); -} PROTOBUF_NOINLINE void FieldOptions::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.FieldOptions) @@ -9443,6 +9440,9 @@ PROTOBUF_NOINLINE bool FieldOptions::IsInitialized() const { return true; } +::_pbi::CachedSize* FieldOptions::AccessCachedSize() const { + return &_impl_._cached_size_; +} void FieldOptions::InternalSwap(FieldOptions* other) { using std::swap; _impl_._extensions_.InternalSwap(&other->_impl_._extensions_); @@ -9526,9 +9526,6 @@ inline void OneofOptions::SharedDtor() { _impl_.uninterpreted_option_.~RepeatedPtrField(); if (this != internal_default_instance()) delete _impl_.features_; } -void OneofOptions::SetCachedSize(int size) const { - _impl_._cached_size_.Set(size); -} PROTOBUF_NOINLINE void OneofOptions::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.OneofOptions) @@ -9701,6 +9698,9 @@ PROTOBUF_NOINLINE bool OneofOptions::IsInitialized() const { return true; } +::_pbi::CachedSize* OneofOptions::AccessCachedSize() const { + return &_impl_._cached_size_; +} void OneofOptions::InternalSwap(OneofOptions* other) { using std::swap; _impl_._extensions_.InternalSwap(&other->_impl_._extensions_); @@ -9795,9 +9795,6 @@ inline void EnumOptions::SharedDtor() { _impl_.uninterpreted_option_.~RepeatedPtrField(); if (this != internal_default_instance()) delete _impl_.features_; } -void EnumOptions::SetCachedSize(int size) const { - _impl_._cached_size_.Set(size); -} PROTOBUF_NOINLINE void EnumOptions::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.EnumOptions) @@ -10041,6 +10038,9 @@ PROTOBUF_NOINLINE bool EnumOptions::IsInitialized() const { return true; } +::_pbi::CachedSize* EnumOptions::AccessCachedSize() const { + return &_impl_._cached_size_; +} void EnumOptions::InternalSwap(EnumOptions* other) { using std::swap; _impl_._extensions_.InternalSwap(&other->_impl_._extensions_); @@ -10135,9 +10135,6 @@ inline void EnumValueOptions::SharedDtor() { _impl_.uninterpreted_option_.~RepeatedPtrField(); if (this != internal_default_instance()) delete _impl_.features_; } -void EnumValueOptions::SetCachedSize(int size) const { - _impl_._cached_size_.Set(size); -} PROTOBUF_NOINLINE void EnumValueOptions::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.EnumValueOptions) @@ -10363,6 +10360,9 @@ PROTOBUF_NOINLINE bool EnumValueOptions::IsInitialized() const { return true; } +::_pbi::CachedSize* EnumValueOptions::AccessCachedSize() const { + return &_impl_._cached_size_; +} void EnumValueOptions::InternalSwap(EnumValueOptions* other) { using std::swap; _impl_._extensions_.InternalSwap(&other->_impl_._extensions_); @@ -10450,9 +10450,6 @@ inline void ServiceOptions::SharedDtor() { _impl_.uninterpreted_option_.~RepeatedPtrField(); if (this != internal_default_instance()) delete _impl_.features_; } -void ServiceOptions::SetCachedSize(int size) const { - _impl_._cached_size_.Set(size); -} PROTOBUF_NOINLINE void ServiceOptions::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.ServiceOptions) @@ -10653,6 +10650,9 @@ PROTOBUF_NOINLINE bool ServiceOptions::IsInitialized() const { return true; } +::_pbi::CachedSize* ServiceOptions::AccessCachedSize() const { + return &_impl_._cached_size_; +} void ServiceOptions::InternalSwap(ServiceOptions* other) { using std::swap; _impl_._extensions_.InternalSwap(&other->_impl_._extensions_); @@ -10747,9 +10747,6 @@ inline void MethodOptions::SharedDtor() { _impl_.uninterpreted_option_.~RepeatedPtrField(); if (this != internal_default_instance()) delete _impl_.features_; } -void MethodOptions::SetCachedSize(int size) const { - _impl_._cached_size_.Set(size); -} PROTOBUF_NOINLINE void MethodOptions::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.MethodOptions) @@ -10980,6 +10977,9 @@ PROTOBUF_NOINLINE bool MethodOptions::IsInitialized() const { return true; } +::_pbi::CachedSize* MethodOptions::AccessCachedSize() const { + return &_impl_._cached_size_; +} void MethodOptions::InternalSwap(MethodOptions* other) { using std::swap; _impl_._extensions_.InternalSwap(&other->_impl_._extensions_); @@ -11066,9 +11066,6 @@ inline void UninterpretedOption_NamePart::SharedDtor() { ABSL_DCHECK(GetArenaForAllocation() == nullptr); _impl_.name_part_.Destroy(); } -void UninterpretedOption_NamePart::SetCachedSize(int size) const { - _impl_._cached_size_.Set(size); -} PROTOBUF_NOINLINE void UninterpretedOption_NamePart::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.UninterpretedOption.NamePart) @@ -11230,6 +11227,9 @@ PROTOBUF_NOINLINE bool UninterpretedOption_NamePart::IsInitialized() const { return true; } +::_pbi::CachedSize* UninterpretedOption_NamePart::AccessCachedSize() const { + return &_impl_._cached_size_; +} void UninterpretedOption_NamePart::InternalSwap(UninterpretedOption_NamePart* other) { using std::swap; auto* lhs_arena = GetArenaForAllocation(); @@ -11359,9 +11359,6 @@ inline void UninterpretedOption::SharedDtor() { _impl_.string_value_.Destroy(); _impl_.aggregate_value_.Destroy(); } -void UninterpretedOption::SetCachedSize(int size) const { - _impl_._cached_size_.Set(size); -} PROTOBUF_NOINLINE void UninterpretedOption::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.UninterpretedOption) @@ -11647,6 +11644,9 @@ PROTOBUF_NOINLINE bool UninterpretedOption::IsInitialized() const { return true; } +::_pbi::CachedSize* UninterpretedOption::AccessCachedSize() const { + return &_impl_._cached_size_; +} void UninterpretedOption::InternalSwap(UninterpretedOption* other) { using std::swap; auto* lhs_arena = GetArenaForAllocation(); @@ -11747,9 +11747,6 @@ inline void FeatureSet::SharedDtor() { ABSL_DCHECK(GetArenaForAllocation() == nullptr); _impl_._extensions_.~ExtensionSet(); } -void FeatureSet::SetCachedSize(int size) const { - _impl_._cached_size_.Set(size); -} PROTOBUF_NOINLINE void FeatureSet::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.FeatureSet) @@ -11989,6 +11986,9 @@ PROTOBUF_NOINLINE bool FeatureSet::IsInitialized() const { return true; } +::_pbi::CachedSize* FeatureSet::AccessCachedSize() const { + return &_impl_._cached_size_; +} void FeatureSet::InternalSwap(FeatureSet* other) { using std::swap; _impl_._extensions_.InternalSwap(&other->_impl_._extensions_); @@ -12078,9 +12078,6 @@ inline void FeatureSetDefaults_FeatureSetEditionDefault::SharedDtor() { _impl_.edition_.Destroy(); if (this != internal_default_instance()) delete _impl_.features_; } -void FeatureSetDefaults_FeatureSetEditionDefault::SetCachedSize(int size) const { - _impl_._cached_size_.Set(size); -} PROTOBUF_NOINLINE void FeatureSetDefaults_FeatureSetEditionDefault::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.FeatureSetDefaults.FeatureSetEditionDefault) @@ -12249,6 +12246,9 @@ PROTOBUF_NOINLINE bool FeatureSetDefaults_FeatureSetEditionDefault::IsInitialize return true; } +::_pbi::CachedSize* FeatureSetDefaults_FeatureSetEditionDefault::AccessCachedSize() const { + return &_impl_._cached_size_; +} void FeatureSetDefaults_FeatureSetEditionDefault::InternalSwap(FeatureSetDefaults_FeatureSetEditionDefault* other) { using std::swap; auto* lhs_arena = GetArenaForAllocation(); @@ -12343,9 +12343,6 @@ inline void FeatureSetDefaults::SharedDtor() { _impl_.minimum_edition_.Destroy(); _impl_.maximum_edition_.Destroy(); } -void FeatureSetDefaults::SetCachedSize(int size) const { - _impl_._cached_size_.Set(size); -} PROTOBUF_NOINLINE void FeatureSetDefaults::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.FeatureSetDefaults) @@ -12536,6 +12533,9 @@ PROTOBUF_NOINLINE bool FeatureSetDefaults::IsInitialized() const { return true; } +::_pbi::CachedSize* FeatureSetDefaults::AccessCachedSize() const { + return &_impl_._cached_size_; +} void FeatureSetDefaults::InternalSwap(FeatureSetDefaults* other) { using std::swap; auto* lhs_arena = GetArenaForAllocation(); @@ -12642,9 +12642,6 @@ inline void SourceCodeInfo_Location::SharedDtor() { _impl_.leading_comments_.Destroy(); _impl_.trailing_comments_.Destroy(); } -void SourceCodeInfo_Location::SetCachedSize(int size) const { - _impl_._cached_size_.Set(size); -} PROTOBUF_NOINLINE void SourceCodeInfo_Location::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.SourceCodeInfo.Location) @@ -12895,6 +12892,9 @@ PROTOBUF_NOINLINE bool SourceCodeInfo_Location::IsInitialized() const { return true; } +::_pbi::CachedSize* SourceCodeInfo_Location::AccessCachedSize() const { + return &_impl_._cached_size_; +} void SourceCodeInfo_Location::InternalSwap(SourceCodeInfo_Location* other) { using std::swap; auto* lhs_arena = GetArenaForAllocation(); @@ -12954,9 +12954,6 @@ inline void SourceCodeInfo::SharedDtor() { ABSL_DCHECK(GetArenaForAllocation() == nullptr); _impl_.location_.~RepeatedPtrField(); } -void SourceCodeInfo::SetCachedSize(int size) const { - _impl_._cached_size_.Set(size); -} PROTOBUF_NOINLINE void SourceCodeInfo::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.SourceCodeInfo) @@ -13077,6 +13074,9 @@ PROTOBUF_NOINLINE bool SourceCodeInfo::IsInitialized() const { return true; } +::_pbi::CachedSize* SourceCodeInfo::AccessCachedSize() const { + return &_impl_._cached_size_; +} void SourceCodeInfo::InternalSwap(SourceCodeInfo* other) { using std::swap; _internal_metadata_.InternalSwap(&other->_internal_metadata_); @@ -13169,9 +13169,6 @@ inline void GeneratedCodeInfo_Annotation::SharedDtor() { _impl_.path_.~RepeatedField(); _impl_.source_file_.Destroy(); } -void GeneratedCodeInfo_Annotation::SetCachedSize(int size) const { - _impl_._cached_size_.Set(size); -} PROTOBUF_NOINLINE void GeneratedCodeInfo_Annotation::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.GeneratedCodeInfo.Annotation) @@ -13412,6 +13409,9 @@ PROTOBUF_NOINLINE bool GeneratedCodeInfo_Annotation::IsInitialized() const { return true; } +::_pbi::CachedSize* GeneratedCodeInfo_Annotation::AccessCachedSize() const { + return &_impl_._cached_size_; +} void GeneratedCodeInfo_Annotation::InternalSwap(GeneratedCodeInfo_Annotation* other) { using std::swap; auto* lhs_arena = GetArenaForAllocation(); @@ -13473,9 +13473,6 @@ inline void GeneratedCodeInfo::SharedDtor() { ABSL_DCHECK(GetArenaForAllocation() == nullptr); _impl_.annotation_.~RepeatedPtrField(); } -void GeneratedCodeInfo::SetCachedSize(int size) const { - _impl_._cached_size_.Set(size); -} PROTOBUF_NOINLINE void GeneratedCodeInfo::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.GeneratedCodeInfo) @@ -13596,6 +13593,9 @@ PROTOBUF_NOINLINE bool GeneratedCodeInfo::IsInitialized() const { return true; } +::_pbi::CachedSize* GeneratedCodeInfo::AccessCachedSize() const { + return &_impl_._cached_size_; +} void GeneratedCodeInfo::InternalSwap(GeneratedCodeInfo* other) { using std::swap; _internal_metadata_.InternalSwap(&other->_internal_metadata_); diff --git a/src/google/protobuf/descriptor.pb.h b/src/google/protobuf/descriptor.pb.h index f0fc9c04d7..65e54e6da9 100644 --- a/src/google/protobuf/descriptor.pb.h +++ b/src/google/protobuf/descriptor.pb.h @@ -747,12 +747,12 @@ class PROTOBUF_EXPORT UninterpretedOption_NamePart final : const char* _InternalParse(const char* ptr, ::google::protobuf::internal::ParseContext* ctx) final; ::uint8_t* _InternalSerialize( ::uint8_t* target, ::google::protobuf::io::EpsCopyOutputStream* stream) const final; - int GetCachedSize() const final { return _impl_._cached_size_.Get(); } + int GetCachedSize() const { return _impl_._cached_size_.Get(); } private: + ::google::protobuf::internal::CachedSize* AccessCachedSize() const final; void SharedCtor(::google::protobuf::Arena* arena); void SharedDtor(); - void SetCachedSize(int size) const final; void InternalSwap(UninterpretedOption_NamePart* other); private: @@ -935,12 +935,12 @@ class PROTOBUF_EXPORT SourceCodeInfo_Location final : const char* _InternalParse(const char* ptr, ::google::protobuf::internal::ParseContext* ctx) final; ::uint8_t* _InternalSerialize( ::uint8_t* target, ::google::protobuf::io::EpsCopyOutputStream* stream) const final; - int GetCachedSize() const final { return _impl_._cached_size_.Get(); } + int GetCachedSize() const { return _impl_._cached_size_.Get(); } private: + ::google::protobuf::internal::CachedSize* AccessCachedSize() const final; void SharedCtor(::google::protobuf::Arena* arena); void SharedDtor(); - void SetCachedSize(int size) const final; void InternalSwap(SourceCodeInfo_Location* other); private: @@ -1201,12 +1201,12 @@ class PROTOBUF_EXPORT GeneratedCodeInfo_Annotation final : const char* _InternalParse(const char* ptr, ::google::protobuf::internal::ParseContext* ctx) final; ::uint8_t* _InternalSerialize( ::uint8_t* target, ::google::protobuf::io::EpsCopyOutputStream* stream) const final; - int GetCachedSize() const final { return _impl_._cached_size_.Get(); } + int GetCachedSize() const { return _impl_._cached_size_.Get(); } private: + ::google::protobuf::internal::CachedSize* AccessCachedSize() const final; void SharedCtor(::google::protobuf::Arena* arena); void SharedDtor(); - void SetCachedSize(int size) const final; void InternalSwap(GeneratedCodeInfo_Annotation* other); private: @@ -1457,12 +1457,12 @@ class PROTOBUF_EXPORT FieldOptions_EditionDefault final : const char* _InternalParse(const char* ptr, ::google::protobuf::internal::ParseContext* ctx) final; ::uint8_t* _InternalSerialize( ::uint8_t* target, ::google::protobuf::io::EpsCopyOutputStream* stream) const final; - int GetCachedSize() const final { return _impl_._cached_size_.Get(); } + int GetCachedSize() const { return _impl_._cached_size_.Get(); } private: + ::google::protobuf::internal::CachedSize* AccessCachedSize() const final; void SharedCtor(::google::protobuf::Arena* arena); void SharedDtor(); - void SetCachedSize(int size) const final; void InternalSwap(FieldOptions_EditionDefault* other); private: @@ -1651,12 +1651,12 @@ class PROTOBUF_EXPORT FeatureSet final : const char* _InternalParse(const char* ptr, ::google::protobuf::internal::ParseContext* ctx) final; ::uint8_t* _InternalSerialize( ::uint8_t* target, ::google::protobuf::io::EpsCopyOutputStream* stream) const final; - int GetCachedSize() const final { return _impl_._cached_size_.Get(); } + int GetCachedSize() const { return _impl_._cached_size_.Get(); } private: + ::google::protobuf::internal::CachedSize* AccessCachedSize() const final; void SharedCtor(::google::protobuf::Arena* arena); void SharedDtor(); - void SetCachedSize(int size) const final; void InternalSwap(FeatureSet* other); private: @@ -2131,12 +2131,12 @@ class PROTOBUF_EXPORT ExtensionRangeOptions_Declaration final : const char* _InternalParse(const char* ptr, ::google::protobuf::internal::ParseContext* ctx) final; ::uint8_t* _InternalSerialize( ::uint8_t* target, ::google::protobuf::io::EpsCopyOutputStream* stream) const final; - int GetCachedSize() const final { return _impl_._cached_size_.Get(); } + int GetCachedSize() const { return _impl_._cached_size_.Get(); } private: + ::google::protobuf::internal::CachedSize* AccessCachedSize() const final; void SharedCtor(::google::protobuf::Arena* arena); void SharedDtor(); - void SetCachedSize(int size) const final; void InternalSwap(ExtensionRangeOptions_Declaration* other); private: @@ -2364,12 +2364,12 @@ class PROTOBUF_EXPORT EnumDescriptorProto_EnumReservedRange final : const char* _InternalParse(const char* ptr, ::google::protobuf::internal::ParseContext* ctx) final; ::uint8_t* _InternalSerialize( ::uint8_t* target, ::google::protobuf::io::EpsCopyOutputStream* stream) const final; - int GetCachedSize() const final { return _impl_._cached_size_.Get(); } + int GetCachedSize() const { return _impl_._cached_size_.Get(); } private: + ::google::protobuf::internal::CachedSize* AccessCachedSize() const final; void SharedCtor(::google::protobuf::Arena* arena); void SharedDtor(); - void SetCachedSize(int size) const final; void InternalSwap(EnumDescriptorProto_EnumReservedRange* other); private: @@ -2546,12 +2546,12 @@ class PROTOBUF_EXPORT DescriptorProto_ReservedRange final : const char* _InternalParse(const char* ptr, ::google::protobuf::internal::ParseContext* ctx) final; ::uint8_t* _InternalSerialize( ::uint8_t* target, ::google::protobuf::io::EpsCopyOutputStream* stream) const final; - int GetCachedSize() const final { return _impl_._cached_size_.Get(); } + int GetCachedSize() const { return _impl_._cached_size_.Get(); } private: + ::google::protobuf::internal::CachedSize* AccessCachedSize() const final; void SharedCtor(::google::protobuf::Arena* arena); void SharedDtor(); - void SetCachedSize(int size) const final; void InternalSwap(DescriptorProto_ReservedRange* other); private: @@ -2728,12 +2728,12 @@ class PROTOBUF_EXPORT UninterpretedOption final : const char* _InternalParse(const char* ptr, ::google::protobuf::internal::ParseContext* ctx) final; ::uint8_t* _InternalSerialize( ::uint8_t* target, ::google::protobuf::io::EpsCopyOutputStream* stream) const final; - int GetCachedSize() const final { return _impl_._cached_size_.Get(); } + int GetCachedSize() const { return _impl_._cached_size_.Get(); } private: + ::google::protobuf::internal::CachedSize* AccessCachedSize() const final; void SharedCtor(::google::protobuf::Arena* arena); void SharedDtor(); - void SetCachedSize(int size) const final; void InternalSwap(UninterpretedOption* other); private: @@ -3002,12 +3002,12 @@ class PROTOBUF_EXPORT SourceCodeInfo final : const char* _InternalParse(const char* ptr, ::google::protobuf::internal::ParseContext* ctx) final; ::uint8_t* _InternalSerialize( ::uint8_t* target, ::google::protobuf::io::EpsCopyOutputStream* stream) const final; - int GetCachedSize() const final { return _impl_._cached_size_.Get(); } + int GetCachedSize() const { return _impl_._cached_size_.Get(); } private: + ::google::protobuf::internal::CachedSize* AccessCachedSize() const final; void SharedCtor(::google::protobuf::Arena* arena); void SharedDtor(); - void SetCachedSize(int size) const final; void InternalSwap(SourceCodeInfo* other); private: @@ -3179,12 +3179,12 @@ class PROTOBUF_EXPORT GeneratedCodeInfo final : const char* _InternalParse(const char* ptr, ::google::protobuf::internal::ParseContext* ctx) final; ::uint8_t* _InternalSerialize( ::uint8_t* target, ::google::protobuf::io::EpsCopyOutputStream* stream) const final; - int GetCachedSize() const final { return _impl_._cached_size_.Get(); } + int GetCachedSize() const { return _impl_._cached_size_.Get(); } private: + ::google::protobuf::internal::CachedSize* AccessCachedSize() const final; void SharedCtor(::google::protobuf::Arena* arena); void SharedDtor(); - void SetCachedSize(int size) const final; void InternalSwap(GeneratedCodeInfo* other); private: @@ -3356,12 +3356,12 @@ class PROTOBUF_EXPORT FeatureSetDefaults_FeatureSetEditionDefault final : const char* _InternalParse(const char* ptr, ::google::protobuf::internal::ParseContext* ctx) final; ::uint8_t* _InternalSerialize( ::uint8_t* target, ::google::protobuf::io::EpsCopyOutputStream* stream) const final; - int GetCachedSize() const final { return _impl_._cached_size_.Get(); } + int GetCachedSize() const { return _impl_._cached_size_.Get(); } private: + ::google::protobuf::internal::CachedSize* AccessCachedSize() const final; void SharedCtor(::google::protobuf::Arena* arena); void SharedDtor(); - void SetCachedSize(int size) const final; void InternalSwap(FeatureSetDefaults_FeatureSetEditionDefault* other); private: @@ -3548,12 +3548,12 @@ class PROTOBUF_EXPORT ServiceOptions final : const char* _InternalParse(const char* ptr, ::google::protobuf::internal::ParseContext* ctx) final; ::uint8_t* _InternalSerialize( ::uint8_t* target, ::google::protobuf::io::EpsCopyOutputStream* stream) const final; - int GetCachedSize() const final { return _impl_._cached_size_.Get(); } + int GetCachedSize() const { return _impl_._cached_size_.Get(); } private: + ::google::protobuf::internal::CachedSize* AccessCachedSize() const final; void SharedCtor(::google::protobuf::Arena* arena); void SharedDtor(); - void SetCachedSize(int size) const final; void InternalSwap(ServiceOptions* other); private: @@ -3907,12 +3907,12 @@ class PROTOBUF_EXPORT OneofOptions final : const char* _InternalParse(const char* ptr, ::google::protobuf::internal::ParseContext* ctx) final; ::uint8_t* _InternalSerialize( ::uint8_t* target, ::google::protobuf::io::EpsCopyOutputStream* stream) const final; - int GetCachedSize() const final { return _impl_._cached_size_.Get(); } + int GetCachedSize() const { return _impl_._cached_size_.Get(); } private: + ::google::protobuf::internal::CachedSize* AccessCachedSize() const final; void SharedCtor(::google::protobuf::Arena* arena); void SharedDtor(); - void SetCachedSize(int size) const final; void InternalSwap(OneofOptions* other); private: @@ -4253,12 +4253,12 @@ class PROTOBUF_EXPORT MethodOptions final : const char* _InternalParse(const char* ptr, ::google::protobuf::internal::ParseContext* ctx) final; ::uint8_t* _InternalSerialize( ::uint8_t* target, ::google::protobuf::io::EpsCopyOutputStream* stream) const final; - int GetCachedSize() const final { return _impl_._cached_size_.Get(); } + int GetCachedSize() const { return _impl_._cached_size_.Get(); } private: + ::google::protobuf::internal::CachedSize* AccessCachedSize() const final; void SharedCtor(::google::protobuf::Arena* arena); void SharedDtor(); - void SetCachedSize(int size) const final; void InternalSwap(MethodOptions* other); private: @@ -4646,12 +4646,12 @@ class PROTOBUF_EXPORT MessageOptions final : const char* _InternalParse(const char* ptr, ::google::protobuf::internal::ParseContext* ctx) final; ::uint8_t* _InternalSerialize( ::uint8_t* target, ::google::protobuf::io::EpsCopyOutputStream* stream) const final; - int GetCachedSize() const final { return _impl_._cached_size_.Get(); } + int GetCachedSize() const { return _impl_._cached_size_.Get(); } private: + ::google::protobuf::internal::CachedSize* AccessCachedSize() const final; void SharedCtor(::google::protobuf::Arena* arena); void SharedDtor(); - void SetCachedSize(int size) const final; void InternalSwap(MessageOptions* other); private: @@ -5057,12 +5057,12 @@ class PROTOBUF_EXPORT FileOptions final : const char* _InternalParse(const char* ptr, ::google::protobuf::internal::ParseContext* ctx) final; ::uint8_t* _InternalSerialize( ::uint8_t* target, ::google::protobuf::io::EpsCopyOutputStream* stream) const final; - int GetCachedSize() const final { return _impl_._cached_size_.Get(); } + int GetCachedSize() const { return _impl_._cached_size_.Get(); } private: + ::google::protobuf::internal::CachedSize* AccessCachedSize() const final; void SharedCtor(::google::protobuf::Arena* arena); void SharedDtor(); - void SetCachedSize(int size) const final; void InternalSwap(FileOptions* other); private: @@ -5744,12 +5744,12 @@ class PROTOBUF_EXPORT FieldOptions final : const char* _InternalParse(const char* ptr, ::google::protobuf::internal::ParseContext* ctx) final; ::uint8_t* _InternalSerialize( ::uint8_t* target, ::google::protobuf::io::EpsCopyOutputStream* stream) const final; - int GetCachedSize() const final { return _impl_._cached_size_.Get(); } + int GetCachedSize() const { return _impl_._cached_size_.Get(); } private: + ::google::protobuf::internal::CachedSize* AccessCachedSize() const final; void SharedCtor(::google::protobuf::Arena* arena); void SharedDtor(); - void SetCachedSize(int size) const final; void InternalSwap(FieldOptions* other); private: @@ -6341,12 +6341,12 @@ class PROTOBUF_EXPORT FeatureSetDefaults final : const char* _InternalParse(const char* ptr, ::google::protobuf::internal::ParseContext* ctx) final; ::uint8_t* _InternalSerialize( ::uint8_t* target, ::google::protobuf::io::EpsCopyOutputStream* stream) const final; - int GetCachedSize() const final { return _impl_._cached_size_.Get(); } + int GetCachedSize() const { return _impl_._cached_size_.Get(); } private: + ::google::protobuf::internal::CachedSize* AccessCachedSize() const final; void SharedCtor(::google::protobuf::Arena* arena); void SharedDtor(); - void SetCachedSize(int size) const final; void InternalSwap(FeatureSetDefaults* other); private: @@ -6557,12 +6557,12 @@ class PROTOBUF_EXPORT ExtensionRangeOptions final : const char* _InternalParse(const char* ptr, ::google::protobuf::internal::ParseContext* ctx) final; ::uint8_t* _InternalSerialize( ::uint8_t* target, ::google::protobuf::io::EpsCopyOutputStream* stream) const final; - int GetCachedSize() const final { return _impl_._cached_size_.Get(); } + int GetCachedSize() const { return _impl_._cached_size_.Get(); } private: + ::google::protobuf::internal::CachedSize* AccessCachedSize() const final; void SharedCtor(::google::protobuf::Arena* arena); void SharedDtor(); - void SetCachedSize(int size) const final; void InternalSwap(ExtensionRangeOptions* other); private: @@ -6958,12 +6958,12 @@ class PROTOBUF_EXPORT EnumValueOptions final : const char* _InternalParse(const char* ptr, ::google::protobuf::internal::ParseContext* ctx) final; ::uint8_t* _InternalSerialize( ::uint8_t* target, ::google::protobuf::io::EpsCopyOutputStream* stream) const final; - int GetCachedSize() const final { return _impl_._cached_size_.Get(); } + int GetCachedSize() const { return _impl_._cached_size_.Get(); } private: + ::google::protobuf::internal::CachedSize* AccessCachedSize() const final; void SharedCtor(::google::protobuf::Arena* arena); void SharedDtor(); - void SetCachedSize(int size) const final; void InternalSwap(EnumValueOptions* other); private: @@ -7330,12 +7330,12 @@ class PROTOBUF_EXPORT EnumOptions final : const char* _InternalParse(const char* ptr, ::google::protobuf::internal::ParseContext* ctx) final; ::uint8_t* _InternalSerialize( ::uint8_t* target, ::google::protobuf::io::EpsCopyOutputStream* stream) const final; - int GetCachedSize() const final { return _impl_._cached_size_.Get(); } + int GetCachedSize() const { return _impl_._cached_size_.Get(); } private: + ::google::protobuf::internal::CachedSize* AccessCachedSize() const final; void SharedCtor(::google::protobuf::Arena* arena); void SharedDtor(); - void SetCachedSize(int size) const final; void InternalSwap(EnumOptions* other); private: @@ -7715,12 +7715,12 @@ class PROTOBUF_EXPORT OneofDescriptorProto final : const char* _InternalParse(const char* ptr, ::google::protobuf::internal::ParseContext* ctx) final; ::uint8_t* _InternalSerialize( ::uint8_t* target, ::google::protobuf::io::EpsCopyOutputStream* stream) const final; - int GetCachedSize() const final { return _impl_._cached_size_.Get(); } + int GetCachedSize() const { return _impl_._cached_size_.Get(); } private: + ::google::protobuf::internal::CachedSize* AccessCachedSize() const final; void SharedCtor(::google::protobuf::Arena* arena); void SharedDtor(); - void SetCachedSize(int size) const final; void InternalSwap(OneofDescriptorProto* other); private: @@ -7907,12 +7907,12 @@ class PROTOBUF_EXPORT MethodDescriptorProto final : const char* _InternalParse(const char* ptr, ::google::protobuf::internal::ParseContext* ctx) final; ::uint8_t* _InternalSerialize( ::uint8_t* target, ::google::protobuf::io::EpsCopyOutputStream* stream) const final; - int GetCachedSize() const final { return _impl_._cached_size_.Get(); } + int GetCachedSize() const { return _impl_._cached_size_.Get(); } private: + ::google::protobuf::internal::CachedSize* AccessCachedSize() const final; void SharedCtor(::google::protobuf::Arena* arena); void SharedDtor(); - void SetCachedSize(int size) const final; void InternalSwap(MethodDescriptorProto* other); private: @@ -8163,12 +8163,12 @@ class PROTOBUF_EXPORT FieldDescriptorProto final : const char* _InternalParse(const char* ptr, ::google::protobuf::internal::ParseContext* ctx) final; ::uint8_t* _InternalSerialize( ::uint8_t* target, ::google::protobuf::io::EpsCopyOutputStream* stream) const final; - int GetCachedSize() const final { return _impl_._cached_size_.Get(); } + int GetCachedSize() const { return _impl_._cached_size_.Get(); } private: + ::google::protobuf::internal::CachedSize* AccessCachedSize() const final; void SharedCtor(::google::protobuf::Arena* arena); void SharedDtor(); - void SetCachedSize(int size) const final; void InternalSwap(FieldDescriptorProto* other); private: @@ -8553,12 +8553,12 @@ class PROTOBUF_EXPORT EnumValueDescriptorProto final : const char* _InternalParse(const char* ptr, ::google::protobuf::internal::ParseContext* ctx) final; ::uint8_t* _InternalSerialize( ::uint8_t* target, ::google::protobuf::io::EpsCopyOutputStream* stream) const final; - int GetCachedSize() const final { return _impl_._cached_size_.Get(); } + int GetCachedSize() const { return _impl_._cached_size_.Get(); } private: + ::google::protobuf::internal::CachedSize* AccessCachedSize() const final; void SharedCtor(::google::protobuf::Arena* arena); void SharedDtor(); - void SetCachedSize(int size) const final; void InternalSwap(EnumValueDescriptorProto* other); private: @@ -8758,12 +8758,12 @@ class PROTOBUF_EXPORT DescriptorProto_ExtensionRange final : const char* _InternalParse(const char* ptr, ::google::protobuf::internal::ParseContext* ctx) final; ::uint8_t* _InternalSerialize( ::uint8_t* target, ::google::protobuf::io::EpsCopyOutputStream* stream) const final; - int GetCachedSize() const final { return _impl_._cached_size_.Get(); } + int GetCachedSize() const { return _impl_._cached_size_.Get(); } private: + ::google::protobuf::internal::CachedSize* AccessCachedSize() const final; void SharedCtor(::google::protobuf::Arena* arena); void SharedDtor(); - void SetCachedSize(int size) const final; void InternalSwap(DescriptorProto_ExtensionRange* other); private: @@ -8957,12 +8957,12 @@ class PROTOBUF_EXPORT ServiceDescriptorProto final : const char* _InternalParse(const char* ptr, ::google::protobuf::internal::ParseContext* ctx) final; ::uint8_t* _InternalSerialize( ::uint8_t* target, ::google::protobuf::io::EpsCopyOutputStream* stream) const final; - int GetCachedSize() const final { return _impl_._cached_size_.Get(); } + int GetCachedSize() const { return _impl_._cached_size_.Get(); } private: + ::google::protobuf::internal::CachedSize* AccessCachedSize() const final; void SharedCtor(::google::protobuf::Arena* arena); void SharedDtor(); - void SetCachedSize(int size) const final; void InternalSwap(ServiceDescriptorProto* other); private: @@ -9169,12 +9169,12 @@ class PROTOBUF_EXPORT EnumDescriptorProto final : const char* _InternalParse(const char* ptr, ::google::protobuf::internal::ParseContext* ctx) final; ::uint8_t* _InternalSerialize( ::uint8_t* target, ::google::protobuf::io::EpsCopyOutputStream* stream) const final; - int GetCachedSize() const final { return _impl_._cached_size_.Get(); } + int GetCachedSize() const { return _impl_._cached_size_.Get(); } private: + ::google::protobuf::internal::CachedSize* AccessCachedSize() const final; void SharedCtor(::google::protobuf::Arena* arena); void SharedDtor(); - void SetCachedSize(int size) const final; void InternalSwap(EnumDescriptorProto* other); private: @@ -9433,12 +9433,12 @@ class PROTOBUF_EXPORT DescriptorProto final : const char* _InternalParse(const char* ptr, ::google::protobuf::internal::ParseContext* ctx) final; ::uint8_t* _InternalSerialize( ::uint8_t* target, ::google::protobuf::io::EpsCopyOutputStream* stream) const final; - int GetCachedSize() const final { return _impl_._cached_size_.Get(); } + int GetCachedSize() const { return _impl_._cached_size_.Get(); } private: + ::google::protobuf::internal::CachedSize* AccessCachedSize() const final; void SharedCtor(::google::protobuf::Arena* arena); void SharedDtor(); - void SetCachedSize(int size) const final; void InternalSwap(DescriptorProto* other); private: @@ -9798,12 +9798,12 @@ class PROTOBUF_EXPORT FileDescriptorProto final : const char* _InternalParse(const char* ptr, ::google::protobuf::internal::ParseContext* ctx) final; ::uint8_t* _InternalSerialize( ::uint8_t* target, ::google::protobuf::io::EpsCopyOutputStream* stream) const final; - int GetCachedSize() const final { return _impl_._cached_size_.Get(); } + int GetCachedSize() const { return _impl_._cached_size_.Get(); } private: + ::google::protobuf::internal::CachedSize* AccessCachedSize() const final; void SharedCtor(::google::protobuf::Arena* arena); void SharedDtor(); - void SetCachedSize(int size) const final; void InternalSwap(FileDescriptorProto* other); private: @@ -10214,12 +10214,12 @@ class PROTOBUF_EXPORT FileDescriptorSet final : const char* _InternalParse(const char* ptr, ::google::protobuf::internal::ParseContext* ctx) final; ::uint8_t* _InternalSerialize( ::uint8_t* target, ::google::protobuf::io::EpsCopyOutputStream* stream) const final; - int GetCachedSize() const final { return _impl_._cached_size_.Get(); } + int GetCachedSize() const { return _impl_._cached_size_.Get(); } private: + ::google::protobuf::internal::CachedSize* AccessCachedSize() const final; void SharedCtor(::google::protobuf::Arena* arena); void SharedDtor(); - void SetCachedSize(int size) const final; void InternalSwap(FileDescriptorSet* other); private: diff --git a/src/google/protobuf/dynamic_message.cc b/src/google/protobuf/dynamic_message.cc index 15ba89876f..3edd940963 100644 --- a/src/google/protobuf/dynamic_message.cc +++ b/src/google/protobuf/dynamic_message.cc @@ -227,8 +227,9 @@ class DynamicMessage final : public Message { Message* New(Arena* arena) const override; - int GetCachedSize() const override; - void SetCachedSize(int size) const override; + internal::CachedSize* AccessCachedSize() const final { + return &cached_byte_size_; + } Metadata GetMetadata() const override; @@ -270,7 +271,7 @@ class DynamicMessage final : public Message { void* MutableOneofFieldRaw(const FieldDescriptor* f); const DynamicMessageFactory::TypeInfo* type_info_; - mutable std::atomic cached_byte_size_; + mutable internal::CachedSize cached_byte_size_; }; struct DynamicMessageFactory::TypeInfo { @@ -613,14 +614,6 @@ Message* DynamicMessage::New(Arena* arena) const { } } -int DynamicMessage::GetCachedSize() const { - return cached_byte_size_.load(std::memory_order_relaxed); -} - -void DynamicMessage::SetCachedSize(int size) const { - cached_byte_size_.store(size, std::memory_order_relaxed); -} - Metadata DynamicMessage::GetMetadata() const { Metadata metadata; metadata.descriptor = type_info_->type; diff --git a/src/google/protobuf/generated_message_bases.h b/src/google/protobuf/generated_message_bases.h index 23798cc4c7..24d94ae2d0 100644 --- a/src/google/protobuf/generated_message_bases.h +++ b/src/google/protobuf/generated_message_bases.h @@ -55,21 +55,23 @@ class PROTOBUF_EXPORT ZeroFieldsBase : public Message { PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; bool IsInitialized() const final { return true; } size_t ByteSizeLong() const final; - int GetCachedSize() const final { return _cached_size_.Get(); } + int GetCachedSize() const { return _cached_size_.Get(); } const char* _InternalParse(const char* ptr, internal::ParseContext* ctx) final; ::uint8_t* _InternalSerialize(::uint8_t* target, io::EpsCopyOutputStream* stream) const final; protected: + internal::CachedSize* AccessCachedSize() const final { + return &_cached_size_; + } + constexpr ZeroFieldsBase() {} explicit ZeroFieldsBase(Arena* arena) : Message(arena) {} ZeroFieldsBase(const ZeroFieldsBase&) = delete; ZeroFieldsBase& operator=(const ZeroFieldsBase&) = delete; ~ZeroFieldsBase() override; - void SetCachedSize(int size) const final { _cached_size_.Set(size); } - static void MergeImpl(Message& to, const Message& from); static void CopyImpl(Message& to, const Message& from); void InternalSwap(ZeroFieldsBase* other); diff --git a/src/google/protobuf/generated_message_util.h b/src/google/protobuf/generated_message_util.h index 8e7f48a924..01fad5fb46 100644 --- a/src/google/protobuf/generated_message_util.h +++ b/src/google/protobuf/generated_message_util.h @@ -192,62 +192,6 @@ T* GetOwnedMessage(Arena* message_arena, T* submessage, submessage_arena)); } -// Hide atomic from the public header and allow easy change to regular int -// on platforms where the atomic might have a perf impact. -// -// CachedSize is like std::atomic but with some important changes: -// -// 1) CachedSize uses Get / Set rather than load / store. -// 2) CachedSize always uses relaxed ordering. -// 3) CachedSize is assignable and copy-constructible. -// 4) CachedSize has a constexpr default constructor, and a constexpr -// constructor that takes an int argument. -// 5) If the compiler supports the __atomic_load_n / __atomic_store_n builtins, -// then CachedSize is trivially copyable. -// -// Developed at https://godbolt.org/z/vYcx7zYs1 ; supports gcc, clang, MSVC. -class PROTOBUF_EXPORT CachedSize { - private: - using Scalar = int; - - public: - constexpr CachedSize() noexcept : atom_(Scalar{}) {} - // NOLINTNEXTLINE(google-explicit-constructor) - constexpr CachedSize(Scalar desired) noexcept : atom_(desired) {} -#if PROTOBUF_BUILTIN_ATOMIC - constexpr CachedSize(const CachedSize& other) = default; - - Scalar Get() const noexcept { - return __atomic_load_n(&atom_, __ATOMIC_RELAXED); - } - - void Set(Scalar desired) noexcept { - __atomic_store_n(&atom_, desired, __ATOMIC_RELAXED); - } -#else - CachedSize(const CachedSize& other) noexcept : atom_(other.Get()) {} - CachedSize& operator=(const CachedSize& other) noexcept { - Set(other.Get()); - return *this; - } - - Scalar Get() const noexcept { // - return atom_.load(std::memory_order_relaxed); - } - - void Set(Scalar desired) noexcept { - atom_.store(desired, std::memory_order_relaxed); - } -#endif - - private: -#if PROTOBUF_BUILTIN_ATOMIC - Scalar atom_; -#else - std::atomic atom_; -#endif -}; - PROTOBUF_EXPORT void DestroyMessage(const void* message); PROTOBUF_EXPORT void DestroyString(const void* s); // Destroy (not delete) the message diff --git a/src/google/protobuf/implicit_weak_message.h b/src/google/protobuf/implicit_weak_message.h index b9ff1fa28d..a17cbd43c2 100644 --- a/src/google/protobuf/implicit_weak_message.h +++ b/src/google/protobuf/implicit_weak_message.h @@ -112,10 +112,6 @@ class PROTOBUF_EXPORT ImplicitWeakMessage : public MessageLite { target); } - int GetCachedSize() const override { - return data_ == nullptr ? 0 : static_cast(data_->size()); - } - typedef void InternalArenaConstructable_; private: diff --git a/src/google/protobuf/map_entry.h b/src/google/protobuf/map_entry.h index a51bc4ecf1..62903aae52 100644 --- a/src/google/protobuf/map_entry.h +++ b/src/google/protobuf/map_entry.h @@ -226,8 +226,6 @@ class MapEntry : public Message { return ValueTypeHandler::Write(kValueFieldNumber, value(), ptr, stream); } - int GetCachedSize() const override { return ByteSizeLong(); } - bool IsInitialized() const override { return ValueTypeHandler::IsInitialized(value_); } diff --git a/src/google/protobuf/message.cc b/src/google/protobuf/message.cc index 94cad748dd..aeded53cd1 100644 --- a/src/google/protobuf/message.cc +++ b/src/google/protobuf/message.cc @@ -179,14 +179,15 @@ uint8_t* Message::_InternalSerialize(uint8_t* target, size_t Message::ByteSizeLong() const { size_t size = WireFormat::ByteSize(*this); - SetCachedSize(internal::ToCachedSize(size)); - return size; -} -void Message::SetCachedSize(int /* size */) const { - ABSL_LOG(FATAL) << "Message class \"" << GetDescriptor()->full_name() - << "\" implements neither SetCachedSize() nor ByteSize(). " - "Must implement one or the other."; + auto* cached_size = AccessCachedSize(); + ABSL_CHECK(cached_size != nullptr) + << "Message class \"" << GetDescriptor()->full_name() + << "\" implements neither AccessCachedSize() nor ByteSizeLong(). " + "Must implement one or the other."; + cached_size->Set(internal::ToCachedSize(size)); + + return size; } size_t Message::ComputeUnknownFieldsSize( diff --git a/src/google/protobuf/message.h b/src/google/protobuf/message.h index d671cfcf93..df4ef42571 100644 --- a/src/google/protobuf/message.h +++ b/src/google/protobuf/message.h @@ -366,17 +366,6 @@ class PROTOBUF_EXPORT Message : public MessageLite { uint8_t* _InternalSerialize(uint8_t* target, io::EpsCopyOutputStream* stream) const override; - private: - // This is called only by the default implementation of ByteSize(), to - // update the cached size. If you override ByteSize(), you do not need - // to override this. If you do not override ByteSize(), you MUST override - // this; the default implementation will crash. - // - // The method is private because subclasses should never call it; only - // override it. Yes, C++ lets you do that. Crazy, huh? - virtual void SetCachedSize(int size) const; - - public: // Introspection --------------------------------------------------- diff --git a/src/google/protobuf/message_lite.cc b/src/google/protobuf/message_lite.cc index 1ea4d02843..bd871e3ffc 100644 --- a/src/google/protobuf/message_lite.cc +++ b/src/google/protobuf/message_lite.cc @@ -75,6 +75,14 @@ std::string MessageLite::DebugString() const { return absl::StrCat("MessageLite at 0x", absl::Hex(this)); } +int MessageLite::GetCachedSize() const { + auto* cached_size = AccessCachedSize(); + if (PROTOBUF_PREDICT_FALSE(cached_size == nullptr)) return ByteSize(); + return cached_size->Get(); +} + +internal::CachedSize* MessageLite::AccessCachedSize() const { return nullptr; } + namespace { // When serializing, we first compute the byte size, then serialize the message. diff --git a/src/google/protobuf/message_lite.h b/src/google/protobuf/message_lite.h index 4fcb6e6ec4..4dc7fb00ee 100644 --- a/src/google/protobuf/message_lite.h +++ b/src/google/protobuf/message_lite.h @@ -39,6 +39,7 @@ #ifndef GOOGLE_PROTOBUF_MESSAGE_LITE_H__ #define GOOGLE_PROTOBUF_MESSAGE_LITE_H__ +#include #include #include #include @@ -84,6 +85,62 @@ class ZeroCopyOutputStream; } // namespace io namespace internal { +// Allow easy change to regular int on platforms where the atomic might have a +// perf impact. +// +// CachedSize is like std::atomic but with some important changes: +// +// 1) CachedSize uses Get / Set rather than load / store. +// 2) CachedSize always uses relaxed ordering. +// 3) CachedSize is assignable and copy-constructible. +// 4) CachedSize has a constexpr default constructor, and a constexpr +// constructor that takes an int argument. +// 5) If the compiler supports the __atomic_load_n / __atomic_store_n builtins, +// then CachedSize is trivially copyable. +// +// Developed at https://godbolt.org/z/vYcx7zYs1 ; supports gcc, clang, MSVC. +class PROTOBUF_EXPORT CachedSize { + private: + using Scalar = int; + + public: + constexpr CachedSize() noexcept : atom_(Scalar{}) {} + // NOLINTNEXTLINE(google-explicit-constructor) + constexpr CachedSize(Scalar desired) noexcept : atom_(desired) {} +#if PROTOBUF_BUILTIN_ATOMIC + constexpr CachedSize(const CachedSize& other) = default; + + Scalar Get() const noexcept { + return __atomic_load_n(&atom_, __ATOMIC_RELAXED); + } + + void Set(Scalar desired) noexcept { + __atomic_store_n(&atom_, desired, __ATOMIC_RELAXED); + } +#else + CachedSize(const CachedSize& other) noexcept : atom_(other.Get()) {} + CachedSize& operator=(const CachedSize& other) noexcept { + Set(other.Get()); + return *this; + } + + Scalar Get() const noexcept { // + return atom_.load(std::memory_order_relaxed); + } + + void Set(Scalar desired) noexcept { + atom_.store(desired, std::memory_order_relaxed); + } +#endif + + private: +#if PROTOBUF_BUILTIN_ATOMIC + Scalar atom_; +#else + std::atomic atom_; +#endif +}; + class SwapFieldHelper; // See parse_context.h for explanation @@ -451,7 +508,7 @@ class PROTOBUF_EXPORT MessageLite { // sub-message is changed, all of its parents' cached sizes would need to be // invalidated, which is too much work for an otherwise inlined setter // method.) - virtual int GetCachedSize() const = 0; + int GetCachedSize() const; virtual const char* _InternalParse(const char* /*ptr*/, internal::ParseContext* /*ctx*/) { @@ -493,6 +550,10 @@ class PROTOBUF_EXPORT MessageLite { internal::InternalMetadata _internal_metadata_; + // The default implementation means there is no cached size and ByteSize + // should be called instead. + virtual internal::CachedSize* AccessCachedSize() const; + public: enum ParseFlags { kMerge = 0,