[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:
J. Nick Koston
2026-04-03 14:00:01 -10:00
parent 675400cc31
commit 2d13def0ee
3 changed files with 67 additions and 151 deletions
+7 -1
View File
@@ -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: