Skip to content

Commit 5b639c8

Browse files
authored
Micro-optimization: use tuples instead of lists for conditions (#228)
This should give a small speed boost to some critical code paths.
1 parent 7c5ee47 commit 5b639c8

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

src/betterproto/__init__.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -307,16 +307,16 @@ def encode_varint(value: int) -> bytes:
307307

308308
def _preprocess_single(proto_type: str, wraps: str, value: Any) -> bytes:
309309
"""Adjusts values before serialization."""
310-
if proto_type in [
310+
if proto_type in (
311311
TYPE_ENUM,
312312
TYPE_BOOL,
313313
TYPE_INT32,
314314
TYPE_INT64,
315315
TYPE_UINT32,
316316
TYPE_UINT64,
317-
]:
317+
):
318318
return encode_varint(value)
319-
elif proto_type in [TYPE_SINT32, TYPE_SINT64]:
319+
elif proto_type in (TYPE_SINT32, TYPE_SINT64):
320320
# Handle zig-zag encoding.
321321
return encode_varint(value << 1 if value >= 0 else (value << 1) ^ (~0))
322322
elif proto_type in FIXED_TYPES:
@@ -840,18 +840,18 @@ def _postprocess_single(
840840
) -> Any:
841841
"""Adjusts values after parsing."""
842842
if wire_type == WIRE_VARINT:
843-
if meta.proto_type in [TYPE_INT32, TYPE_INT64]:
843+
if meta.proto_type in (TYPE_INT32, TYPE_INT64):
844844
bits = int(meta.proto_type[3:])
845845
value = value & ((1 << bits) - 1)
846846
signbit = 1 << (bits - 1)
847847
value = int((value ^ signbit) - signbit)
848-
elif meta.proto_type in [TYPE_SINT32, TYPE_SINT64]:
848+
elif meta.proto_type in (TYPE_SINT32, TYPE_SINT64):
849849
# Undo zig-zag encoding
850850
value = (value >> 1) ^ (-(value & 1))
851851
elif meta.proto_type == TYPE_BOOL:
852852
# Booleans use a varint encoding, so convert it to true/false.
853853
value = value > 0
854-
elif wire_type in [WIRE_FIXED_32, WIRE_FIXED_64]:
854+
elif wire_type in (WIRE_FIXED_32, WIRE_FIXED_64):
855855
fmt = _pack_fmt(meta.proto_type)
856856
value = struct.unpack(fmt, value)[0]
857857
elif wire_type == WIRE_LEN_DELIM:
@@ -915,10 +915,10 @@ def parse(self: T, data: bytes) -> T:
915915
pos = 0
916916
value = []
917917
while pos < len(parsed.value):
918-
if meta.proto_type in [TYPE_FLOAT, TYPE_FIXED32, TYPE_SFIXED32]:
918+
if meta.proto_type in (TYPE_FLOAT, TYPE_FIXED32, TYPE_SFIXED32):
919919
decoded, pos = parsed.value[pos : pos + 4], pos + 4
920920
wire_type = WIRE_FIXED_32
921-
elif meta.proto_type in [TYPE_DOUBLE, TYPE_FIXED64, TYPE_SFIXED64]:
921+
elif meta.proto_type in (TYPE_DOUBLE, TYPE_FIXED64, TYPE_SFIXED64):
922922
decoded, pos = parsed.value[pos : pos + 8], pos + 8
923923
wire_type = WIRE_FIXED_64
924924
else:
@@ -1264,7 +1264,7 @@ def to_timedelta(self) -> timedelta:
12641264
def delta_to_json(delta: timedelta) -> str:
12651265
parts = str(delta.total_seconds()).split(".")
12661266
if len(parts) > 1:
1267-
while len(parts[1]) not in [3, 6, 9]:
1267+
while len(parts[1]) not in (3, 6, 9):
12681268
parts[1] = f"{parts[1]}0"
12691269
return f"{'.'.join(parts)}s"
12701270

0 commit comments

Comments
 (0)