diff --git a/src/google/protobuf/message.cc b/src/google/protobuf/message.cc index 4a3e3e5c8e..866c214543 100644 --- a/src/google/protobuf/message.cc +++ b/src/google/protobuf/message.cc @@ -185,13 +185,7 @@ size_t Message::MaybeComputeUnknownFieldsSize( } size_t Message::SpaceUsedLong() const { - auto* reflection = GetReflection(); - if (PROTOBUF_PREDICT_TRUE(reflection != nullptr)) { - return reflection->SpaceUsedLong(*this); - } - // The only case that does not have reflection is RawMessage. - return internal::DownCast(*this) - .SpaceUsedLong(); + return GetClassData()->full().descriptor_methods->space_used_long(*this); } static std::string GetTypeNameImpl(const MessageLite& msg) { @@ -207,11 +201,17 @@ const internal::TcParseTableBase* Message::GetTcParseTableImpl( return DownCast(msg).GetReflection()->GetTcParseTable(); } +size_t Message::SpaceUsedLongImpl(const MessageLite& msg_lite) { + auto& msg = DownCast(msg_lite); + return msg.GetReflection()->SpaceUsedLong(msg); +} + PROTOBUF_CONSTINIT const MessageLite::DescriptorMethods Message::kDescriptorMethods = { GetTypeNameImpl, InitializationErrorStringImpl, GetTcParseTableImpl, + SpaceUsedLongImpl, }; namespace internal { diff --git a/src/google/protobuf/message.h b/src/google/protobuf/message.h index 22ab4c28ae..876a57cc08 100644 --- a/src/google/protobuf/message.h +++ b/src/google/protobuf/message.h @@ -386,6 +386,8 @@ class PROTOBUF_EXPORT Message : public MessageLite { static const internal::TcParseTableBase* GetTcParseTableImpl( const MessageLite& msg); + static size_t SpaceUsedLongImpl(const MessageLite& msg_lite); + static const DescriptorMethods kDescriptorMethods; }; @@ -1629,12 +1631,6 @@ bool SplitFieldHasExtraIndirectionStatic(const FieldDescriptor* field) { return SplitFieldHasExtraIndirection(field); } -class RawMessageBase : public Message { - public: - using Message::Message; - virtual size_t SpaceUsedLong() const = 0; -}; - inline void MaybePoisonAfterClear(Message* root) { if (root == nullptr) return; #ifndef PROTOBUF_ASAN diff --git a/src/google/protobuf/message_lite.h b/src/google/protobuf/message_lite.h index fc14e810c5..00a5481909 100644 --- a/src/google/protobuf/message_lite.h +++ b/src/google/protobuf/message_lite.h @@ -537,6 +537,7 @@ class PROTOBUF_EXPORT MessageLite { std::string (*get_type_name)(const MessageLite&); std::string (*initialization_error_string)(const MessageLite&); const internal::TcParseTableBase* (*get_tc_table)(const MessageLite&); + size_t (*space_used_long)(const MessageLite&); }; struct ClassDataFull; // Note: The order of arguments in the functions is chosen so that it has