1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-30 03:18:20 +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,12 +275,12 @@ 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): elif ftype is BytesType:
await dump_uvarint(writer, len_list_bytes(svalue)) if isinstance(svalue, list):
await dump_uvarint(writer, _count_bytes_list(svalue))
for sub_svalue in svalue: for sub_svalue in svalue:
await writer.awrite(sub_svalue) await writer.awrite(sub_svalue)
else:
elif ftype is BytesType:
await dump_uvarint(writer, len(svalue)) await dump_uvarint(writer, len(svalue))
await writer.awrite(svalue) await writer.awrite(svalue)
@ -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)