[api] Write protobuf encode output to pre-sized buffer directly (#14018)

This commit is contained in:
J. Nick Koston
2026-02-20 21:39:18 -06:00
committed by GitHub
parent f8f98bf428
commit f77da803c9
6 changed files with 286 additions and 240 deletions
+10 -2
View File
@@ -689,6 +689,14 @@ class MessageType(TypeInfo):
def encode_func(self) -> str:
return "encode_message"
@property
def encode_content(self) -> str:
# Singular message fields pass force=false (skip empty messages)
# The default for encode_nested_message is force=true (for repeated fields)
return (
f"buffer.{self.encode_func}({self.number}, this->{self.field_name}, false);"
)
@property
def decode_length(self) -> str:
# Override to return None for message types because we can't use template-based
@@ -2186,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:
@@ -2194,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