In the custom vtable mode use a non-static member pointer for Clear instead of the trampoline function. That way the public entry point and the function pointer both end up being the same. Currently, the static function is a separate function than the one called externally, forcing two functions to exist.

Normal mode keeps the existing member function path.

In the future we might change this to a static member function instead to avoid the bloat of the member pointer, but that currently affects normal mode and we want to avoid it for now.

PiperOrigin-RevId: 672552340
pull/18148/head
Protobuf Team Bot 6 months ago committed by Copybara-Service
parent 3431016e91
commit 4ede7d1faf
  1. 6
      src/google/protobuf/compiler/cpp/message.cc
  2. 2
      src/google/protobuf/dynamic_message.cc
  3. 2
      src/google/protobuf/implicit_weak_message.cc
  4. 4
      src/google/protobuf/implicit_weak_message.h
  5. 4
      src/google/protobuf/message.cc
  6. 2
      src/google/protobuf/message.h
  7. 12
      src/google/protobuf/message_lite.h

@ -3992,8 +3992,10 @@ void MessageGenerator::GenerateClassData(io::Printer* p) {
)cc"); )cc");
} else { } else {
p->Emit(R"cc( p->Emit(R"cc(
$superclass$::ClearImpl, $superclass$::ByteSizeLongImpl, static_cast<void (::$proto_ns$::MessageLite::*)()>(
$superclass$::_InternalSerializeImpl, &$classname$::ClearImpl),
$superclass$::ByteSizeLongImpl, $superclass$::_InternalSerializeImpl
,
)cc"); )cc");
} }
}; };

