Pool worker fibers and coalesce reactor wake-ups#12
Merged
Conversation
Two fixed-cost reductions motivated by the boundary benchmark: - Worker fibers are pooled: after finishing a request a fiber parks itself back into the pool and is resumed with the next job, instead of being created and GC'd per request. Pushing before yielding is safe because the single-threaded VM cannot pop the fiber until the yield suspends it; dead fibers are skipped on pop. - Reactor wake-ups are coalesced: nsgi_handle only writes a wake byte when it flips the wake flag false to true, and the reactor resets the flag when a pop finds the queue empty. Both transitions happen under the queue mutex, so a producer that skips the write is guaranteed its job is seen by the drain loop the in-flight wake triggers. Benchmark impact (Apple M2, 12-header baseline): 8-thread throughput 161k to 336k req/s (6.2 to 3.0 us/req), single-threaded 60k to 99k req/s, allocations 12 to 10 per request. Scaling stays flat in header count and body size, moving the crossover against the idealized handoff-free Rack harness from ~60 request headers down to ~22, inside the 15-25 range of ordinary browser traffic. Mock host suite and leaks remain clean. Co-Authored-By: Claude Fable 5 <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
Two fixed-cost reductions motivated by the boundary benchmark (#11):
nsgi_handleonly writes a wake byte when it flips the wake flag false to true; the reactor resets the flag when a pop finds the queue empty. Both transitions happen under the queue mutex, so a producer that skips the write is guaranteed its job is seen by the drain loop the in-flight wake triggers (no lost-wakeup window).Benchmark impact (Apple M2, 12-header baseline)
Scaling stays flat in header count and body size (60 headers: 339k req/s; 16 KB body: 338k req/s; 10 allocations in every scenario), which puts the crossover against the handoff-free idealized Rack harness inside the 15-25 header range of ordinary browser traffic. bench/README.md updated with the new matrix.
Verification
leaks --atExit: 0 leaks.🤖 Generated with Claude Code