Repository navigation
perf(scan): replace rayon with a spin-then-park scan pool - #67
Merged
Merged
Conversation
📊 Benchmark Comparison Results
➖ 98 benchmarks within noise (click to expand)
Legend: ✅ Faster (>5%) · |
Waking rayon's workers costs 15-40 us (median) per parallel scan, which set the parallel thresholds at MiB sizes and made early-match first() scans slower than serial. The scan pool behind par::map_chunks and find_first_chunk replaces it: - Idle workers spin for 50 us, then park, so back-to-back scans start in ~3 us. - The caller works on its own job, and workers claim chunks dynamically. - A job lives on the caller's stack. Workers register before reading it, and the caller unpublishes it and waits only for registered workers, never for parked ones. - One job runs at a time: concurrent or nested callers run their chunks inline. - Chunk panics are caught and re-raised in the caller. - A forked child starts its own pool. - HEXHAMMING_NUM_THREADS sets the size (RAYON_NUM_THREADS is honored for compatibility, 1 disables it). Threads start on the first scan that splits. - Chunks are sized so a scan at the threshold gets 8 of them, up to 4 per thread. Completion is counted once per participant and the claim counter has its own cache line: tiny chunks with per-chunk counters cost ~12 us of contention. rayon is no longer a dependency (criterion still uses it for benches). With the cheaper dispatch, thresholds drop from 4 MiB to 512 KiB for block scanners (0.8x at 256 KiB, 1.4-1.7x at 512 KiB, 2.5x at 1 MiB) and from 1 MiB to 64 KiB for generic widths (2.0-3.5x at 128 KiB). The Catalog planner's parallel-speedup model is refitted to match. Interleaved A/B (Apple M4 Max, CPython 3.14, rayon layer -> pool): best/0.5MBx16 nomatch 6.66 -> 4.80 us 1.39x best/1MBx16 nomatch 13.2 -> 4.08 us 3.23x best/2MBx16 nomatch 26.2 -> 6.86 us 3.81x best/4MBx16 nomatch 49.3 -> 11.8 us 4.19x best/8MBx64 nomatch 76.2 -> 19.6 us 3.88x best/16MBx16 nomatch 116 -> 45.1 us 2.56x best/64MBx16 nomatch 314 -> 269 us 1.17x (memory bound) first/16MBx16 match@25% 72.8 -> 14.4 us 5.04x best/0.5MBx20 nomatch (generic) 79.2 -> 13.6 us 5.80x many_best/100q x 1024x16 21.8 -> 7.73 us 2.82x many_first/1000q x 16384x16 518 -> 398 us 1.30x calls and scans under 512 KiB: unchanged (+-3%) Tests: the pool keeps order, propagates panics and stays usable after one, serves 8 concurrent callers with nested jobs, and survives 2000 back-to-back jobs that cross the spin/park boundary. It ran 40x clean in a stress loop and clean under Miri (data-race and aliasing checks, 4 threads). A new CI job runs these tests under ThreadSanitizer and Miri on Linux. Python tests cover scans in a forked child and HEXHAMMING_NUM_THREADS=1/3. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: c7edd725-5714-4044-acf4-c9afc6ec8cf8
SDE cannot follow the execve of the Python subprocess in test_num_threads_environment_variable (PreparePindForFollowExecve fails). The test still runs natively on every platform, and the fork test runs under emulation. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: c7edd725-5714-4044-acf4-c9afc6ec8cf8
Profiling the scan pool with empty chunks showed a job cost ~3 us even
with 2 chunks: every job made all 15 workers do contended
read-modify-writes on one shared `users` counter (join and leave) and on
`completed`, whether or not the job had work for them. At the parallel
threshold that was most of the scan: a 512 KiB scan spent ~4.7 us of its
~5 us in dispatch.
* A job only draws in as many workers as it has chunks beyond the
caller's; the rest see the new generation and sit it out without
touching shared state.
* Workers flag that they have joined on their own cache line instead of
bumping a shared counter. The caller clears the job pointer and waits
for each flag, which keeps the SeqCst join/leave protocol intact.
* The `completed` counter is gone: once the caller's own claim loop
ends every chunk is claimed, and a worker's chunks are done when its
flag clears.
Dispatch cost, spinning workers, best of 5 x 20000 calls:
chunks empty chunks 1 us chunks
2 2.96 -> 0.42 us 2.56 -> 1.55 us
8 2.94 -> 1.46 us 4.12 -> 2.67 us
16 4.72 -> 2.95 us 5.10 -> 3.79 us
64 9.43 -> 7.78 us 11.12 -> 8.79 us
Scans, interleaved, best of 3:
0.5 MiB x 16 B best 4.13 -> 2.43 us (1.70x), first 1.72x, all 1.50x
0.5 MiB x 64 B 1.61-1.99x
1-2 MiB 1.04-1.36x
4-64 MiB, and generic widths: within noise, checked against a second
build of the previous pool as a control
Tried and dropped: letting each participant claim the chunk of its own
number through its own cache line. Empty 16-chunk jobs got faster
(2.95 -> 1.60 us), but with real work they got slower (16 x 1 us:
3.79 -> 4.80 us), since a late-joining worker's chunk waits until the
caller takes it over.
Miri (8 seeds) and 20x stress runs are clean; a new test alternates
small and large jobs so workers left out of one must rejoin the next.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: c7edd725-5714-4044-acf4-c9afc6ec8cf8
mrecachinas
force-pushed
the
perf/scan-pool
branch
from
September 23, 2026 11:59
11d5067 to
55ba5f5
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Waking rayon's workers costs 15–40 µs (median) per parallel scan. That pushed the parallel thresholds up to MiB sizes, and made
first()slower than serial when the match comes early.This PR replaces rayon with a small spin-then-park pool behind the same
par::map_chunks/find_first_chunkinterface from #66.Design
HEXHAMMING_NUM_THREADSsets the size.RAYON_NUM_THREADSis still honored for compatibility, and1disables the pool. Threads start on the first scan that splits.rayon is no longer a library dependency (criterion still uses it for benches).
Thresholds. With cheaper dispatch, splitting pays off much earlier, so the thresholds are re-measured:
Interleaved A/B (Apple M4 Max, CPython 3.14, #66 → this PR):
best0.5 MB×16best1 MB×16best2 MB×16best4 MB×16best8 MB×64best16 MB×16best64 MB×16 (memory-bound)first16 MB×16, match @25%best0.5 MB×20 (generic)many_best100q × 1024×16Calls and scans under 512 KiB are unchanged (±3%). This table predates the dispatch-cost commit below, which is faster again near the threshold.
Dispatch cost. Profiling the pool with empty chunks showed about 3 µs per job even with 2 chunks. Every job made all 15 workers do contended read-modify-writes on a shared
userscounter (on join and leave) and oncompleted. At 512 KiB that was most of the scan. The second commit keeps uninvolved workers out and moves join/leave to per-worker cache lines. Spinning workers, best of 5 × 20000 calls:End to end, interleaved: 0.5 MiB × 16 B scans are 1.50–1.72× faster, 0.5 MiB × 64 B 1.61–1.99×, and 1–2 MiB 1.04–1.36×. Larger scans and generic widths are within noise; that was checked against a second build of the previous pool used as a control. At the thresholds the parallel median beats serial: 512 KiB × 16 B takes 3.3–3.7 µs against 6.7–6.9 µs.
A sweep with lower thresholds (128 KiB / 16 KiB) did not justify moving them. Below 512 KiB, splitting wins on best-case times but loses on medians (0.15–0.96×) for block-scanner widths, and gains little (0.85–1.33×) for generic widths.
Tried and dropped: letting each participant claim the chunk of its own number through its own cache line. Empty 16-chunk jobs got faster (2.95 → 1.60 µs), but with real work they got slower (16 × 1 µs: 3.79 → 4.80 µs), because a late-joining worker's chunk waits for the caller.
Tests
pool-sanitizers, runs these tests under ThreadSanitizer and Miri on Linux. (TSan's runtime is broken on this Mac, so CI is its first run.)HEXHAMMING_NUM_THREADS=1/3.Review focus: the job hand-off in
src/par.rs: the per-workeractiveflags,helpers, and the unpublish protocol inPool::run.