Skip to content

perf: round-3 optimizations — dispatch, static, extract, observe hot paths - #31

Merged
aarambh-darshan merged 1 commit into
mainfrom
perf/round3-core-hot-path
Aug 26, 2026
Merged

aarambh-darshan merged 1 commit into
mainfrom
perf/round3-core-hot-path

Conversation

@aarambh-darshan

Copy link
Copy Markdown
Member

Summary

Round 3 of the optimization campaign (tracked in the local audit backlog). This PR targets everything outside arvik-middleware: the dispatch path, static-file serving, extractors, and metrics. Follows #28 (round 2) and #30 (middleware).

Router — dispatch unboxing (C2/C3)

RouterService::call used to clone an Arc, box a large async state machine, and re-box again at the erasure boundary. Dispatch is now split into:

  • route() — synchronous: radix-trie match, param interning, extension inserts, returns the leaf future + pattern
  • one single Box::pin at the leaf, with layered and non-layered paths sharing it

Removes one pure-waste heap alloc per request plus two atomic RMWs.

Static files (arvik-static)

Finding Change
C7a/b .br/.gz probes are now direct open attempts (a miss costs one failed open instead of stat+open pairs) in both ServeDir and ServeFile; plain files validate via the already-open descriptor; canonical root cached in OnceCell
C8 Embedded serving checks O(1) A::get before the O(all-assets) directory-prefix scan
C9 relative_path = one sized String with inline traversal validation (was PathBuf + Vec + N component Strings + join); duplicate field dropped
C10 Hot MIME types return HeaderValue::from_static; table verified byte-for-byte against mime_guess 2.0.5 by unit test
C12 Fallback services behind Arc<BoxCloneService> → service clone is a refcount bump
C14 Embedded ETag/mtime cached per asset path (was hex-formatted + validated every request)

New regression tests: traversal/normalization semantics for relative_path, content-type table parity vs mime_guess.

Metrics (arvik-observe) — C11

  • status label borrowed from StatusCode::as_str() (canonical zero-padded form) — kills the per-request format!
  • route label refcounts the interned Arc<str> instead of to_string()
  • exact-size response bodies record straight from the size hint — no CountingBody, no atomic, no callback allocation for any Content-Length response; only genuinely streamed bodies get wrapped

Core / extract

  • C5: constant Content-Types use HeaderValue::from_static across into_response.rs, ResponseBuilder helpers, and JSON responses (~9 sites) — was malloc+memcpy+validation per response
  • C15: JSON serialization hint 128→512 B; application/json;charset=utf-8 fast path before Mime parse; BufWriter between upload chunks and temp files; targeted multipart boundary extraction (replaces multer's full Mime parse; quoted/case/first-wins semantics covered by tests); cookie-jar memoization shared across all three jar extractors
  • C1 documented in-code: the erased-handler re-box is inherent while H::Future is a GAT (elimination tracked for the future trait migration)

Verification

  • ✅ cargo fmt --all -- --check
  • ✅ RUSTFLAGS="-D warnings" cargo clippy --workspace --all-features --all-targets
  • ✅ cargo test --workspace --all-features
  • Benchmarks will be re-run after merge to quantify plain-route / static-route deltas against BENCHMARK_REPORT.md baselines.

Deferred (intentional)

  • C4 (Arc'd baked stacks), C6 (single extensions map), O1 (handler trait migration) — architectural, next round.

…paths

Router (C2/C3): split dispatch into a synchronous `route()` head (trie
match, param interning, extension insert) plus a single boxed leaf
future, removing the double-box at the Tower erasure boundary and the
Arc RMW + large state-machine box in `RouterService::call`.

Static files:
- C7a/b: precompressed `.br`/`.gz` probes are now direct open attempts
  (one failed open instead of stat+open pairs) for ServeDir and
  ServeFile; plain files validate through the already-open descriptor,
  and the canonical serve root is cached in a OnceCell instead of a
  per-request realpath walk.
- C8: embedded serving checks the O(1) `A::get` before the O(assets)
  directory-prefix scan.
- C9: `relative_path` builds one sized string with inline traversal
  validation; the duplicate PathBuf field is gone.
- C10: hot MIME types resolve to 'static header values (table mirrors
  mime_guess 2.0.5; parity + traversal regression tests added).
- C12: fallback services live behind Arc so the per-request service
  clone is a refcount bump.
- C14: hash-derived embedded metadata (ETag/mtime) is cached per asset.

Metrics (C11): status/method labels borrowed from canonical 'static
forms, route label refcounts the interned Arc, exact-size response
bodies record from the size hint without any counting wrapper.

Core/extract: C5 replaces per-response HeaderValue::from_str on
constant content types with from_static across into_response,
response builder helpers, and JSON responses. C15 quick wins: larger
JSON serialization hint, `application/json;charset=utf-8` fast path,
BufWriter between upload chunks and temp files, targeted multipart
boundary extraction (+ tests), cookie-jar memoization shared by all
three jar extractors.

Gates: cargo fmt --check, clippy --workspace --all-features
-all-targets -D warnings, cargo test --workspace --all-features —
all green locally.
@aarambh-darshan
aarambh-darshan merged commit 29feb50 into main Aug 26, 2026
5 checks passed
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.

1 participant