Skip to content

perf: cache compiled request validators - #448

Draft
pr1m8 wants to merge 1 commit into
mainfrom
wa/cache-request-validators
Draft

pr1m8 wants to merge 1 commit into
mainfrom
wa/cache-request-validators

Conversation

@pr1m8

@pr1m8 pr1m8 commented Nov 17, 2026

Copy link
Copy Markdown

Why

Repeatedly interpreting recursive request schemas can block the event loop for large inputs. Compile each request schema once per server and reuse its validator.

What changed

  • Cache compiled validators by schema identity in a server-local WeakMap.
  • Apply the cache to request initialization, streamed request data, and the existing v1.1 compatibility checks. Keep control-message validation and detailed error generation unchanged.
  • Add boundary tests for cache reuse, recursive inputs, malformed requests, custom byte/date refinements, and streamed data.
  • Add a reproducible synthetic benchmark.

Validation

  • npm run check: passed (types, formatting, lint).
  • npm run test:single: 822 passed, 1 skipped across 38 files.
  • npm run build: passed.
  • npm run bench -- --run __tests__/request-validation.bench.ts: passed.

Local benchmark on Node 24.21.0 / TypeBox 1.1.38, using the committed 10,000-record synthetic fixture:

Validator Mean per check Samples
Interpreted 299.59 ms 3
Compiled and cached 2.6559 ms 189

This measures validation only, not end-to-end RPC latency. The compiled validator was constructed before measurement; first-use compilation is not included. The boundary tests verify cache reuse across requests. Validation and decoding remain synchronous, and this does not change payload limits. Text evidence is sufficient for this non-visual server optimization.

Versioning

  • Breaking protocol change
  • Breaking ts/js API change

~ written by ⠕ Replit

This branch has not been deployed

No deployments
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