Repository navigation
Conversation
spawn_cpu passed each caller's closure straight to spawn_blocking, so every distinct closure instantiated tokio's blocking-task machinery in the calling crate (399 instantiations in lance). Box the closure and its output and hand them to a non-generic spawn_cpu_erased instead. Co-Authored-By: Claude Opus 5.5 <[email protected]>
wjones127
marked this pull request as ready for review
October 28, 2026 23:37
There was a problem hiding this comment.
Claude Code Review
This repository is configured for manual code reviews. Comment @claude review for a one-time review, or @claude review always to subscribe this PR to a review on every future push.
Tip: disable this comment in your organization's Code Review settings.
Contributor
There was a problem hiding this comment.
Full erasure is reasonable for the measured compile savings. The reported 2.2–4.4% encoder slowdowns remain an unexplained, non-blocking performance risk. Erasing only the closure would avoid the result allocation, while retaining task machinery per result type; that is an alternative if runtime cost becomes more important than maximizing compile savings.
wjones127
added a commit
that referenced
this pull request
Nov 11, 2026
…10716) ## Summary `spawn_cpu` passed each caller's closure straight to `spawn_blocking`, so tokio's blocking-task machinery (task cell, harness, vtable, drop glue) was instantiated once per closure type in every calling crate. This PR puts the closure and a slot for its output in one `Arc<CpuTask<F, T>>` and hands it to a non-generic `spawn_erased(Arc<dyn RunOnce>)`, so that machinery is instantiated once in `lance-core`. ```text spawn_cpu<E, F, R>(func) Arc<CpuTask { func: UnsafeCell<Option<F>>, output: UnsafeCell<Option<Result<R, E>>> }> -> spawn_erased(Arc<dyn RunOnce>) -> JoinHandle<()> // non-generic, only tokio call .map(|_| task.output.take()) // typed, no downcast ``` This revives #10444 with a cheaper design: one allocation per task instead of two, and no `Any` downcast or locks. It follows `std::thread::spawn`, which erases the closure but keeps the result typed. Panics still propagate with their original payload through the `JoinError` (`spawn_cpu_reraises_the_closure_panic`). ## Evidence LLVM IR lines, `cargo llvm-lines --lib`, dev profile. The main baseline was measured at 839b1a5, 85 upstream commits behind the branch's base at measurement time, so the deltas are approximate: | crate | main | PR | Δ | |---|---|---|---| | `lance` | 7,378,036 | 6,848,859 | −7.2% | | `lance-index` | 4,145,525 | 4,041,401 | −2.5% | Runtime benchmarks (the `spawn_cpu` microbenchmark from #10444 and the `lance-encoding` benches that regressed there) are not yet run. ## Merge Risk **Door:** two-way. `spawn_cpu`'s signature and panic behavior are unchanged; no format change. **Blast Radius:** `perf`. Every `spawn_cpu` call pays one allocation and one indirect call. The `UnsafeCell` fields are sequenced by the task lifecycle (worker writes `output`, caller reads it only after the join handle resolves) instead of locked. --------- Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: Xuanwo <[email protected]>
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.
Summary
spawn_cpupassed each caller's closure straight tospawn_blocking, so every distinct closure instantiated tokio's blocking-task machinery (task cell, harness, raw vtable, drop glue) in the calling crate. Inlance, the genericIvfIndexBuilder<S, Q>/IVFIndex<S, Q>matrix turns 24 call sites into 399 instantiations of about 1.8K IR lines each. This PR boxes the closure and its output and hands them to a non-genericspawn_cpu_erased, so that machinery is instantiated once inlance-core. Panic propagation is unchanged (spawn_cpu_reraises_the_closure_panicstill passes).Evidence
Compile cost: dev profile,
CARGO_INCREMENTAL=0, single-crate rebuild. IR fromcargo llvm-lines --lib; time is the rustc-Z time-passestotal.spawn_cpurowsspawn_cpuinstantiationslancelance-index(Two additional
lance-indexbaseline runs taken while the host was loaded, 27.9 and 29.6 s, are excluded.)Runtime: bench profile, main and PR binaries run alternately and pinned with
taskset(4 cores for the first two groups, 8 for FTS) on a 16-vCPU host. Values are mean ± stdev of criterion point estimates.spawn_cpuround trip, no-op closure (ad hoc, below)lance-indexinvert_search(1000000)lance-indexinvert_phrase_search(1000000)lance-encodingencode_compressed/zstd_strings_10colslance-encodingencode_compressed/lz4_strings_10colslance-encodingencode_structural_pages/sparse_boolean_listlance-encodingencode_structural_pages/dense_boolean_listlance-encodingvariable_offsets/delta/v2.3The erasure adds two small allocations and one indirect call per task, about 70 ns on a ~3.4 µs round trip. That cost is invisible once a task does about 10 µs of work, and FTS search, which calls
spawn_cpuper partition per query, shows no measurable change. The two encoder regressions are reproducible, but per-call overhead does not explain them. Each iteration encodes one or two pages (defaultmax_page_bytesis 32 MiB), so there are only one or twospawn_cpucalls, while the gap is about 80 µs. A codegen or inlining shift in the encode path is more likely, but this is not confirmed (perfis unavailable on this host).Ad hoc
spawn_cpumicrobenchmark (not committed)Merge Risk
Door: two-way.
spawn_cpu's signature and panic behavior are unchanged, and there is no format change.Blast Radius:
perf. Everyspawn_cpucaller pays the small per-task cost shown above.🤖 Generated with Claude Code