Skip to content

[Prototype] Reuse one dispatch fiber and batch await continuations (follow-up to #6) - #8

Draft
jbaczuk-qualia wants to merge 1 commit into
jbaczuk/fiber-await-v18from
jbaczuk/fiber-await-v18-reuse
Draft

jbaczuk-qualia wants to merge 1 commit into
jbaczuk/fiber-await-v18from
jbaczuk/fiber-await-v18-reuse

Conversation

@jbaczuk-qualia

@jbaczuk-qualia jbaczuk-qualia commented Oct 2, 2026 •

Copy link
Copy Markdown

Summary

Optional follow-up to #6 (stacked on it; merge #6 first). Needs qualialabs/node#9. Instead of a new hidden fiber per dispatched job, keep one long-lived hidden dispatch fiber, and after each job let it run the dispatchable jobs queued right behind it, so a chain of awaits in fibered code costs one switch onto the fiber and one back instead of two per await.

File Change
fibers_async.js reusingDispatcher: runs jobs on the dispatch fiber and, while it is still the dispatch fiber, keeps calling Fiber.__runNextDispatchable(). A job that blocks keeps the fiber (it's parked inside that job) and the next dispatch starts a new one, the same trick as znewsham's drain fiber in this repo's #7. A parked fiber that is later resumed never pulls in other jobs. A dispatch that arrives while the dispatch fiber itself is running (nested drain) falls back to a one-off fiber. FIBERS_AWAIT_REUSE=0 (or a node without node#9) gives the #6 behaviour; Fiber.microtasksBatched counts jobs run without a switch
src/fibers.cc Fiber.__runNextDispatchable(), defined only when dlsym finds v8_qualia_RunNextDispatchableMicrotask

Testing

On #6's node 18 build plus node#9, arm64 dev-qualia container:

  • My standalone checks: 11/11 with reuse on and off (same 45 jobs: 27 dispatched + 18 batched with reuse, 45 dispatched without).
  • znewsham's test-microtask-fibers.js (with Fiber.current swapped for the running fiber, poolSize = 1e9): same as on [Prototype] Run await continuations from fibers in hidden fibers (needs node#7) #6, 7/7 behaviour tests pass including "a parked microtask does not stall other promises" and "resuming a parked fiber does not drain unrelated microtasks mid-callback"; the 100k park/resume cycle test still exceeds its 10 s timeout (~9.7 s for the cycles alone; every park needs its own fiber).
  • meteor-promise suite: now 15/15 with native Promise too (the extra fiber that [Prototype] Run await continuations from fibers in hidden fibers (needs node#7) #6 caused in should not double-wrap callbacks is gone).
  • test/: unchanged (pool.js/cleanup.js pthread coro_destroy segfault as on stock).
  • Local qli5 qualia boots; startup hook that awaits then blocks works; fiberless code still throws Can't wait without a fiber.
#6 #6 + this
chain of awaits in a fiber ~25–30 us 0.13 us (stock: 0.05–0.07 us)
I/O-style await in a fiber ~33 us ~28 us (stock: ~1.1 us; ~20 us is the pthread run+yield floor)
drains / awaits in code that never had a fiber unchanged unchanged

🤖 Generated with Claude Code

Instead of a new hidden fiber per dispatched job, keep one long-lived
hidden dispatch fiber. After each job it runs the dispatchable jobs
queued right behind it (Fiber.__runNextDispatchable, needs the matching
node patch), so a chain of awaits in fibered code costs one switch onto
the fiber and one back instead of two per await. A job that blocks keeps
the fiber and the next dispatch starts a new one; a fiber that was
parked and later resumed never pulls in other jobs.

FIBERS_AWAIT_REUSE=0 restores one new fiber per job.
Fiber.microtasksBatched counts jobs run without a switch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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