ReviewOS

stacks/bun-router

perf(router): identify enriched default 404 responses

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

Problem

Frameworks that add fallback context to generic 404 responses cannot distinguish bun-router's already enriched default 404 from a user-created JSON 404 without cloning and consuming the response body. In the Stacks production profile, that downstream clone, stream read, and exact-body comparison remain a measurable part of every missing-route request.

Proposed change

Export a stable symbol from the root and runtime entries and mark bun-router's enriched default 404 responses with it. Downstream frameworks can return a marked response immediately while retaining their body-inspection fallback for custom or older router responses.

Acceptance criteria

  • Mark enriched default 404 responses from both router serving implementations.
  • Preserve status, body, headers, global middleware, fallback, 405, and error behavior.
  • Export the marker from the narrow runtime entry and package root.
  • Add deterministic tests for marked default 404s and unmarked user responses.
  • Benchmark downstream elimination of response cloning on exact Bun 1.4.2.
  • Keep stable package publication pending explicit release approval.
9 days ago

Implemented and measured on the exact Bun 1.4.2 release binary.

Downstream Stacks production-handler benchmark

Both benchmark clones use bun-router 0.1.19 and the same downstream symbol check. The baseline package leaves default 404s unmarked; the candidate marks them. Each operation runs the full Stacks and bun-router missing-route path and consumes the response body.

16 alternating pairs, 20,000 operations per sample, 2,000-operation warmup:

  • Baseline: 211,935.40 operations/s
  • Candidate: 253,386.98 operations/s
  • Throughput: +19.56%
  • Elapsed time: -16.36%
  • Candidate wins: 15/16
  • 95% paired elapsed-time CI: -21.49 ms to -9.38 ms
  • Benchmark SHA-256: 1e950b481d94639238647d854f1cab8b37cabcbf80840024aee9b18d546c735b

bun-router marker overhead control

16 alternating pairs, 50,000 missing-route operations per sample, 5,000-operation warmup:

  • Baseline: 688,322.76 operations/s
  • Candidate: 684,977.08 operations/s
  • Point estimate: -0.49% throughput
  • Candidate wins: 6/16
  • 95% paired elapsed-time CI: -1.32 ms to +2.03 ms
  • Benchmark SHA-256: cd8fc4f570116374280f85d7a74b7feae373431bb32346242bf82fd7b7c99f1e

The marker write is neutral within measurement noise, while downstream code that recognizes it avoids cloning and reading an already enriched response.

A separate 12-pair, 100,000-request HTTP cohort trended +1.48% but had a wide interval and is excluded from the improvement claim.

Validation

  • Full suite: 1,014 pass, 0 fail, 3,074 assertions across 62 files
  • Standard and registry TypeScript projects: pass
  • Full pickier lint: pass
  • Package build and CLI compile: pass
  • Root and runtime entries export the same global symbol
  • Router default enriched 404s are marked; user-created 404 responses remain unmarked

Stable package publication remains pending explicit release approval under #899.

Sign in to comment on this issue.