Skip to content

perf(silkd): memchr the session sentinel scan (~26x)#16

Merged
CMGS merged 1 commit into
mainfrom
perf/session-memchr
Jul 9, 2026
Merged

perf(silkd): memchr the session sentinel scan (~26x)#16
CMGS merged 1 commit into
mainfrom
perf/session-memchr

Conversation

@CMGS

@CMGS CMGS commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

A persistent-shell (session) command's output is delimited by an unpredictable __SILK_<32hex>__ sentinel. converse scanned each accumulated ~32KB output chunk for it with a hand-rolled haystack.windows(needle.len()).position(|w| w == needle) byte-walk, once per read. For a session command with large output (a build, a verbose log), that scan runs on every 32KB chunk.

Measurement (isolated, M2 Max)

Scanning a 32KB no-match buffer for the 41-byte marker:

µs/scan
naive windows().position() 37.8 µs
memchr::memmem::find 1.4 µs
ratio ~26x

37.8µs/32KB is more than the base64 encode of the same bytes (~15µs) — so on a large-output session command the sentinel scan was a larger per-chunk cost than the base64 the perf sweep had assumed dominated. For a 200MB session command (~6100 chunks) the scans alone drop from ~230ms to ~9ms.

Change

Replace the naive find with memchr::memmem::find (SIMD first-byte skipping); memchr was already in the lock tree via regex, now a direct dep. Only the session path uses this scan — plain exec is unaffected (it frames on the exit protocol, not a sentinel).

Gates

  • macOS: fmt --check ✓, clippy --all-targets -D warnings ✓, cargo test ✓.
  • Linux (native arm64 rust:1-slim container, fixtures + git): clippy ✓, cargo test ✓ (18/18 incl. the session round-trip tests that drive converse).

converse scanned each ~32KB accumulated output chunk for the unpredictable
command marker with a naive windows().position() byte-walk. That is ~37.8us per
32KB scan — more than the base64 encode of the same bytes — and it runs once per
read, so a large-output session command (a build, a verbose log) pays it on
every chunk. memchr::memmem::find does the same search with SIMD first-byte
skipping at ~1.4us/32KB (~26x). Only the session path uses this scan; plain exec
is unaffected.
@CMGS CMGS merged commit 7e4b5e6 into main Jul 9, 2026
1 check passed
@CMGS CMGS deleted the perf/session-memchr branch July 9, 2026 16:54
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.

1 participant