8000 Don't create a bytearray for each call to _serialize_single · danielgtaylor/python-betterproto@98a91f5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 98a91f5

Browse files
committed
Don't create a bytearray for each call to _serialize_single
1 parent 6e8d5da commit 98a91f5

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

src/betterproto/__init__.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -414,24 +414,25 @@ def _serialize_single(
414414
"""Serializes a single field and value."""
415415
value = _preprocess_single(proto_type, wraps, value)
416416

417-
output = bytearray()
418417
if proto_type in WIRE_VARINT_TYPES:
419418
key = encode_varint(field_number << 3)
420-
output += key + value
419+
output = key + value
421420
elif proto_type in WIRE_FIXED_32_TYPES:
422421
key = encode_varint((field_number << 3) | 5)
423-
output += key + value
422+
output = key + value
424423
elif proto_type in WIRE_FIXED_64_TYPES:
425424
key = encode_varint((field_number << 3) | 1)
426-
output += key + value
425+
output = key + value
427426
elif proto_type in WIRE_LEN_DELIM_TYPES:
428-
if len(value) or serialize_empty or wraps:
427+
if value or serialize_empty or wraps:
429428
key = encode_varint((field_number << 3) | 2)
430-
output += key + encode_varint(len(value)) + value
429+
output = key + encode_varint(len(value)) + value
430+
else:
431+
output = b""
431432
else:
432433
raise NotImplementedError(proto_type)
433434

434-
return bytes(output)
435+
return output
435436

436437

437438
def _parse_float(value: Any) -> float:

0 commit comments

Comments
 (0)
0