[api] Pass ProtoWriteBuffer by reference in encode()

Pass ProtoWriteBuffer by reference instead of by value in virtual
encode() methods. This avoids copying both buffer_ and pos_ pointers
for every encode call - only a single pointer is passed.

Also simplifies encode_message since child writes directly advance
the shared pos_. Adds debug_check_size_ to verify calculate_size
matches actual encode output.
This commit is contained in:
J. Nick Koston
2026-02-16 15:01:59 -06:00
parent af4579ca87
commit 6369f3b279
6 changed files with 199 additions and 186 deletions
+2 -2
View File
@@ -2194,7 +2194,7 @@ def build_message_type(
# Only generate encode method if this message needs encoding and has fields
if needs_encode and encode:
o = f"void {desc.name}::encode(ProtoWriteBuffer buffer) const {{"
o = f"void {desc.name}::encode(ProtoWriteBuffer &buffer) const {{"
if len(encode) == 1 and len(encode[0]) + len(o) + 3 < 120:
o += f" {encode[0]} }}\n"
else:
@@ -2202,7 +2202,7 @@ def build_message_type(
o += indent("\n".join(encode)) + "\n"
o += "}\n"
cpp += o
prot = "void encode(ProtoWriteBuffer buffer) const override;"
prot = "void encode(ProtoWriteBuffer &buffer) const override;"
public_content.append(prot)
# If no fields to encode or message doesn't need encoding, the default implementation in ProtoMessage will be used