Make SpaceUsedLong non-virtual to reduce the size of the vtable.

PiperOrigin-RevId: 576128619
pull/14497/head
Protobuf Team Bot 1 year ago committed by Copybara-Service
parent c68554cc24
commit 4d92106451
  1. 8
      src/google/protobuf/message.cc
  2. 11
      src/google/protobuf/message.h

@ -188,7 +188,13 @@ size_t Message::MaybeComputeUnknownFieldsSize(
} }
size_t Message::SpaceUsedLong() const { size_t Message::SpaceUsedLong() const {
return GetReflection()->SpaceUsedLong(*this); 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<const internal::RawMessageBase&>(*this)
.SpaceUsedLong();
} }
static std::string GetTypeNameImpl(const MessageLite& msg) { static std::string GetTypeNameImpl(const MessageLite& msg) {

@ -286,8 +286,7 @@ class PROTOBUF_EXPORT Message : public MessageLite {
void DiscardUnknownFields(); void DiscardUnknownFields();
// Computes (an estimate of) the total number of bytes currently used for // Computes (an estimate of) the total number of bytes currently used for
// storing the message in memory. The default implementation calls the // storing the message in memory.
// Reflection object's SpaceUsed() method.
// //
// SpaceUsed() is noticeably slower than ByteSize(), as it is implemented // SpaceUsed() is noticeably slower than ByteSize(), as it is implemented
// using reflection (rather than the generated code implementation for // using reflection (rather than the generated code implementation for
@ -297,7 +296,7 @@ class PROTOBUF_EXPORT Message : public MessageLite {
// Note: The precise value of this method should never be depended on, and can // Note: The precise value of this method should never be depended on, and can
// change substantially due to internal details. In debug builds, this will // change substantially due to internal details. In debug builds, this will
// include a random fuzz factor to prevent these dependencies. // include a random fuzz factor to prevent these dependencies.
virtual size_t SpaceUsedLong() const; size_t SpaceUsedLong() const;
[[deprecated("Please use SpaceUsedLong() instead")]] int SpaceUsed() const { [[deprecated("Please use SpaceUsedLong() instead")]] int SpaceUsed() const {
return internal::ToIntSize(SpaceUsedLong()); return internal::ToIntSize(SpaceUsedLong());
@ -1573,6 +1572,12 @@ bool SplitFieldHasExtraIndirectionStatic(const FieldDescriptor* field) {
return ret; return ret;
} }
class RawMessageBase : public Message {
public:
using Message::Message;
virtual size_t SpaceUsedLong() const = 0;
};
} // namespace internal } // namespace internal
template <typename Type> template <typename Type>

Loading…
Cancel
Save