mirror of
https://github.com/esphome/esphome.git
synced 2026-10-02 01:10:22 +00:00
[api] Default response only string fields to a null StringRef
A StringRef field that is only ever encoded is skipped when empty before its pointer is read, and the dump helper checks empty() first, so pointing it at "" buys nothing while costing one store per field in every message constructor. Fields that are decoded or force encoded keep the empty string default.
This commit is contained in:
@@ -576,7 +576,7 @@ def create_field_type_info(
|
||||
|
||||
# Special handling for string fields - use StringRef for zero-copy
|
||||
if field.type == 9:
|
||||
return PointerToStringBufferType(field, None)
|
||||
return PointerToStringBufferType(field, needs_decode)
|
||||
|
||||
validate_field_type(field.type, field.name)
|
||||
if field.type == 11:
|
||||
@@ -1215,12 +1215,22 @@ class PointerToStringBufferType(PointerToBufferTypeBase):
|
||||
reference_type = "StringRef &"
|
||||
const_reference_type = "const StringRef &"
|
||||
|
||||
def __init__(
|
||||
self, field: descriptor.FieldDescriptorProto, needs_decode: bool
|
||||
) -> None:
|
||||
super().__init__(field, None)
|
||||
self._needs_decode = needs_decode
|
||||
|
||||
@classmethod
|
||||
def can_use_dump_field(cls) -> bool:
|
||||
return True
|
||||
|
||||
@property
|
||||
def public_content(self) -> list[str]:
|
||||
# A field that is only ever encoded and skipped when empty never has its pointer read, so a
|
||||
# null default lets the whole message construct as one zero fill
|
||||
if not self._needs_decode and not self.force:
|
||||
return [f"StringRef {self.field_name}{{nullptr, 0}};"]
|
||||
return [f"StringRef {self.field_name}{{}};"]
|
||||
|
||||
@property
|
||||
|
||||
Reference in New Issue
Block a user