Skip to content

perf(app-core): speed up router dispatch - #865

Merged
trueadm merged 1 commit into
mainfrom
fix/faster-router-dispatch
Aug 26, 2026
Merged

trueadm merged 1 commit into
mainfrom
fix/faster-router-dispatch

Conversation

@jonkwheeler

@jonkwheeler jonkwheeler commented Aug 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Match static app routes with exact string comparisons instead of running a regular expression for every candidate.
  • Normalize the request method at most once per dispatch instead of once for every server-route candidate.
  • Add a correctness-gated router benchmark, ratio guards, and MCP benchmark catalog exposure.

Why

  • The router was doing avoidable allocation and RegExp work in its hottest scan loop even when a route had no parameters.
  • In the same 1,000-route harness, the stable before/after scores dropped from 31.127 to 4.813 ms per million static candidates (84.5%) and from 22.200 to 4.737 ms per million wrong-method candidates (78.7%). The dynamic-route control remains on the existing RegExp path.

Changes

  • Compile parameter-free paths to exact strings while preserving capturing RegExp matching for params and catch-alls.
  • Lazily cache the uppercased request method on the first server-route candidate.
  • Cover root, literal RegExp-special-character, decoded parameter, catch-all, lowercase method, and method-miss behavior.
  • Register router-dispatch in the benchmark harness and MCP server, with same-process ratio guards against the dynamic control.

Validation

  • pnpm format:check (not run; scoped formatting passed)
  • pnpm typecheck (not run; scoped app-core typecheck passed)
  • pnpm test (not run; targeted suites passed)
  • targeted tests: app-core — 10 files, 113 tests passed; MCP server — 1 file, 15 tests passed
  • node benchmarks/bench.mjs --quick --ratios router-dispatch — 2/2 ratio guards passed
  • CI=true pnpm sync
  • git diff --check

Risk / follow-ups

  • Static matching remains exact and dynamic parameter decoding is unchanged. The benchmark correctness gates and regression test cover both paths; no follow-up is required.

Provenance

  • An agent produced this diff (agent-authored)

View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.


Note

Low Risk
Localized hot-path optimization in route matching with explicit tests and benchmarks; dynamic matching and param decoding behavior are unchanged.

Overview
Speeds up @octanejs/app-core request routing by avoiding per-candidate RegExp work on static paths and by uppercasing the HTTP method at most once per match() call when scanning server routes.

Router: Parameter-free paths compile to an exact string and compare with pathname === pattern; routes with params or catch-alls still use capturing RegExp. Server-route method checks reuse a lazily computed normalizedMethod instead of calling toUpperCase() on every candidate.

Regression coverage: Adds a Node-only router-dispatch benchmark (1,000-route tables: static hit, wrong-method miss, dynamic params) with local baselines, ratio guards vs the dynamic path, harness registration, and router-dispatch in the Octane MCP octane_benchmark catalog. New Vitest coverage locks static, param, catch-all, literal special characters, and method matching behavior.

Reviewed by Cursor Bugbot for commit a51c7fd. Bugbot is set up for automated code reviews on this repo. Configure here.

@github-actions github-actions Bot added agent-authored Diff or issue body substantially produced by an AI agent perf PR: performance work labels Aug 26, 2026
@jonkwheeler
jonkwheeler marked this pull request as ready for review August 26, 2026 18:32
@trueadm
trueadm merged commit 9dda682 into main Aug 26, 2026
31 of 46 checks passed
@trueadm
trueadm deleted the fix/faster-router-dispatch branch August 26, 2026 18:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent-authored Diff or issue body substantially produced by an AI agent perf PR: performance work

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants