ReviewOS

stacks/bun-router

perf(router): finalize synchronous middleware responses directly

#906
Closed chris opened this 9 days ago ·
9 days ago

Problem

_dispatchMatchedRoute() now receives synchronous responses from fully synchronous middleware chains, but it still sends every middleware-bearing route through finishAsyncMatchedResponse(). The finalizer is declared async, so a synchronous middleware response regains a Promise boundary after the chain compiler removed the inner ones.

Proposed change

Finalize a synchronous middleware response directly. Keep promised responses on finishAsyncMatchedResponse(), preserve null-to-500 behavior, cookie application, synchronous and asynchronous custom error handling, middleware short circuits, and public builder overrides.

Acceptance criteria

  • Keep cached route dispatch synchronous when the handler and every middleware layer finish synchronously.
  • Preserve promised middleware, rejection, custom error, cookie, and empty-response behavior.
  • Add deterministic regression coverage for synchronous and asynchronous middleware dispatch.
  • Improve representative cached route and downstream Stacks request benchmarks on exact Bun 1.4.2.
  • Pass the complete suite, typechecks, lint, and package build.
  • Keep stable package publication pending explicit release approval.
9 days ago

Implemented and validated on exact Bun 1.4.2.

Commits:

The matched-route dispatcher now finalizes completed synchronous middleware responses directly. Promised results stay on the asynchronous finalizer. Public middleware builder overrides are normalized to same-realm Promises, so cross-realm Promises remain asynchronous, and cookie finalization failures remain outside middleware error recovery so custom error handlers run once.

Isolated cached dispatch benchmark, 16 alternating fresh-process pairs at 100,000 operations per process:

  • Synchronous middleware dispatch: 2,494,715 to 4,138,246 ops/s, +65.88% throughput, -39.72% elapsed time, candidate won 16/16 pairs.
  • Paired elapsed delta 95% CI: -18.587 ms to -13.253 ms per 100,000 operations.
  • Asynchronous control: +2.99% point estimate, candidate won 12/16 pairs, paired elapsed CI -4.479 ms to +0.484 ms. This control is statistically neutral.
  • Artifact: /Users/chris/.cache/codex-bench/bun-router/906/comparison.json
  • SHA-256: 5ce6dd2df7ecd95f3845f312a38d1b91a45a5991777541c7c07fae2ceb6ce855

Downstream Stacks benchmark, registered synchronous route with the real production maintenance middleware, 16 alternating fresh-process pairs, seven timed 20,000-request batches per process with the median batch selected:

  • 243,215 to 261,974 requests/s, +7.71% throughput, -7.16% elapsed time, candidate won 14/16 pairs.
  • Paired elapsed delta 95% CI: -9.709 ms to -2.067 ms per 20,000 requests.
  • This measures the cached matched-route inner dispatch seam, excluding outer serverResponse work and network HTTP.
  • Artifact: /Users/chris/.cache/codex-bench/bun-router/906/stacks-dispatch-comparison.json
  • SHA-256: 29765e91c102a7461bdbf321af31f580b9443f15b60b7c5e3c71eff4bc411954

Exact final candidate package:

  • 280 KB, 159 files
  • SHA-256: 6dcaba358d011febf177f3ef56d48c38494383c61c7aee6b163a8acf06b948e6

Validation:

  • Full repository suite: 1,029 pass, 0 fail, 3,114 assertions across 63 files.
  • Package suite: 587 pass, 0 fail, 1,810 assertions across 46 files.
  • Lint passed.
  • Both TypeScript projects passed.
  • Package build and CLI compile passed.
  • git diff --check passed.
  • Independent standards and specification reviews are clean.

Stable package publication remains pending explicit release approval.

Sign in to comment on this issue.