Skip to content

Commit c11ca08

Browse files
Stop inlining ClassDataLite::type_name[] to make
sizeof(ClassDataFull) == sizeof(ClassDataLite) #ifdef PROTOBUF_MESSAGE_GLOBALS. This allows embedding ClassData(Lite|Full) under a common type (GlobalsBase) to access default instances with known offset. PiperOrigin-RevId: 866629828
1 parent 0e4441f commit c11ca08

File tree

6 files changed

+13
-14
lines changed

6 files changed

+13
-14
lines changed

src/google/protobuf/compiler/cpp/helpers.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -624,8 +624,7 @@ std::string ClassDataType(const Descriptor* descriptor,
624624
// via options.
625625
IsBootstrapProto(options, descriptor->file())
626626
? "ClassDataFull"
627-
: absl::StrFormat("ClassDataLite<%d>",
628-
descriptor->full_name().size() + 1);
627+
: "ClassDataLite";
629628
}
630629

631630
std::string DescriptorTableName(const FileDescriptor* file,

src/google/protobuf/compiler/cpp/message.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4123,14 +4123,13 @@ void MessageGenerator::GenerateClassData(io::Printer* p) {
41234123
} else {
41244124
p->Emit(
41254125
{
4126-
{"type_size", descriptor_->full_name().size() + 1},
41274126
{"is_initialized", is_initialized},
41284127
{"custom_vtable_methods", custom_vtable_methods},
41294128
{"v2_data", emit_v2_data},
41304129
},
41314130
R"cc(
41324131
constexpr auto $classname$::InternalGenerateClassData_() {
4133-
return $pbi$::ClassDataLite<$type_size$>{
4132+
return $pbi$::ClassDataLite{
41344133
{
41354134
$default_instance$,
41364135
&_table_.header,
@@ -4151,7 +4150,7 @@ void MessageGenerator::GenerateClassData(io::Printer* p) {
41514150
41524151
PROTOBUF_CONSTINIT
41534152
PROTOBUF_ATTRIBUTE_INIT_PRIORITY1
4154-
const $pbi$::ClassDataLite<$type_size$> $classname$_class_data_ =
4153+
const $pbi$::ClassDataLite $classname$_class_data_ =
41554154
$classname$::InternalGenerateClassData_();
41564155
41574156
//~ This function needs to be marked as weak to avoid significantly

src/google/protobuf/implicit_weak_message.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ const TcParseTable<0> ImplicitWeakMessage::table_ =
6262
internal::CreateStubTcParseTable<ImplicitWeakMessage, ParseImpl>(
6363
class_data_.base());
6464

65-
constexpr ClassDataLite<1> ImplicitWeakMessage::class_data_ = {
65+
constexpr ClassDataLite ImplicitWeakMessage::class_data_ = {
6666
{
6767
&implicit_weak_message_default_instance.instance,
6868
&table_.header,

src/google/protobuf/implicit_weak_message.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class PROTOBUF_EXPORT ImplicitWeakMessage final : public MessageLite {
8585

8686
private:
8787
static const TcParseTable<0> table_;
88-
static const ClassDataLite<1> class_data_;
88+
static const ClassDataLite class_data_;
8989

9090
static void MergeImpl(MessageLite&, const MessageLite&);
9191

src/google/protobuf/message_lite.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ absl::string_view TypeId::name() const {
114114
return data_->full().descriptor_methods()->get_type_name(data_);
115115
}
116116

117-
// For LITE messages, the type name is a char[] just beyond ClassData.
118-
return reinterpret_cast<const char*>(data_) + sizeof(internal::ClassData);
117+
// For LITE messages, the type name is accessed via ClassDataLite.
118+
return static_cast<const internal::ClassDataLite*>(data_)->type_name;
119119
}
120120

121121
std::string MessageLite::InitializationErrorString() const {

src/google/protobuf/message_lite.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -464,12 +464,13 @@ struct PROTOBUF_EXPORT ClassData {
464464
uint8_t alignment() const { return message_creator.alignment(); }
465465
};
466466

467-
template <size_t N>
468-
struct ClassDataLite {
469-
ClassData header;
470-
const char type_name[N];
467+
struct ClassDataLite : ClassData {
468+
constexpr ClassDataLite(ClassData base, const char* type_name)
469+
: ClassData(base), type_name(type_name) {}
471470

472-
constexpr const ClassData* base() const { return &header; }
471+
const char* type_name;
472+
473+
constexpr const ClassData* base() const { return this; }
473474
};
474475

475476
// We use a secondary vtable for descriptor based methods. This way ClassData

0 commit comments

Comments
 (0)