mirror of
https://github.com/esphome/esphome.git
synced 2026-02-25 21:43:14 -07:00
[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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user