Skip to content

Changed slcan serial read routine to be faster and to manage the buffer more carefully#2028

Open
aaronfultonnz wants to merge 1 commit intohardbyte:mainfrom
aaronfultonnz:main
Open

Changed slcan serial read routine to be faster and to manage the buffer more carefully#2028
aaronfultonnz wants to merge 1 commit intohardbyte:mainfrom
aaronfultonnz:main

Conversation

@aaronfultonnz
Copy link

Summary of Changes

Changes the serial read in slcan to be faster by reading all available bytes at once rather than one at a time. This is a huge performance boost (at least on windows).

Related Issues / Pull Requests

@@ -219,16 +219,17 @@ def _read(self, timeout: float | None) -> str | None:
# Due to accessing `serialPortOrig.in_waiting` too often will reduce the performance.
# We read the `serialPortOrig.in_waiting` only once here.
in_waiting = self.serialPortOrig.in_waiting
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
in_waiting = self.serialPortOrig.in_waiting
size = self.serialPortOrig.in_waiting or 1

You could remove the if-else by always reading at least one.

if byte:
self._buffer.extend(byte)

for i in range(len(self._buffer)):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for i in range(len(self._buffer)):
for i, byte in enumerate(self._buffer):

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Performance fix for slcan

2 participants