Move `InitializationErrorString` into the secondary vtable, retrieved from `ClassData`.

This removes one virtual function from MessageLite with zero per-type increase.

PiperOrigin-RevId: 574994142
pull/14483/head
Protobuf Team Bot 1 year ago committed by Copybara-Service
parent 2690017b16
commit 1b7615905f
  1. 5
      src/google/protobuf/message.cc
  2. 2
      src/google/protobuf/message.h
  3. 8
      src/google/protobuf/message_lite.cc
  4. 3
      src/google/protobuf/message_lite.h

@ -195,8 +195,13 @@ static std::string GetTypeNameImpl(const MessageLite& msg) {
return DownCast<const Message&>(msg).GetDescriptor()->full_name(); return DownCast<const Message&>(msg).GetDescriptor()->full_name();
} }
static std::string InitializationErrorStringImpl(const MessageLite& msg) {
return DownCast<const Message&>(msg).InitializationErrorString();
}
constexpr MessageLite::DescriptorMethods Message::kDescriptorMethods = { constexpr MessageLite::DescriptorMethods Message::kDescriptorMethods = {
GetTypeNameImpl, GetTypeNameImpl,
InitializationErrorStringImpl,
}; };
namespace internal { namespace internal {

@ -271,7 +271,7 @@ class PROTOBUF_EXPORT Message : public MessageLite {
// Like FindInitializationErrors, but joins all the strings, delimited by // Like FindInitializationErrors, but joins all the strings, delimited by
// commas, and returns them. // commas, and returns them.
std::string InitializationErrorString() const override; std::string InitializationErrorString() const;
// Clears all unknown fields from this message and all embedded messages. // Clears all unknown fields from this message and all embedded messages.
// Normally, if unknown tag numbers are encountered when parsing a message, // Normally, if unknown tag numbers are encountered when parsing a message,

@ -68,6 +68,14 @@ void MessageLite::OnDemandRegisterArenaDtor(Arena* arena) {
} }
std::string MessageLite::InitializationErrorString() const { std::string MessageLite::InitializationErrorString() const {
auto* data = GetClassData();
ABSL_DCHECK(data != nullptr);
if (data->descriptor_methods != nullptr) {
// For !LITE messages, we use the descriptor method function.
return data->descriptor_methods->initialization_error_string(*this);
}
return "(cannot determine missing fields for lite message)"; return "(cannot determine missing fields for lite message)";
} }

@ -235,7 +235,7 @@ class PROTOBUF_EXPORT MessageLite {
// This is not implemented for Lite messages -- it just returns "(cannot // This is not implemented for Lite messages -- it just returns "(cannot
// determine missing fields for lite message)". However, it is implemented // determine missing fields for lite message)". However, it is implemented
// for full messages. See message.h. // for full messages. See message.h.
virtual std::string InitializationErrorString() const; std::string InitializationErrorString() const;
// If |other| is the exact same class as this, calls MergeFrom(). Otherwise, // If |other| is the exact same class as this, calls MergeFrom(). Otherwise,
// results are undefined (probably crash). // results are undefined (probably crash).
@ -523,6 +523,7 @@ class PROTOBUF_EXPORT MessageLite {
// costs in MessageLite. // costs in MessageLite.
struct DescriptorMethods { struct DescriptorMethods {
std::string (*get_type_name)(const MessageLite&); std::string (*get_type_name)(const MessageLite&);
std::string (*initialization_error_string)(const MessageLite&);
}; };
struct ClassData { struct ClassData {
// Note: The order of arguments in the functions is chosen so that it has // Note: The order of arguments in the functions is chosen so that it has

Loading…
Cancel
Save