refactor(python): faster serialization of nested protobuf
What changed, and why it matters
This is a routine performance refactor in Trezor's Python library. It replaces a slow, CPU-heavy way of measuring nested protobuf message sizes with a faster approach that builds the message in memory once. There is no indication this fixes a security vulnerability; it is described purely as a speed improvement.
No security action required. Treat as normal code maintenance. If reviewing, verify that memory usage growth for deeply nested messages is acceptable for the library's deployment contexts.
Security signals we found
No security framing in commit title or message
No changelog entry marked as security-related
Change is a performance refactor with equivalent output
No input validation, bounds checking, or trust boundary changes
Evidence from the diff
The commit removes the CountingWriter class, which serializes a nested message once just to count bytes, then serializes it a second time to write it. For deeply nested messages this caused exponential CPU cost. The new code serializes the nested message into a BytesIO buffer, writes the length prefix, then writes the buffer. This is a classic time-for-space optimization and does not change wire format or parsing behavior.
Changed components
python/src/trezorlib/protobuf.pydump_message serialization path for nested protobuf messagesInspect captured patch +6 / −14
### python/src/trezorlib/protobuf.py
@@ -349,16 +349,6 @@ def readinto(self, buf: bytearray, /) -> int:
return nread
-class CountingWriter:
- def __init__(self) -> None:
- self.size = 0
-
- def write(self, buf: bytes | bytearray | memoryview, /) -> int:
- nwritten = len(buf)
- self.size += nwritten
- return nwritten
-
-
def decode_packed_array_field(field: Field, reader: Reader) -> list[t.Any]:
assert field.repeated, "Not decoding packed array into non-repeated field"
length = load_uvarint(reader)
@@ -515,10 +505,12 @@ def dump_message(writer: Writer, msg: "MessageType") -> None:
raise ValueError(
f"Value {svalue} in field {field.name} is not {field.py_type.__name__}"
)
- counter = CountingWriter()
- dump_message(counter, svalue)
- dump_uvarint(writer, counter.size)
- dump_message(writer, svalue)
+ inner = BytesIO()
+ dump_message(inner, svalue)
+ inner = inner.getvalue()
+ dump_uvarint(writer, len(inner))
+ writer.write(inner)
+ inner = None
elif issubclass(field.py_type, IntEnum):
if svalue not in field.py_type.__members__.values():Why this scored 18/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.