Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve plain_text frame helper performance #506

Closed
wants to merge 6 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions aioesphomeapi/_frame_helper/plain_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,20 @@ def data_received(self, data: bytes) -> None: # pylint: disable=too-many-branch
msg_type_int = maybe_msg_type
else:
# Message type is longer than 1 byte
msg_type = bytes(init_bytes[2:3])
msg_type = init_bytes[2:3]
else:
# Length is longer than 1 byte
length = bytes(init_bytes[1:3])
length = init_bytes[1:3]
# If the message is long, we need to read the rest of the length
while length[-1] & 0x80 == 0x80:
add_length = self._read_exactly(1)
if add_length is None:
return
length += add_length
length_int = bytes_to_varuint(length)
length_int = bytes_to_varuint(bytes(length))
# Since the length is longer than 1 byte we do not have the
# message type yet.
msg_type = b""
msg_type = bytearray()

# If the we do not have the message type yet because the message
# length was so long it did not fit into the first byte we need
Expand All @@ -102,7 +102,7 @@ def data_received(self, data: bytes) -> None: # pylint: disable=too-many-branch
if add_msg_type is None:
return
msg_type += add_msg_type
msg_type_int = bytes_to_varuint(msg_type)
msg_type_int = bytes_to_varuint(bytes(msg_type))

if TYPE_CHECKING:
assert length_int is not None
Expand All @@ -119,6 +119,8 @@ def data_received(self, data: bytes) -> None: # pylint: disable=too-many-branch
# at the start of the frame.
if packet_data_bytearray is None:
return
# It would be better to not convert to bytes at all.
# https://github.com/protocolbuffers/protobuf/issues/10774
packet_data = bytes(packet_data_bytearray)

end_of_frame_pos = self._pos
Expand Down