1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-26 17:38:39 +00:00

protobuf: simplify the list of bytes optimization

This commit is contained in:
Jan Pochyla 2018-11-02 16:43:17 +01:00
parent 76dbef99bf
commit 5c409ceeac

View File

@ -275,14 +275,14 @@ async def dump_message(writer, msg, fields=None):
elif ftype is BoolType: elif ftype is BoolType:
await dump_uvarint(writer, int(svalue)) await dump_uvarint(writer, int(svalue))
elif ftype is BytesType and is_chunked(svalue):
await dump_uvarint(writer, len_list_bytes(svalue))
for sub_svalue in svalue:
await writer.awrite(sub_svalue)
elif ftype is BytesType: elif ftype is BytesType:
await dump_uvarint(writer, len(svalue)) if isinstance(svalue, list):
await writer.awrite(svalue) await dump_uvarint(writer, _count_bytes_list(svalue))
for sub_svalue in svalue:
await writer.awrite(sub_svalue)
else:
await dump_uvarint(writer, len(svalue))
await writer.awrite(svalue)
elif ftype is UnicodeType: elif ftype is UnicodeType:
svalue = svalue.encode() svalue = svalue.encode()
@ -334,9 +334,10 @@ def count_message(msg, fields=None):
elif ftype is BytesType: elif ftype is BytesType:
for svalue in fvalue: for svalue in fvalue:
svalue = ( if isinstance(svalue, list):
len(svalue) if not is_chunked(svalue) else len_list_bytes(svalue) svalue = _count_bytes_list(svalue)
) else:
svalue = len(svalue)
nbytes += count_uvarint(svalue) nbytes += count_uvarint(svalue)
nbytes += svalue nbytes += svalue
@ -360,15 +361,7 @@ def count_message(msg, fields=None):
return nbytes return nbytes
def is_chunked(svalue): def _count_bytes_list(svalue):
return (
isinstance(svalue, list)
and len(svalue) > 0
and not isinstance(svalue[0], (int, bool))
)
def len_list_bytes(svalue):
res = 0 res = 0
for x in svalue: for x in svalue:
res += len(x) res += len(x)