Skip to content

Commit

Permalink
Shrink code size of generated protocol buffer code via a few
Browse files Browse the repository at this point in the history
changes.

(1) Use PROTOBUF_NOINLINE attributes to avoid inlining of
Clear(), ByteSizeLong() and IsInitialized() routines
within the same .pb.cc file

(2) If the tag number is less than 16 and the type is one of
a handful of common types, call a non-inlined templated
routine specialized for that tag number that handles both
the EnsureSpace call and the encoding of the tag and the
value into the array.

PiperOrigin-RevId: 529162319
  • Loading branch information
jeffreyadean authored and copybara-github committed May 3, 2023
1 parent fdfb958 commit 97c3513
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,25 @@ void SingularPrimitive::GenerateInlineAccessorDefinitions(

void SingularPrimitive::GenerateSerializeWithCachedSizesToArray(
io::Printer* p) const {
p->Emit(R"cc(
target = stream->EnsureSpace(target);
target = ::_pbi::WireFormatLite::Write$DeclaredType$ToArray(
$number$, this->_internal_$name$(), target);
)cc");
if ((descriptor_->number() < 16) &&
(descriptor_->type() == FieldDescriptor::TYPE_INT32 ||
descriptor_->type() == FieldDescriptor::TYPE_INT64 ||
descriptor_->type() == FieldDescriptor::TYPE_ENUM)) {
// Call special non-inlined routine with tag number hardcoded as a
// template parameter that handles the EnsureSpace and the writing
// of the tag+value to the array
p->Emit(R"cc(
target = ::$proto_ns$::internal::WireFormatLite::
Write$declared_type$ToArrayWithField<$number$>(
stream, this->_internal_$name$(), target);
)cc");
} else {
p->Emit(R"cc(
target = stream->EnsureSpace(target);
target = ::_pbi::WireFormatLite::Write$DeclaredType$ToArray(
$number$, this->_internal_$name$(), target);
)cc");
}
}

void SingularPrimitive::GenerateByteSize(io::Printer* p) const {
Expand Down
6 changes: 3 additions & 3 deletions src/google/protobuf/compiler/cpp/message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2852,7 +2852,7 @@ void MessageGenerator::GenerateClear(io::Printer* p) {
const int kMaxUnconditionalPrimitiveBytesClear = 4;

format(
"void $classname$::Clear() {\n"
"PROTOBUF_NOINLINE void $classname$::Clear() {\n"
"// @@protoc_insertion_point(message_clear_start:$full_name$)\n");
format.Indent();

Expand Down Expand Up @@ -3939,7 +3939,7 @@ void MessageGenerator::GenerateByteSize(io::Printer* p) {
if (descriptor_->options().message_set_wire_format()) {
// Special-case MessageSet.
format(
"::size_t $classname$::ByteSizeLong() const {\n"
"PROTOBUF_NOINLINE ::size_t $classname$::ByteSizeLong() const {\n"
"$annotate_bytesize$"
"// @@protoc_insertion_point(message_set_byte_size_start:$full_name$)\n"
" ::size_t total_size = $extensions$.MessageSetByteSize();\n"
Expand Down Expand Up @@ -4182,7 +4182,7 @@ void MessageGenerator::GenerateByteSize(io::Printer* p) {
void MessageGenerator::GenerateIsInitialized(io::Printer* p) {
if (HasSimpleBaseClass(descriptor_, options_)) return;
Formatter format(p);
format("bool $classname$::IsInitialized() const {\n");
format("PROTOBUF_NOINLINE bool $classname$::IsInitialized() const {\n");
format.Indent();

if (descriptor_->extension_range_count() > 0) {
Expand Down
34 changes: 17 additions & 17 deletions src/google/protobuf/compiler/plugin.pb.cc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 97c3513

Please sign in to comment.