@ -270,7 +270,7 @@ struct DynamicMessageFactory::TypeInfo {
&DynamicMessage::MergeImpl, &DynamicMessage::MergeImpl,
internal::MessageCreator(), // to be filled later internal::MessageCreator(), // to be filled later
&DynamicMessage::DeleteImpl, &DynamicMessage::DeleteImpl,
DynamicMessage::ClearImpl, static_cast<void (MessageLite::*)()>(&DynamicMessage::ClearImpl),
DynamicMessage::ByteSizeLongImpl, DynamicMessage::ByteSizeLongImpl,
DynamicMessage::_InternalSerializeImpl, DynamicMessage::_InternalSerializeImpl,
PROTOBUF_FIELD_OFFSET(DynamicMessage, cached_byte_size_), PROTOBUF_FIELD_OFFSET(DynamicMessage, cached_byte_size_),

@ -72,7 +72,7 @@ constexpr MessageLite::ClassDataLite<1> ImplicitWeakMessage::class_data_ = {
internal::MessageCreator(NewImpl<ImplicitWeakMessage>, internal::MessageCreator(NewImpl<ImplicitWeakMessage>,
sizeof(ImplicitWeakMessage)), sizeof(ImplicitWeakMessage)),
GetDeleteImpl<ImplicitWeakMessage>(), GetDeleteImpl<ImplicitWeakMessage>(),
&ClearImpl, GetClearImpl<ImplicitWeakMessage>(),
&ByteSizeLongImpl, &ByteSizeLongImpl,
&_InternalSerializeImpl, &_InternalSerializeImpl,
PROTOBUF_FIELD_OFFSET(ImplicitWeakMessage, cached_size_), PROTOBUF_FIELD_OFFSET(ImplicitWeakMessage, cached_size_),

@ -87,10 +87,6 @@ class PROTOBUF_EXPORT ImplicitWeakMessage final : public MessageLite {
static void MergeImpl(MessageLite&, const MessageLite&); static void MergeImpl(MessageLite&, const MessageLite&);
static void ClearImpl(MessageLite& msg) {
static_cast<ImplicitWeakMessage&>(msg).Clear();
}
static size_t ByteSizeLongImpl(const MessageLite& msg) { static size_t ByteSizeLongImpl(const MessageLite& msg) {
return static_cast<const ImplicitWeakMessage&>(msg).ByteSizeLong(); return static_cast<const ImplicitWeakMessage&>(msg).ByteSizeLong();
} }

@ -71,8 +71,8 @@ void Message::MergeImpl(MessageLite& to, const MessageLite& from) {
DownCastMessage<Message>(&to)); DownCastMessage<Message>(&to));
} }
void Message::ClearImpl(MessageLite& msg) { void Message::ClearImpl() {
ReflectionOps::Clear(&DownCastMessage<Message>(msg)); ReflectionOps::Clear(DownCastMessage<Message>(this));
} }
size_t Message::ByteSizeLongImpl(const MessageLite& msg) { size_t Message::ByteSizeLongImpl(const MessageLite& msg) {

@ -390,7 +390,7 @@ class PROTOBUF_EXPORT Message : public MessageLite {
// Reflection based version for reflection based types. // Reflection based version for reflection based types.
static absl::string_view GetTypeNameImpl(const ClassData* data); static absl::string_view GetTypeNameImpl(const ClassData* data);
static void MergeImpl(MessageLite& to, const MessageLite& from); static void MergeImpl(MessageLite& to, const MessageLite& from);
static void ClearImpl(MessageLite& msg); void ClearImpl();
static size_t ByteSizeLongImpl(const MessageLite& msg); static size_t ByteSizeLongImpl(const MessageLite& msg);
static uint8_t* _InternalSerializeImpl(const MessageLite& msg, static uint8_t* _InternalSerializeImpl(const MessageLite& msg,
uint8_t* target, uint8_t* target,

@ -333,7 +333,7 @@ class PROTOBUF_EXPORT MessageLite {
// will likely be needed again, so the memory used may not be freed. // 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. // To ensure that all memory used by a Message is freed, you must delete it.
#if defined(PROTOBUF_CUSTOM_VTABLE) #if defined(PROTOBUF_CUSTOM_VTABLE)
void Clear() { _class_data_->clear(*this); } void Clear() { (this->*_class_data_->clear)(); }
#else #else
virtual void Clear() = 0; virtual void Clear() = 0;
#endif // PROTOBUF_CUSTOM_VTABLE #endif // PROTOBUF_CUSTOM_VTABLE
@ -649,13 +649,9 @@ class PROTOBUF_EXPORT MessageLite {
return DeleteImpl<T>; return DeleteImpl<T>;
} }
template <typename T>
static void ClearImpl(MessageLite& msg) {
return static_cast<T&>(msg).Clear();
}
template <typename T> template <typename T>
static constexpr auto GetClearImpl() { static constexpr auto GetClearImpl() {
return ClearImpl<T>; return static_cast<void (MessageLite::*)()>(&T::Clear);
} }
#else // PROTOBUF_CUSTOM_VTABLE #else // PROTOBUF_CUSTOM_VTABLE
// When custom vtables are off we avoid instantiating the functions because we // When custom vtables are off we avoid instantiating the functions because we
@ -715,7 +711,7 @@ class PROTOBUF_EXPORT MessageLite {
internal::MessageCreator message_creator; internal::MessageCreator message_creator;
#if defined(PROTOBUF_CUSTOM_VTABLE) #if defined(PROTOBUF_CUSTOM_VTABLE)
DeleteMessageF delete_message; DeleteMessageF delete_message;
void (*clear)(MessageLite&); void (MessageLite::*clear)();
size_t (*byte_size_long)(const MessageLite&); size_t (*byte_size_long)(const MessageLite&);
uint8_t* (*serialize)(const MessageLite& msg, uint8_t* ptr, uint8_t* (*serialize)(const MessageLite& msg, uint8_t* ptr,
io::EpsCopyOutputStream* stream); io::EpsCopyOutputStream* stream);
@ -760,7 +756,7 @@ class PROTOBUF_EXPORT MessageLite {
void (*merge_to_from)(MessageLite& to, const MessageLite& from_msg), void (*merge_to_from)(MessageLite& to, const MessageLite& from_msg),
internal::MessageCreator message_creator, // internal::MessageCreator message_creator, //
DeleteMessageF delete_message, // DeleteMessageF delete_message, //
void (*clear)(MessageLite&), void (MessageLite::*clear)(),
size_t (*byte_size_long)(const MessageLite&), size_t (*byte_size_long)(const MessageLite&),
uint8_t* (*serialize)(const MessageLite& msg, uint8_t* ptr, uint8_t* (*serialize)(const MessageLite& msg, uint8_t* ptr,
io::EpsCopyOutputStream* stream), io::EpsCopyOutputStream* stream),

Loading…
Cancel
Save