Skip to content

perf: 10x dynamic route optimization — handler probe, radix tree#38

Merged
Nadhila-dot merged 2 commits intomainfrom
optimize/dynamic-route-perf
Mar 31, 2026
Merged

perf: 10x dynamic route optimization — handler probe, radix tree#38
Nadhila-dot merged 2 commits intomainfrom
optimize/dynamic-route-perf

Conversation

@layer78
Copy link
Copy Markdown
Collaborator

@layer78 layer78 commented Mar 31, 2026

Major performance overhaul for parameterized route matching and dispatch:

Router (rsrc/src/router.rs):

  • Replace HashMap<MethodKey, > with [Option<>; 7] array-indexed dispatch
  • Add freeze() to sort radix tree children for binary search (>4 children)
  • Iterative match_path for unambiguous trees (no recursion/backtracking)
  • SIMD-accelerated segment splitting via memchr
  • Stack-allocated param values [&str; 8] instead of Vec

Dispatch pipeline (rsrc/src/lib.rs):

  • std::str::from_utf8 instead of from_utf8_lossy for path conversion
  • Inline normalize_runtime_path fast-path at call sites
  • Single-pass path traversal check (one byte scan vs 4 memmem calls)
  • Single-pass header selection with exact capacity pre-computation
  • memchr for '?' detection in envelope builders

Handler pre-evaluation (src/index.js):

  • probeHandlerForFastPath() calls handler with mock req/res at registration
  • Resolves closure variables to literals in synthetic handler source
  • Enables Rust dynamic fast-path for handlers like: res.json({ id: req.params.id, engine: closureVar, mode: 'dynamic' })
  • Eliminates Rust→JS→Rust bridge crossing for resolvable handlers

Rishi Yadav added 2 commits March 31, 2026 13:12
…o-bridge fast-path

Major performance overhaul for parameterized route matching and dispatch:

Router (rsrc/src/router.rs):
- Replace HashMap<MethodKey, _> with [Option<_>; 7] array-indexed dispatch
- Add freeze() to sort radix tree children for binary search (>4 children)
- Iterative match_path for unambiguous trees (no recursion/backtracking)
- SIMD-accelerated segment splitting via memchr
- Stack-allocated param values [&str; 8] instead of Vec

Dispatch pipeline (rsrc/src/lib.rs):
- std::str::from_utf8 instead of from_utf8_lossy for path conversion
- Inline normalize_runtime_path fast-path at call sites
- Single-pass path traversal check (one byte scan vs 4 memmem calls)
- Single-pass header selection with exact capacity pre-computation
- memchr for '?' detection in envelope builders

Handler pre-evaluation (src/index.js):
- probeHandlerForFastPath() calls handler with mock req/res at registration
- Resolves closure variables to literals in synthetic handler source
- Enables Rust dynamic fast-path for handlers like:
    res.json({ id: req.params.id, engine: closureVar, mode: 'dynamic' })
- Eliminates Rust→JS→Rust bridge crossing for resolvable handlers
Examples:
01 - Hello World (basic server, createApp, res.json)
02 - Route Params (:param syntax, req.params, multi-param)
03 - HTTP Methods (GET, POST, PUT, PATCH, DELETE, req.json)
04 - Middleware (app.use, path-scoped, next())
05 - Error Handling (app.error, app.404, custom errors)
06 - CORS (cors middleware, origins, credentials, preflight)
07 - Route Groups (app.group, nested prefixes, API versioning)
08 - Query Params (req.query, multi-value arrays)
09 - Request Body (req.json, req.text, req.body, req.arrayBuffer)
10 - Native Cache (res.ncache, route-level cache option, LRU)
11 - Sessions (session middleware, req.session, HMAC cookies)
12 - Streaming (res.stream, chunked transfer, SSE)
13 - Response Types (res.send, res.type, res.set, res.locals)
14 - Validation (validate middleware, body/params/query schemas)
15 - Optimizations (static/dynamic fast-path, opt, runtime cache)

All examples use @http-native/core imports and JSDoc comments.
@Nadhila-dot Nadhila-dot merged commit 1a6620c into main Mar 31, 2026
2 checks passed
@Nadhila-dot Nadhila-dot deleted the optimize/dynamic-route-perf branch April 1, 2026 14:47
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.

2 participants