Micro-optimization: use tuples instead of lists for conditions (#228)
This should give a small speed boost to some critical code paths.
This commit is contained in:
parent
7c5ee47e68
commit
5b639c82b2
@ -307,16 +307,16 @@ def encode_varint(value: int) -> bytes:
|
|||||||
|
|
||||||
def _preprocess_single(proto_type: str, wraps: str, value: Any) -> bytes:
|
def _preprocess_single(proto_type: str, wraps: str, value: Any) -> bytes:
|
||||||
"""Adjusts values before serialization."""
|
"""Adjusts values before serialization."""
|
||||||
if proto_type in [
|
if proto_type in (
|
||||||
TYPE_ENUM,
|
TYPE_ENUM,
|
||||||
TYPE_BOOL,
|
TYPE_BOOL,
|
||||||
TYPE_INT32,
|
TYPE_INT32,
|
||||||
TYPE_INT64,
|
TYPE_INT64,
|
||||||
TYPE_UINT32,
|
TYPE_UINT32,
|
||||||
TYPE_UINT64,
|
TYPE_UINT64,
|
||||||
]:
|
):
|
||||||
return encode_varint(value)
|
return encode_varint(value)
|
||||||
elif proto_type in [TYPE_SINT32, TYPE_SINT64]:
|
elif proto_type in (TYPE_SINT32, TYPE_SINT64):
|
||||||
# Handle zig-zag encoding.
|
# Handle zig-zag encoding.
|
||||||
return encode_varint(value << 1 if value >= 0 else (value << 1) ^ (~0))
|
return encode_varint(value << 1 if value >= 0 else (value << 1) ^ (~0))
|
||||||
elif proto_type in FIXED_TYPES:
|
elif proto_type in FIXED_TYPES:
|
||||||
@ -840,18 +840,18 @@ class Message(ABC):
|
|||||||
) -> Any:
|
) -> Any:
|
||||||
"""Adjusts values after parsing."""
|
"""Adjusts values after parsing."""
|
||||||
if wire_type == WIRE_VARINT:
|
if wire_type == WIRE_VARINT:
|
||||||
if meta.proto_type in [TYPE_INT32, TYPE_INT64]:
|
if meta.proto_type in (TYPE_INT32, TYPE_INT64):
|
||||||
bits = int(meta.proto_type[3:])
|
bits = int(meta.proto_type[3:])
|
||||||
value = value & ((1 << bits) - 1)
|
value = value & ((1 << bits) - 1)
|
||||||
signbit = 1 << (bits - 1)
|
signbit = 1 << (bits - 1)
|
||||||
value = int((value ^ signbit) - signbit)
|
value = int((value ^ signbit) - signbit)
|
||||||
elif meta.proto_type in [TYPE_SINT32, TYPE_SINT64]:
|
elif meta.proto_type in (TYPE_SINT32, TYPE_SINT64):
|
||||||
# Undo zig-zag encoding
|
# Undo zig-zag encoding
|
||||||
value = (value >> 1) ^ (-(value & 1))
|
value = (value >> 1) ^ (-(value & 1))
|
||||||
elif meta.proto_type == TYPE_BOOL:
|
elif meta.proto_type == TYPE_BOOL:
|
||||||
# Booleans use a varint encoding, so convert it to true/false.
|
# Booleans use a varint encoding, so convert it to true/false.
|
||||||
value = value > 0
|
value = value > 0
|
||||||
elif wire_type in [WIRE_FIXED_32, WIRE_FIXED_64]:
|
elif wire_type in (WIRE_FIXED_32, WIRE_FIXED_64):
|
||||||
fmt = _pack_fmt(meta.proto_type)
|
fmt = _pack_fmt(meta.proto_type)
|
||||||
value = struct.unpack(fmt, value)[0]
|
value = struct.unpack(fmt, value)[0]
|
||||||
elif wire_type == WIRE_LEN_DELIM:
|
elif wire_type == WIRE_LEN_DELIM:
|
||||||
@ -915,10 +915,10 @@ class Message(ABC):
|
|||||||
pos = 0
|
pos = 0
|
||||||
value = []
|
value = []
|
||||||
while pos < len(parsed.value):
|
while pos < len(parsed.value):
|
||||||
if meta.proto_type in [TYPE_FLOAT, TYPE_FIXED32, TYPE_SFIXED32]:
|
if meta.proto_type in (TYPE_FLOAT, TYPE_FIXED32, TYPE_SFIXED32):
|
||||||
decoded, pos = parsed.value[pos : pos + 4], pos + 4
|
decoded, pos = parsed.value[pos : pos + 4], pos + 4
|
||||||
wire_type = WIRE_FIXED_32
|
wire_type = WIRE_FIXED_32
|
||||||
elif meta.proto_type in [TYPE_DOUBLE, TYPE_FIXED64, TYPE_SFIXED64]:
|
elif meta.proto_type in (TYPE_DOUBLE, TYPE_FIXED64, TYPE_SFIXED64):
|
||||||
decoded, pos = parsed.value[pos : pos + 8], pos + 8
|
decoded, pos = parsed.value[pos : pos + 8], pos + 8
|
||||||
wire_type = WIRE_FIXED_64
|
wire_type = WIRE_FIXED_64
|
||||||
else:
|
else:
|
||||||
@ -1264,7 +1264,7 @@ class _Duration(Duration):
|
|||||||
def delta_to_json(delta: timedelta) -> str:
|
def delta_to_json(delta: timedelta) -> str:
|
||||||
parts = str(delta.total_seconds()).split(".")
|
parts = str(delta.total_seconds()).split(".")
|
||||||
if len(parts) > 1:
|
if len(parts) > 1:
|
||||||
while len(parts[1]) not in [3, 6, 9]:
|
while len(parts[1]) not in (3, 6, 9):
|
||||||
parts[1] = f"{parts[1]}0"
|
parts[1] = f"{parts[1]}0"
|
||||||
return f"{'.'.join(parts)}s"
|
return f"{'.'.join(parts)}s"
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user