Repository navigation
perf: walk schema objects without a WeakSet in assertObjectDepth - #934
Merged
Merged
Conversation
build() walks every external schema with assertObjectDepth, and Fastify
passes all shared schemas to every route build. The walk allocated two
{ value, depth, exit } entries and did WeakSet add/has/delete for each
node.
Keep the same iterative depth-first walk and the same errors, but hold
the pending nodes in two arrays and the ancestors in a path array that
is cut to the current depth. The depth limit keeps the path short.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This makes Fastify startup 8-9% faster for an app with 200 routes and 40 shared schemas. The checks and the errors do not change.
Why
Fastify compiles one serializer for each response schema. Each time, it passes all shared schemas (
app.addSchema) inoptions.schema.build()makes a newRefResolverfor each call, so it walks every shared schema again withassertObjectDepth(index.js:487). For 200 routes with 2 status codes and 40 shared schemas, that is 16,000 walks at startup. In a CPU profile of that app (Node 24, median of 7 runs),assertObjectDepthtook 138 ms of the 789 ms inbuild(), about 17%. With this change it takes 91 ms.For each node, the walk allocated 2
{ value, depth, exit }entries, and it did a WeakSetadd,hasanddelete.What changed
assertObjectDepthkeeps the same iterative depth-first walk, in the same order. Two arrays (values,depths) hold the pending nodes. An arraypathholds the ancestors of the current node, cut to the current depth. A node that is already inpathis a cycle. The depth limit (maxDepth * 2) keepspathat 201 entries or fewer, so the scan stays cheap.The walk stays iterative, so the stack-safety fix from #874 is kept. A shared sub-object that is not a cycle is still accepted. The two error messages, and the node where each one is thrown, are the same.
Numbers
Apple M3 Pro,
Fastify()toready(), fastify 5.12.8 with@fastify/fast-json-stringify-compiler, separate processes, old and new in alternating order, median of 21 runs (11 runs for the 10-route app on Node 26 and the app without shared schemas):On Node 26, every new run was faster than every old run. The worst cases are not slower: a chain of depth 199 with 200k leaves takes 51-54 ms on main and 37-40 ms with this change.
The repo bench measures only serialization, so I used a small script. Each shared schema has about 12 properties, a nested object and a
$refto the one before it. Each route has a 200 response with 2$refs and a 404 response.Script
Tests
test/schema-depth.test.js: shared sub-objects are accepted, a cycle through an array is rejected, the depth error for an external schema has its name, and a function as the schema still givesschema is invalid. The test also passes onmain.$refkeys, roots that are not objects,maxDepth0 to 5). The result, the error and the error order were the same every time.npm testpasses with 100% coverage, and lint is clean.Related: #874 (added
assertObjectDepth), #900 (addedhasRef), #506 (slow cold start with many shared schemas).I used Claude (Opus 5.5) to write and benchmark this change. I checked the results and the reasoning before opening it.
Checklist
npm run test && npm run benchmark --if-presentand the Code of conduct