[api] Write protobuf encode output to pre-sized buffer directly

ProtoSize::calculate_size() already computes the exact encoded size
before encode() runs and is the boundary validation. The buffer is
pre-sized to match. Since the buffer size is always correct,
push_back() capacity checks on every byte are redundant overhead.

Rename ProtoWriteBuffer to ProtoWritePreSizedBuffer to document the
contract. Write through a raw uint8_t* pointer instead of push_back().
Pre-resize the buffer to include payload space before encoding.

Add ESPHOME_DEBUG_API bounds checks to validate writes stay within the
pre-sized region during development and integration testing.
This commit is contained in:
J. Nick Koston
2026-02-16 10:49:47 -06:00
parent 0c4827d348
commit af4579ca87
5 changed files with 96 additions and 62 deletions

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