mirror of
https://github.com/esphome/esphome.git
synced 2026-09-24 13:34:07 +00:00
[api] Add encode_raw_short_string for forced string fields with max_data_length
Replace 3-call sequence (write_raw_byte + write_raw_byte + encode_raw) with single inlined encode_raw_short_string call for forced string fields with max_data_length < 128. The compiler can hoist the pos pointer across all three writes in one function boundary.
This commit is contained in:
@@ -1074,10 +1074,16 @@ class PointerToStringBufferType(PointerToBufferTypeBase):
|
||||
|
||||
@property
|
||||
def encode_content(self) -> str:
|
||||
max_len = self.max_data_length
|
||||
if max_len is not None and max_len < 128 and self.force:
|
||||
tag = self.calculate_tag()
|
||||
if tag < 128:
|
||||
return (
|
||||
f"buffer.encode_raw_short_string({tag}, this->{self.field_name});"
|
||||
)
|
||||
if result := self._encode_bytes_with_precomputed_tag(
|
||||
f"this->{self.field_name}.c_str()",
|
||||
f"this->{self.field_name}.size()",
|
||||
max_len=self.max_data_length,
|
||||
):
|
||||
return result
|
||||
if self.force:
|
||||
|
||||
Reference in New Issue
Block a user