Repository navigation
Optimize slcan serial read and buffer processing - #2080
YeWenxuan64 wants to merge 6 commits into
Conversation
|
Tick the box to add this pull request to the merge queue (same as
|
|
A benchmark generate by AI |
|
@Mergifyio queue |
☑️ Command disallowed due to command restrictions in the Mergify configuration.Details
|
Refactor reading from serial to improve performance by using C level's bytearray.find() to replace `for` iteration.
984cc3a to
c936fd9
Compare
AI Review:Thanks, this is a worthwhile speedup. I checked that it behaves the same as before ( Two things before this can go in: 1. It is slower than 2. CI lint fails. Please run 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.
|
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) |
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.
|
😅Let me retry again…… |
Refactor reading from serial to improve performance by using C level's bytearray.find() to replace
foriteration.Summary of Changes
Key changes:
for i, byte in enumerate(buffer): if byte in (ok, err): ...loopbytearray.findcalls for terminator detection; the ERROR search is bounded to the bytes before the first OK terminator, so a large backlog of queued frames stays fastRelated Issues / Pull Requests
Type of Change
Checklist
tox).Additional Notes
bytearray.find(v5)I could also provide performance testing scripts (generate by AI) if you need.