mirror of
https://github.com/esphome/esphome.git
synced 2026-09-24 05:24:14 +00:00
[api] Add force field option to skip zero checks on hot path
Add a `force` proto field option that generates `_force` variants of calc_ and encode methods, skipping the zero/empty check. Applied to BluetoothLERawAdvertisement fields that are almost always non-default (address, rssi, data) to eliminate dead branches on the BLE proxy hot path. On-device benchmarks show calculate_size improved from 12,649 ns to 10,982 ns per 12-advertisement batch (-13.2%) with only +8 bytes flash. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.6
parent
ea7cfffdda
commit
a27f5127d8
@@ -151,6 +151,11 @@ class TypeInfo(ABC):
|
||||
"""Check if the field is repeated."""
|
||||
return self._field.label == FieldDescriptorProto.LABEL_REPEATED
|
||||
|
||||
@property
|
||||
def force(self) -> bool:
|
||||
"""Check if this field should always be encoded (skip zero/empty check)."""
|
||||
return get_field_opt(self._field, pb.force, False)
|
||||
|
||||
@property
|
||||
def wire_type(self) -> WireType:
|
||||
"""Get the wire type for the field."""
|
||||
@@ -218,6 +223,8 @@ class TypeInfo(ABC):
|
||||
|
||||
@property
|
||||
def encode_content(self) -> str:
|
||||
if self.force:
|
||||
return f"buffer.{self.encode_func}({self.number}, this->{self.field_name}, true);"
|
||||
return f"buffer.{self.encode_func}({self.number}, this->{self.field_name});"
|
||||
|
||||
encode_func = None
|
||||
@@ -1086,6 +1093,8 @@ class FixedArrayBytesType(TypeInfo):
|
||||
|
||||
@property
|
||||
def encode_content(self) -> str:
|
||||
if self.force:
|
||||
return f"buffer.encode_bytes({self.number}, this->{self.field_name}, this->{self.field_name}_len, true);"
|
||||
return f"buffer.encode_bytes({self.number}, this->{self.field_name}, this->{self.field_name}_len);"
|
||||
|
||||
def dump(self, name: str) -> str:
|
||||
@@ -2134,7 +2143,8 @@ def build_message_type(
|
||||
encode.extend(wrap_with_ifdef(ti.encode_content, field_ifdef))
|
||||
size_calc.extend(
|
||||
wrap_with_ifdef(
|
||||
ti.get_size_calculation(f"this->{ti.field_name}"), field_ifdef
|
||||
ti.get_size_calculation(f"this->{ti.field_name}", ti.force),
|
||||
field_ifdef,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user