perf: skip the Ajv schema lookup on every anyOf/oneOf/if validation #926
All Vouch review confidence checks passed
Residual risk 0.19 = Change Risk (0.48) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.48 · 500 lines diff across 6 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.372
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #926"
general
weight: 0.35
gurgunday
Proposal:
anyOf/oneOf/ifschema once per serializer, instead of looking it up on every serialization.Problem:
For
anyOf,oneOfandif/then/elsethe generated code callsvalidator.validate(schemaRef, value)to pick the branch. That goes throughajv.validate(ref, data). Ajv compiles each schema once and keeps the validation function, but it looks that function up again on every call: it normalizes the ref with a regular expression, reads it from itsschemas/refsregistries, and after the validation copies the errors toajv.errors. On small values that lookup costs more than the validation itself.This is the path taken by every nullable property written as
anyOf: [{ type: 'string' }, { type: 'null' }], which is what TypeBox emits forType.Union([Type.String(), Type.Null()]).Change:
index.js: each schema ref used by the generated code gets one variable in the serializer scope. The call site becomes(validate_0 || (validate_0 = validator.getSchemaValidator(ref)))(value), so the function is fetched on first use and then called directly. A ref validated in several places shares one variable.lib/validator.js: newgetSchemaValidator(schemaRef), which returns the function fromajv.getSchema()and throws the sameno schema with key or referror asajv.validate()when the ref is unknown.validate()is unchanged.lib/standalone.js: the inlined validator object exposes the same method.Schemas are still compiled lazily, on first use;
compileValidatorsand the standalone modes work as before. The output does not change.Tests:
test/compile-validators.test.js: nothing is looked up at build time, each schema is looked up once after eleven serializations, a shared schema uses one variable, andgetSchemaValidatorthrows on an unknown ref. The existing assertions on the generated code are updated to the new call.Benchmark:
Apple Silicon, default mechanism on both sides, median ops/sec over 15 alternating rounds, output consumed with
Buffer.byteLength.Node.js 26.12.0 (V8 14.6):
| Case | fast-json-stringify v7.3.5 (default mechanism) | This PR (default mechanism) | Difference |
|---|---|---|---|
|
anyOf[string, null] x2, list of 50 objects | 67.3k | 102.0k | +51.6% ||
anyOf[string, null], single object | 5.93M | 7.34M | +23.8% ||
oneOfof two objects, list of 50 | 177.9k | 314.4k | +76.8% ||
if/then/else, single object | 5.99M | 7.50M | +25.2% || control: user profile response | 498.2k | 502.2k | +0.8% |
Node.js 24.22.0 (V8 13.6):
| Case | fast-json-stringify v7.3.5 (default mechanism) | This PR (default mechanism) | Difference |
|---|---|---|---|
|
anyOf[string, null] x2, list of 50 objects | 64.1k | 95.8k | +49.4% ||
anyOf[string, null], single object | 5.78M | 7.21M | +24.9% ||
oneOfof two objects, list of 50 | 163.9k | 271.3k | +65.6% ||
if/then/else, single object | 6.35M | 8.04M | +26.5% || control: user profile response | 494.6k | 504.5k | +2.0% |
Node.js 22.23.1 (V8 12.4):
| Case | fast-json-stringify v7.3.5 (default mechanism) | This PR (default mechanism) | Difference |
|---|---|---|---|
|
anyOf[string, null] x2, list of 50 objects | 60.5k | 93.7k | +55.0% ||
anyOf[string, null], single object | 5.90M | 7.47M | +26.6% ||
oneOfof two objects, list of 50 | 155.3k | 275.0k | +77.1% ||
if/then/else, single object | 6.51M | 8.53M | +31.0% || control: user profile response | 652.7k | 654.7k | +0.3% |
The last row is a control: the
user profileschema ofbenchmark/fixtures/api-responseshas noanyOf/oneOf/if, so its generated code does not change.Note:
Validator#validateis kept, so standalone files generated by earlier versions keep working.objectMechanism: 'native'fix: the two PRs touch different files and can be merged in any order.