Avoid one dict lookup when processing packets (#431)

This commit is contained in:
J. Nick Koston 2023-05-08 10:07:51 -05:00 committed by GitHub
parent 9a35b717c2
commit 6b32b7c5a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -651,11 +651,11 @@ class APIConnection:
def _process_packet(self, msg_type_proto: int, data: bytes) -> None: def _process_packet(self, msg_type_proto: int, data: bytes) -> None:
"""Process a packet from the socket.""" """Process a packet from the socket."""
if msg_type_proto not in MESSAGE_TYPE_TO_PROTO: if not (class_ := MESSAGE_TYPE_TO_PROTO.get(msg_type_proto)):
_LOGGER.debug("%s: Skipping message type %s", self.log_name, msg_type_proto) _LOGGER.debug("%s: Skipping message type %s", self.log_name, msg_type_proto)
return return
msg = MESSAGE_TYPE_TO_PROTO[msg_type_proto]() msg = class_()
try: try:
# MergeFromString instead of ParseFromString since # MergeFromString instead of ParseFromString since
# ParseFromString will clear the message first and # ParseFromString will clear the message first and