Inline the stub functions in custom vtable mode.

That way we avoid the extra function call and they behave more like normal
virtual functions.
Has no effect in normal mode.

PiperOrigin-RevId: 654039245
pull/17440/head
Protobuf Team Bot 7 months ago committed by Copybara-Service
parent 5d8f7eed1e
commit 9a0c8f6113
  1. 19
      src/google/protobuf/message_lite.cc
  2. 16
      src/google/protobuf/message_lite.h

@ -64,23 +64,6 @@ void MessageLite::CheckTypeAndMergeFrom(const MessageLite& other) {
data->merge_to_from(*this, other);
}
#if defined(PROTOBUF_CUSTOM_VTABLE)
uint8_t* MessageLite::_InternalSerialize(
uint8_t* ptr, io::EpsCopyOutputStream* stream) const {
return _class_data_->serialize(*this, ptr, stream);
}
MessageLite* MessageLite::New(Arena* arena) const {
return static_cast<MessageLite*>(_class_data_->new_message(this, arena));
}
void MessageLite::Clear() { _class_data_->clear(*this); }
size_t MessageLite::ByteSizeLong() const {
return _class_data_->byte_size_long(*this);
}
#endif // PROTOBUF_CUSTOM_VTABLE
bool MessageLite::IsInitialized() const {
auto* data = GetClassData();
return data->is_initialized != nullptr ? data->is_initialized(*this) : true;
@ -137,7 +120,9 @@ std::string MessageLite::DebugString() const {
return absl::StrCat("MessageLite at 0x", absl::Hex(this));
}
#if !defined(PROTOBUF_CUSTOM_VTABLE)
int MessageLite::GetCachedSize() const { return AccessCachedSize().Get(); }
#endif
namespace {

@ -229,7 +229,9 @@ class PROTOBUF_EXPORT MessageLite {
// Construct a new instance on the arena. Ownership is passed to the caller
// if arena is a nullptr.
#if defined(PROTOBUF_CUSTOM_VTABLE)
MessageLite* New(Arena* arena) const;
MessageLite* New(Arena* arena) const {
return static_cast<MessageLite*>(_class_data_->new_message(this, arena));
}
#else
virtual MessageLite* New(Arena* arena) const = 0;
#endif // PROTOBUF_CUSTOM_VTABLE
@ -246,7 +248,7 @@ class PROTOBUF_EXPORT MessageLite {
// will likely be needed again, so the memory used may not be freed.
// To ensure that all memory used by a Message is freed, you must delete it.
#if defined(PROTOBUF_CUSTOM_VTABLE)
void Clear();
void Clear() { _class_data_->clear(*this); }
#else
virtual void Clear() = 0;
#endif // PROTOBUF_CUSTOM_VTABLE
@ -470,7 +472,7 @@ class PROTOBUF_EXPORT MessageLite {
// ByteSizeLong() is generally linear in the number of fields defined for the
// proto.
#if defined(PROTOBUF_CUSTOM_VTABLE)
size_t ByteSizeLong() const;
size_t ByteSizeLong() const { return _class_data_->byte_size_long(*this); }
#else
virtual size_t ByteSizeLong() const = 0;
#endif // PROTOBUF_CUSTOM_VTABLE
@ -512,7 +514,11 @@ 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.)
#if defined(PROTOBUF_CUSTOM_VTABLE)
int GetCachedSize() const { return AccessCachedSize().Get(); }
#else
int GetCachedSize() const;
#endif
const char* _InternalParse(const char* ptr, internal::ParseContext* ctx);
@ -795,7 +801,9 @@ class PROTOBUF_EXPORT MessageLite {
// uint8_t* _InternalSerialize(uint8_t* ptr) const;
#if defined(PROTOBUF_CUSTOM_VTABLE)
uint8_t* _InternalSerialize(uint8_t* ptr,
io::EpsCopyOutputStream* stream) const;
io::EpsCopyOutputStream* stream) const {
return _class_data_->serialize(*this, ptr, stream);
}
#else // PROTOBUF_CUSTOM_VTABLE
virtual uint8_t* _InternalSerialize(
uint8_t* ptr, io::EpsCopyOutputStream* stream) const = 0;

Loading…
Cancel
Save