Skip to content

Commit 162a740

Browse files
tonyliaosscopybara-github
authored andcommitted
Reduced nesting in GenerateByteSize: slight readability improvements in generated code.
GenerateByteSize itself remains deeply nested, but by factoring out one part of the loop, at least we make the part that generates UpdateByteSize a bit more readable. Making the callsite of MayEmitIfNonDefaultCheck less nested actually resulted in slight readability improvements also in the generated code, namely of the form: @@ -10563,8 +10559,7 @@ PROTOBUF_NOINLINE void OneStringEdition: { // string data = 1; - cached_has_bits = - this_._impl_._has_bits_[0]; + cached_has_bits = this_._impl_._has_bits_[0]; if (cached_has_bits & 0x00000001u) { total_size += 1 + ::proto2::internal::WireFormatLite::StringSize( this_._internal_data()); These readability improvements should be kept IMO -- they make the generated protobuf C++ code slightly easier to read. PiperOrigin-RevId: 655180880
1 parent a0387c1 commit 162a740

File tree

5 files changed

+59
-61
lines changed

5 files changed

+59
-61
lines changed

editions/golden/compare_cpp_codegen_failure.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030
{
3131
- // optional int32 int32_field = 1;
3232
+ // int32 int32_field = 1;
33-
cached_has_bits =
34-
this_._impl_._has_bits_[0];
33+
cached_has_bits = this_._impl_._has_bits_[0];
3534
if (cached_has_bits & 0x00000001u) {
35+
total_size += ::_pbi::WireFormatLite::Int32SizePlusOne(
3636
[ FAILED ] third_party/protobuf/editions/golden/simple_proto3.pb.cc
3737
[ RUN ] third_party/protobuf/editions/golden/simple_proto3.pb.h
3838
@@ @@

editions/golden/compare_cpp_codegen_failure.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<testsuites tests="1" name="AllTests">
33
<testsuite name="EditionsCodegenTests">
44
<testcase name="third_party/protobuf/editions/golden/simple_proto3.pb.cc" status="run" result="completed" classname="DiffTest">
5-
<failure message="Value of: third_party/protobuf/editions/golden/simple_proto3.pb.cc&#x0A;Expected: &#x0A;// Generated by the protocol buffer compiler. DO NOT EDIT!&#x0A;// NO CHECKED-IN PROTOBUF GENCODE&#x0A;// source: third_party/protobuf/editions/golden/simple_proto3.proto&#x0A;// Protobuf C++ Version: 0.20240718.0&#x0A;&#x0A;#include &quot;third_party/protobuf/editions/golden/simple_proto3.pb.h&quot;&#x0A;&#x0A;#include &lt;algorithm&gt;&#x0A;#include &lt;type_traits&gt;&#x0A;#include &quot;third_party/protobuf/io/coded_stream.h&quot;&#x0A;#include &quot;third_party/protobuf/generated_message_tctable_impl.h&quot;&#x0A;#include &quot;third_party/protobuf/extension_set.h&quot;&#x0A;#include &quot;third_party/protobuf/wire_format_lite.h&quot;&#x0A;#include &quot;third_party/protobuf/io/zero_copy_stream_impl_lite.h&quot;&#x0A;// @@protoc_insertion_point(includes)&#x0A;&#x0A;// Must be included last.&#x0A;, with the difference:&#x0A;@@ @@&#x0A; ::_pbi::TcParser::GetTable&lt;::protobuf_editions_test::golden::SimpleProto3&gt;(), // to_prefetch&#x0A; #endif // PROTOBUF_PREFETCH_PARSE_TABLE&#x0A; }, {{&#x0A;- // optional int32 int32_field = 1;&#x0A;+ // int32 int32_field = 1;&#x0A; {::_pbi::TcParser::FastV32S1,&#x0A; {8, 0, 0, PROTOBUF_FIELD_OFFSET(SimpleProto3, _impl_.int32_field_)}},&#x0A; }}, {{&#x0A; 65535, 65535&#x0A; }}, {{&#x0A;- // optional int32 int32_field = 1;&#x0A;+ // int32 int32_field = 1;&#x0A; {PROTOBUF_FIELD_OFFSET(SimpleProto3, _impl_.int32_field_), _Internal::kHasBitsOffset + 0, 0,&#x0A; (0 | ::_fl::kFcOptional | ::_fl::kInt32)},&#x0A; }},&#x0A;@@ @@&#x0A; (void)cached_has_bits;&#x0A; &#x0A; cached_has_bits = this_._impl_._has_bits_[0];&#x0A;- // optional int32 int32_field = 1;&#x0A;+ // int32 int32_field = 1;&#x0A; if (cached_has_bits &amp; 0x00000001u) {&#x0A; target = ::proto2::internal::WireFormatLite::&#x0A; WriteInt32ToArrayWithField&lt;1&gt;(&#x0A;@@ @@&#x0A; (void)cached_has_bits;&#x0A; &#x0A; {&#x0A;- // optional int32 int32_field = 1;&#x0A;+ // int32 int32_field = 1;&#x0A; cached_has_bits =&#x0A; this_._impl_._has_bits_[0];&#x0A; if (cached_has_bits &amp; 0x00000001u) {" type=""></failure>
5+
<failure message="Value of: third_party/protobuf/editions/golden/simple_proto3.pb.cc&#x0A;Expected: &#x0A;// Generated by the protocol buffer compiler. DO NOT EDIT!&#x0A;// NO CHECKED-IN PROTOBUF GENCODE&#x0A;// source: third_party/protobuf/editions/golden/simple_proto3.proto&#x0A;// Protobuf C++ Version: 0.20240718.0&#x0A;&#x0A;#include &quot;third_party/protobuf/editions/golden/simple_proto3.pb.h&quot;&#x0A;&#x0A;#include &lt;algorithm&gt;&#x0A;#include &lt;type_traits&gt;&#x0A;#include &quot;third_party/protobuf/io/coded_stream.h&quot;&#x0A;#include &quot;third_party/protobuf/generated_message_tctable_impl.h&quot;&#x0A;#include &quot;third_party/protobuf/extension_set.h&quot;&#x0A;#include &quot;third_party/protobuf/wire_format_lite.h&quot;&#x0A;#include &quot;third_party/protobuf/io/zero_copy_stream_impl_lite.h&quot;&#x0A;// @@protoc_insertion_point(includes)&#x0A;&#x0A;// Must be included last.&#x0A;, with the difference:&#x0A;@@ @@&#x0A; ::_pbi::TcParser::GetTable&lt;::protobuf_editions_test::golden::SimpleProto3&gt;(), // to_prefetch&#x0A; #endif // PROTOBUF_PREFETCH_PARSE_TABLE&#x0A; }, {{&#x0A;- // optional int32 int32_field = 1;&#x0A;+ // int32 int32_field = 1;&#x0A; {::_pbi::TcParser::FastV32S1,&#x0A; {8, 0, 0, PROTOBUF_FIELD_OFFSET(SimpleProto3, _impl_.int32_field_)}},&#x0A; }}, {{&#x0A; 65535, 65535&#x0A; }}, {{&#x0A;- // optional int32 int32_field = 1;&#x0A;+ // int32 int32_field = 1;&#x0A; {PROTOBUF_FIELD_OFFSET(SimpleProto3, _impl_.int32_field_), _Internal::kHasBitsOffset + 0, 0,&#x0A; (0 | ::_fl::kFcOptional | ::_fl::kInt32)},&#x0A; }},&#x0A;@@ @@&#x0A; (void)cached_has_bits;&#x0A; &#x0A; cached_has_bits = this_._impl_._has_bits_[0];&#x0A;- // optional int32 int32_field = 1;&#x0A;+ // int32 int32_field = 1;&#x0A; if (cached_has_bits &amp; 0x00000001u) {&#x0A; target = ::proto2::internal::WireFormatLite::&#x0A; WriteInt32ToArrayWithField&lt;1&gt;(&#x0A;@@ @@&#x0A; (void)cached_has_bits;&#x0A; &#x0A; {&#x0A;- // optional int32 int32_field = 1;&#x0A;+ // int32 int32_field = 1;&#x0A; cached_has_bits = this_._impl_._has_bits_[0];&#x0A; if (cached_has_bits &amp; 0x00000001u) {&#x0A; total_size += ::_pbi::WireFormatLite::Int32SizePlusOne(" type=""></failure>
66
</testcase>
77
<testcase name="third_party/protobuf/editions/golden/simple_proto3.pb.h" status="run" result="completed" classname="DiffTest">
88
<failure message="Value of: third_party/protobuf/editions/golden/simple_proto3.pb.h&#x0A;Expected: &#x0A;// Generated by the protocol buffer compiler. DO NOT EDIT!&#x0A;// NO CHECKED-IN PROTOBUF GENCODE&#x0A;// source: third_party/protobuf/editions/golden/simple_proto3.proto&#x0A;// Protobuf C++ Version: 0.20240718.0&#x0A;&#x0A;#ifndef GOOGLE_PROTOBUF_INCLUDED_third_5fparty_2fprotobuf_2feditions_2fgolden_2fsimple_5fproto3_2eproto_2epb_2eh&#x0A;#define GOOGLE_PROTOBUF_INCLUDED_third_5fparty_2fprotobuf_2feditions_2fgolden_2fsimple_5fproto3_2eproto_2epb_2eh&#x0A;&#x0A;#include &lt;limits&gt;&#x0A;#include &lt;string&gt;&#x0A;#include &lt;type_traits&gt;&#x0A;#include &lt;utility&gt;&#x0A;&#x0A;#include &quot;third_party/protobuf/runtime_version.h&quot;&#x0A;#if PROTOBUF_VERSION != 20240718&#x0A;#error &quot;Protobuf C++ gencode is built with an incompatible version of&quot;&#x0A;#error &quot;Protobuf C++ headers/runtime. See&quot;&#x0A;#error &quot;https://protobuf.dev/support/cross-version-runtime-guarantee/#cpp&quot;&#x0A;#endif&#x0A;#include &quot;third_party/protobuf/io/coded_stream.h&quot;&#x0A;#include &quot;third_party/protobuf/arena.h&quot;&#x0A;#include &quot;third_party/protobuf/arenastring.h&quot;&#x0A;#include &quot;third_party/protobuf/generated_message_tctable_decl.h&quot;&#x0A;#include &quot;third_party/protobuf/generated_message_util.h&quot;&#x0A;#include &quot;third_party/protobuf/metadata_lite.h&quot;&#x0A;#include &quot;third_party/protobuf/message_lite.h&quot;&#x0A;// @@protoc_insertion_point(includes)&#x0A;&#x0A;// Must be included last.&#x0A;&#x0A;#endif // GOOGLE_PROTOBUF_INCLUDED_third_5fparty_2fprotobuf_2feditions_2fgolden_2fsimple_5fproto3_2eproto_2epb_2eh&#x0A;, with the difference:&#x0A;@@ @@&#x0A; enum : int {&#x0A; kInt32FieldFieldNumber = 1,&#x0A; };&#x0A;- // optional int32 int32_field = 1;&#x0A;+ // int32 int32_field = 1;&#x0A; bool has_int32_field() const;&#x0A; void clear_int32_field() ;&#x0A; ::int32_t int32_field() const;&#x0A;@@ @@&#x0A; &#x0A; // SimpleProto3&#x0A; &#x0A;-// optional int32 int32_field = 1;&#x0A;+// int32 int32_field = 1;&#x0A; inline bool SimpleProto3::has_int32_field() const {&#x0A; bool value = (_impl_._has_bits_[0] &amp; 0x00000001u) != 0;&#x0A; return value;" type=""></failure>

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

Lines changed: 51 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,6 +1213,55 @@ class AccessorVerifier {
12131213

12141214
} // namespace
12151215

1216+
void MessageGenerator::EmitUpdateByteSizeForField(
1217+
const FieldDescriptor* field, io::Printer* p,
1218+
int& cached_has_word_index) const {
1219+
p->Emit(
1220+
{{"comment", [&] { PrintFieldComment(Formatter{p}, field, options_); }},
1221+
{"update_byte_size_for_field",
1222+
[&] { field_generators_.get(field).GenerateByteSize(p); }},
1223+
{"update_cached_has_bits",
1224+
[&] {
1225+
if (!HasHasbit(field) || field->options().weak()) return;
1226+
1227+
int has_bit_index = has_bit_indices_[field->index()];
1228+
1229+
if (cached_has_word_index == (has_bit_index / 32)) return;
1230+
1231+
cached_has_word_index = (has_bit_index / 32);
1232+
p->Emit({{"index", cached_has_word_index}},
1233+
R"cc(
1234+
cached_has_bits = this_.$has_bits$[$index$];
1235+
)cc");
1236+
}},
1237+
{"check_if_field_present",
1238+
[&] {
1239+
if (!HasHasbit(field)) {
1240+
MayEmitIfNonDefaultCheck(p, "this_.", field);
1241+
return;
1242+
}
1243+
1244+
if (field->options().weak()) {
1245+
p->Emit("if (has_$name$())");
1246+
return;
1247+
}
1248+
1249+
int has_bit_index = has_bit_indices_[field->index()];
1250+
p->Emit(
1251+
{{"mask", absl::StrFormat("0x%08xu",
1252+
uint32_t{1} << (has_bit_index % 32))}},
1253+
"if (cached_has_bits & $mask$)");
1254+
}}},
1255+
R"cc(
1256+
$comment$;
1257+
$update_cached_has_bits$;
1258+
$check_if_field_present$ {
1259+
//~ Force newline.
1260+
$update_byte_size_for_field$;
1261+
}
1262+
)cc");
1263+
}
1264+
12161265
void MessageGenerator::GenerateFieldAccessorDefinitions(io::Printer* p) {
12171266
p->Emit("// $classname$\n\n");
12181267

@@ -4748,62 +4797,8 @@ void MessageGenerator::GenerateByteSize(io::Printer* p) {
47484797
// Go back and emit checks for each of the fields we
47494798
// processed.
47504799
for (const auto* field : fields) {
4751-
p->Emit(
4752-
{{"comment",
4753-
[&] {
4754-
PrintFieldComment(Formatter{p}, field,
4755-
options_);
4756-
}},
4757-
{"update_byte_size_for_field",
4758-
[&] {
4759-
field_generators_.get(field).GenerateByteSize(
4760-
p);
4761-
}},
4762-
{"update_cached_has_bits",
4763-
[&] {
4764-
if (!HasHasbit(field) ||
4765-
field->options().weak())
4766-
return;
4767-
int has_bit_index =
4768-
has_bit_indices_[field->index()];
4769-
if (cached_has_word_index ==
4770-
(has_bit_index / 32))
4771-
return;
4772-
cached_has_word_index = (has_bit_index / 32);
4773-
p->Emit({{"index", cached_has_word_index}},
4774-
R"cc(
4775-
cached_has_bits =
4776-
this_.$has_bits$[$index$];
4777-
)cc");
4778-
}},
4779-
{"check_if_field_present",
4780-
[&] {
4781-
if (!HasHasbit(field)) {
4782-
MayEmitIfNonDefaultCheck(p, "this_.", field);
4783-
return;
4784-
}
4785-
4786-
if (field->options().weak()) {
4787-
p->Emit("if (has_$name$())");
4788-
return;
4789-
}
4790-
4791-
int has_bit_index =
4792-
has_bit_indices_[field->index()];
4793-
p->Emit(
4794-
{{"mask", absl::StrFormat(
4795-
"0x%08xu",
4796-
1u << (has_bit_index % 32))}},
4797-
"if (cached_has_bits & $mask$)");
4798-
}}},
4799-
R"cc(
4800-
$comment$;
4801-
$update_cached_has_bits$;
4802-
$check_if_field_present$ {
4803-
//~ Force newline.
4804-
$update_byte_size_for_field$;
4805-
}
4806-
)cc");
4800+
EmitUpdateByteSizeForField(field, p,
4801+
cached_has_word_index);
48074802
}
48084803
}},
48094804
{"may_update_cached_has_word_index",

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ class MessageGenerator {
186186
int HasWordIndex(const FieldDescriptor* field) const;
187187
std::vector<uint32_t> RequiredFieldsBitMask() const;
188188

189+
// Helper function to reduce nesting levels of deep Emit calls.
190+
void EmitUpdateByteSizeForField(const FieldDescriptor* field, io::Printer* p,
191+
int& cached_has_word_index) const;
192+
189193
const Descriptor* descriptor_;
190194
int index_in_file_messages_;
191195
Options options_;

src/google/protobuf/descriptor.pb.cc

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)