perf(ext/napi): use threadpool for async work instead of spawning threads#32776
Merged
bartlomieju merged 1 commit intodenoland:mainfrom Mar 16, 2026
Merged
Conversation
…eads Replace `std::thread::spawn` with `deno_core::unsync::spawn_blocking` (tokio's blocking threadpool) in `napi_queue_async_work`. This reuses threads from a pool instead of creating a new OS thread per call, reducing overhead especially on Linux where thread creation via clone() is expensive. Fixes denoland#32773 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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
std::thread::spawnwithdeno_core::unsync::spawn_blocking(tokio's blocking threadpool) innapi_queue_async_workclone()is expensiveuv_queue_workthreadpoolContext
PR #32560 fixed a deadlock bug by moving
executecallbacks to worker threads viastd::thread::spawn. This created a performance regression (#32773) because every async NAPI call spawns and tears down a fresh OS thread. The tokio blocking threadpool keeps threads alive for reuse (default 10s keep-alive), avoiding per-call thread creation overhead.Benchmark (macOS aarch64, napi-rs async add)
On macOS the steady-state is similar since thread creation is lightweight. On Linux (reporter's platform),
std::thread::spawnis significantly more expensive due toclone()syscall cost — the reporter measured 62µs → 258µs regression.Test plan
cargo test --test integration -- napi— all NAPI tests pass (debug + release)Closes #32773
🤖 Generated with Claude Code