Skip to content

Commit

Permalink
Expand LazySerializerEmitter to cover proto3 cases.
Browse files Browse the repository at this point in the history
This should provide some performance wins for proto3, similar to the ones added years ago for proto2.

PiperOrigin-RevId: 513853367
  • Loading branch information
mkruskal-google authored and Copybara-Service committed Mar 3, 2023
1 parent eaa8f24 commit fab7f92
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions src/google/protobuf/compiler/cpp/message.cc
Expand Up @@ -3639,8 +3639,6 @@ void MessageGenerator::GenerateSerializeWithCachedSizesBody(io::Printer* p) {
LazySerializerEmitter(MessageGenerator* mg, io::Printer* p)
: mg_(mg),
p_(p),
eager_(mg->descriptor_->file()->syntax() ==
FileDescriptor::SYNTAX_PROTO3),
cached_has_bit_index_(kNoHasbit) {}

~LazySerializerEmitter() { Flush(); }
Expand All @@ -3649,13 +3647,14 @@ void MessageGenerator::GenerateSerializeWithCachedSizesBody(io::Printer* p) {
// oneof, and handle them at the next Flush().
void Emit(const FieldDescriptor* field) {
Formatter format(p_);
if (eager_ || MustFlush(field)) {

if (!field->has_presence() || MustFlush(field)) {
Flush();
}
if (!field->real_containing_oneof()) {
// TODO(ckennelly): Defer non-oneof fields similarly to oneof fields.

if (!field->options().weak() && !field->is_repeated() && !eager_) {
if (HasHasbit(field) && field->has_presence()) {
// We speculatively load the entire _has_bits_[index] contents, even
// if it is for only one field. Deferring non-oneof emitting would
// allow us to determine whether this is going to be useful.
Expand Down Expand Up @@ -3699,7 +3698,6 @@ void MessageGenerator::GenerateSerializeWithCachedSizesBody(io::Printer* p) {

MessageGenerator* mg_;
io::Printer* p_;
bool eager_;
std::vector<const FieldDescriptor*> v_;

// cached_has_bit_index_ maintains that:
Expand Down

0 comments on commit fab7f92

Please sign in to comment.