From c936fd9d820e4b6542fd6ca5b248447d8c00a925 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B6=E6=96=87=E8=BD=A9?= Date: Sat, 25 Jul 2026 18:07:38 +0800 Subject: [PATCH 1/4] Optimize slcan serial read and buffer processing Refactor reading from serial to improve performance by using C level's bytearray.find() to replace `for` iteration. --- can/interfaces/slcan.py | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/can/interfaces/slcan.py b/can/interfaces/slcan.py index 2ec4df238..ae54f04b2 100644 --- a/can/interfaces/slcan.py +++ b/can/interfaces/slcan.py @@ -219,21 +219,28 @@ def _read(self, timeout: float | None) -> str | None: with error_check("Could not read from serial device"): while True: - # Due to accessing `serialPortOrig.in_waiting` too often will reduce - # the performance. We read the `serialPortOrig.in_waiting` only once here. - size = self.serialPortOrig.in_waiting or 1 - self._buffer.extend(self.serialPortOrig.read(size)) - - for i, byte in enumerate(self._buffer): - if byte in (self._OK[0], self._ERROR[0]): - string = self._buffer[: i + 1].decode() - del self._buffer[: i + 1] - return string - - if _timeout.expired(): - break - - return None + size = self.serialPortOrig.in_waiting or 1 # read the `serialPortOrig.in_waiting` only once. + self._buffer.extend(self.serialPortOrig.read(size)) # read serial at once + + i1 = self._buffer.find(self._OK) # bytearray.find() or byte.find() is in C level + i2 = self._buffer.find(self._ERROR) + + if i1 >= 0 and i2 >= 0: + i = i1 if i1 < i2 else i2 # python if/else branch is faster than min()/max() + elif i1 >= 0: + i = i1 + elif i2 >= 0: + i = i2 + else: + if _timeout.expired(): + break + continue + + string = self._buffer[:i + 1].decode() + del self._buffer[:i + 1] + return string + + return None def flush(self) -> None: self._buffer.clear() From 03613c2dcd8c379623249724e65d1c825ed898a6 Mon Sep 17 00:00:00 2001 From: YeWenxuan64 Date: Mon, 23 Nov 2026 22:00:49 +0800 Subject: [PATCH 2/4] Limit slcan ERROR search to bytes before the first OK terminator An ERROR byte only matters if it comes before the first OK byte, so the unbounded find() scanned the whole backlog on every recv() and made the branch slower than main under backlog (9.3 vs 1.9 us/recv with 20,000 queued frames; bounded find: 0.4 us/recv). --- can/interfaces/slcan.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/can/interfaces/slcan.py b/can/interfaces/slcan.py index ae54f04b2..b4773a0ba 100644 --- a/can/interfaces/slcan.py +++ b/can/interfaces/slcan.py @@ -223,7 +223,9 @@ def _read(self, timeout: float | None) -> str | None: self._buffer.extend(self.serialPortOrig.read(size)) # read serial at once i1 = self._buffer.find(self._OK) # bytearray.find() or byte.find() is in C level - i2 = self._buffer.find(self._ERROR) + # an ERROR byte only matters if it comes before the first OK byte, + # so limit the second search to avoid scanning the whole backlog + i2 = self._buffer.find(self._ERROR, 0, i1 if i1 >= 0 else len(self._buffer)) if i1 >= 0 and i2 >= 0: i = i1 if i1 < i2 else i2 # python if/else branch is faster than min()/max() From 95e7974433966498c8fd29291b5a69b636eaa1a9 Mon Sep 17 00:00:00 2001 From: YeWenxuan64 Date: Mon, 23 Nov 2026 22:10:56 +0800 Subject: [PATCH 3/4] Fix slcan lint: black/ruff formatting and changelog fragment black and ruff reformatted _read (wrapped long lines, stripped trailing whitespace on blank lines, min() per ruff FURB136 - the if/else branch is faster but does not survive the repo lint); added doc/changelog.d/2080.changed.rst. --- can/interfaces/slcan.py | 32 +++++++++++++++++++++----------- doc/changelog.d/2080.changed.rst | 1 + 2 files changed, 22 insertions(+), 11 deletions(-) create mode 100644 doc/changelog.d/2080.changed.rst diff --git a/can/interfaces/slcan.py b/can/interfaces/slcan.py index b4773a0ba..c490bb825 100644 --- a/can/interfaces/slcan.py +++ b/can/interfaces/slcan.py @@ -219,16 +219,26 @@ def _read(self, timeout: float | None) -> str | None: with error_check("Could not read from serial device"): while True: - size = self.serialPortOrig.in_waiting or 1 # read the `serialPortOrig.in_waiting` only once. - self._buffer.extend(self.serialPortOrig.read(size)) # read serial at once - - i1 = self._buffer.find(self._OK) # bytearray.find() or byte.find() is in C level + size = ( + self.serialPortOrig.in_waiting or 1 + ) # read the `serialPortOrig.in_waiting` only once. + self._buffer.extend( + self.serialPortOrig.read(size) + ) # read serial at once + + i1 = self._buffer.find( + self._OK + ) # bytearray.find() or byte.find() is in C level # an ERROR byte only matters if it comes before the first OK byte, # so limit the second search to avoid scanning the whole backlog - i2 = self._buffer.find(self._ERROR, 0, i1 if i1 >= 0 else len(self._buffer)) - + i2 = self._buffer.find( + self._ERROR, 0, i1 if i1 >= 0 else len(self._buffer) + ) + if i1 >= 0 and i2 >= 0: - i = i1 if i1 < i2 else i2 # python if/else branch is faster than min()/max() + i = min( + i2, i1 + ) # python if/else branch is faster than min()/max() but ruff FURB136 rewrites it elif i1 >= 0: i = i1 elif i2 >= 0: @@ -237,11 +247,11 @@ def _read(self, timeout: float | None) -> str | None: if _timeout.expired(): break continue - - string = self._buffer[:i + 1].decode() - del self._buffer[:i + 1] + + string = self._buffer[: i + 1].decode() + del self._buffer[: i + 1] return string - + return None def flush(self) -> None: diff --git a/doc/changelog.d/2080.changed.rst b/doc/changelog.d/2080.changed.rst new file mode 100644 index 000000000..44dd194e5 --- /dev/null +++ b/doc/changelog.d/2080.changed.rst @@ -0,0 +1 @@ +The ``slcan`` interface now finds frame terminators with the C-level ``bytearray.find`` instead of a Python-level byte loop, so ``recv()`` is faster and stays fast even when a large backlog of frames has accumulated in the serial buffer. From ce6260433967c3c7a1b0b780d4a49f92bead8ce2 Mon Sep 17 00:00:00 2001 From: YeWenxuan64 Date: Tue, 24 Nov 2026 16:10:57 +0800 Subject: [PATCH 4/4] Fix pylint C0301: move slcan comment to its own line The trailing comment made the line 102 chars, over pylint's 100-char limit (ruff exempts E501 for can/interfaces/*, black does not wrap comments). As a standalone line it is 98 chars. --- can/interfaces/slcan.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/can/interfaces/slcan.py b/can/interfaces/slcan.py index c490bb825..73a18bd8c 100644 --- a/can/interfaces/slcan.py +++ b/can/interfaces/slcan.py @@ -236,9 +236,8 @@ def _read(self, timeout: float | None) -> str | None: ) if i1 >= 0 and i2 >= 0: - i = min( - i2, i1 - ) # python if/else branch is faster than min()/max() but ruff FURB136 rewrites it + # python if/else branch is faster than min()/max() but ruff FURB136 rewrites it + i = min(i2, i1) elif i1 >= 0: i = i1 elif i2 >= 0: