Skip to content

Optimize slcan serial read and buffer processing - #2080

Open
YeWenxuan64 wants to merge 6 commits into
hardbyte:mainfrom
YeWenxuan64:slcan_read_perf_improve
Open

YeWenxuan64 wants to merge 6 commits into
hardbyte:mainfrom
YeWenxuan64:slcan_read_perf_improve

Conversation

@YeWenxuan64

@YeWenxuan64 YeWenxuan64 commented Jul 25, 2026 •

Copy link
Copy Markdown

Refactor reading from serial to improve performance by using C level's bytearray.find() to replace for iteration.

Summary of Changes

Key changes:

  • Removed the Python-level for i, byte in enumerate(buffer): if byte in (ok, err): ... loop
  • Replaced with C-level bytearray.find calls for terminator detection; the ERROR search is bounded to the bytes before the first OK terminator, so a large backlog of queued frames stays fast

Related Issues / Pull Requests

  • None

Type of Change

  • Bug fix
  • New feature
  • Documentation update
  • Refactoring
  • Other (please describe):

Checklist

  • I have followed the contribution guide.
  • I have added or updated tests as appropriate.
  • I have added or updated documentation as appropriate.
  • I have added a news fragment for towncrier.
  • All checks and tests pass (tox).

Additional Notes

Method vs bytearray.find (v5)
v1 for loop ~2–4× slower
v2 single-pass local ~2–4× slower
v3 filter+next ~3–6× slower
v4 bytes.find ~1.5–2× slower (copy overhead)
v5 bytearray.find 1.0× (baseline)

I could also provide performance testing scripts (generate by AI) if you need.

@mergify

mergify Bot commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue).

  • Queue this pull request

@YeWenxuan64

Copy link
Copy Markdown
Author

A benchmark generate by AI
benchmark_read_methods.py

@YeWenxuan64

Copy link
Copy Markdown
Author

@Mergifyio queue

@mergify

mergify Bot commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

queue

☑️ Command disallowed due to command restrictions in the Mergify configuration.

Details
  • sender-permission >= write

Refactor reading from serial to improve performance by using C level's bytearray.find() to replace `for` iteration.
@zariiii9003
zariiii9003 force-pushed the slcan_read_perf_improve branch from 984cc3a to c936fd9 Compare November 22, 2026 14:33
@zariiii9003

Copy link
Copy Markdown
Collaborator

AI Review:

Thanks, this is a worthwhile speedup. I checked that it behaves the same as before (_OK/_ERROR are single bytes, so the earlier of the two find() results is the same terminator the old loop found), and the SLCAN tests pass. Measured end to end, bus.recv() on an in-memory serial mock gets about 35% faster: 5.6 → 3.5 µs per frame, and with a 2,000-frame backlog 5.1 → 3.5 µs.

Two things before this can go in:

1. It is slower than main when a large backlog builds up. Each recv() returns one frame and keeps the rest in self._buffer. Since \a is rare, self._buffer.find(self._ERROR) scans the entire remaining buffer on every call, so its cost grows with the backlog. With 20,000 queued frames, recv() takes 8.1 µs on this branch against 5.3 µs on main, so an application that has fallen behind catches up more slowly than today. An ERROR byte only matters if it comes before the first OK byte, so limiting the second search avoids the regression:

end = self._buffer.find(self._OK)
error = self._buffer.find(self._ERROR, 0, end if end >= 0 else len(self._buffer))

2. CI lint fails. Please run black and ruff check --fix on can/interfaces/slcan.py, and add a changelog fragment (doc/changelog.d/2080.changed.rst).

With those two fixed, I'm happy to merge.

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).
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.
@YeWenxuan64

Copy link
Copy Markdown
Author

Both points are addressed:

1. Backlog regression — the ERROR search is now bounded to the bytes before the first OK terminator, since an ERROR byte only matters if it comes before the first OK. With 20,000 queued frames (355 KB) recv() takes 0.42 µs on this branch vs 1.92 µs on main, so the backlog case is now faster than main as well. (commit 03613c2)

2. Lint — ran black and ruff on can/interfaces/slcan.py (ruff FURB136 rewrote the if/else to min(), noted in a comment) and added doc/changelog.d/2080.changed.rst. black --check, ruff check, pylint (10.00/10), mypy and the slcan unittest suite all pass locally. (commit 95e7974)

@zariiii9003 zariiii9003 left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you, linter is still failing though

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.
@YeWenxuan64

Copy link
Copy Markdown
Author

😅Let me retry again……

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.

2 participants