diff --git a/src/google/protobuf/compiler/cpp/message.cc b/src/google/protobuf/compiler/cpp/message.cc index dbb121b2f3..ce527cf823 100644 --- a/src/google/protobuf/compiler/cpp/message.cc +++ b/src/google/protobuf/compiler/cpp/message.cc @@ -4526,9 +4526,26 @@ void MessageGenerator::GenerateByteSize(io::Printer* p) { // function, since advancing is actually slower. We sometimes // prefetch more than sizeof(message), because it helps with // next message on arena. - p->Emit(R"cc( - ::_pbi::Prefetch5LinesFrom7Lines(reinterpret_cast(this)); - )cc"); + bool generate_prefetch = false; + // Skip trivial messages with 0 or 1 fields, unless they are repeated, + // to reduce codesize. + switch (optimized_order_.size()) { + case 1: + generate_prefetch = optimized_order_[0]->is_repeated(); + break; + case 0: + break; + default: + generate_prefetch = true; + } + if (!IsPresentMessage(descriptor_, options_)) { + generate_prefetch = false; + } + if (generate_prefetch) { + p->Emit(R"cc( + ::_pbi::Prefetch5LinesFrom7Lines(reinterpret_cast(this)); + )cc"); + } while (it != end) { auto next = FindNextUnequalChunk(it, end, MayGroupChunksForHaswordsCheck); diff --git a/src/google/protobuf/editions/golden/compare_cpp_codegen_failure.txt b/src/google/protobuf/editions/golden/compare_cpp_codegen_failure.txt index b6048b1fa5..03be36fe32 100644 --- a/src/google/protobuf/editions/golden/compare_cpp_codegen_failure.txt +++ b/src/google/protobuf/editions/golden/compare_cpp_codegen_failure.txt @@ -25,9 +25,9 @@ target = ::proto2::internal::WireFormatLite:: WriteInt32ToArrayWithField<1>( @@ @@ + // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; - ::_pbi::Prefetch5LinesFrom7Lines(reinterpret_cast(this)); - // optional int32 int32_field = 1; + // int32 int32_field = 1; cached_has_bits = _impl_._has_bits_[0]; diff --git a/src/google/protobuf/editions/golden/compare_cpp_codegen_failure.xml b/src/google/protobuf/editions/golden/compare_cpp_codegen_failure.xml index d5ad450307..34a5543e6e 100644 --- a/src/google/protobuf/editions/golden/compare_cpp_codegen_failure.xml +++ b/src/google/protobuf/editions/golden/compare_cpp_codegen_failure.xml @@ -2,7 +2,7 @@ - +