Repository navigation
fix: objectMechanism 'native' keeps no data between calls - #940
Merged
Merged
Conversation
2 tasks done
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.
Proposal:
objectMechanism: 'native'no longer keeps data in memory between calls.Problem:
objectMechanism: 'native'copies the input into scratch objects that are reused between calls. They are never released: they keep the values of the last call, and the pool of an array grows to the longest array seen and never shrinks. A serializer that once writes a list of 100,000 rows keeps about 35 MB for as long as it lives, also when it then serves 10 rows. In a Fastify application with 300 routes, a few large responses take the heap from 26 MB to 163 MB, and it does not come back.This is acceptable for an option that is set by hand. It is not for a default, which is what is being asked for Fastify v6 in fastify/fastify#6507.
A second problem: the
JSON.stringifyfast path of V8 handles objects with up to 127 properties. From 128 the option is 34% to 38% slower than the default mechanism.Change:
lib/projection.js: each call builds new objects with the declared properties and drops them when it returns. Nothing is shared between calls, so there is no scratch, no pool, and no code for a call that starts while another one is running.JSON.stringifythrows.__proto__is now written, as the default mechanism does. Onmainit was dropped.objectMechanismsection is updated.Tests:
nullroot, and a call made inside another one with a BigInt in the outer call, in the inner one and in both.__proto__, in the schema and in the input.Benchmark:
Node v26.13.0, Apple Silicon.
Memory kept by the serializers after the call, with the input dropped and after a full garbage collection:
Heap of a Fastify application with 300 routes, after each phase of traffic:
Speed, ops/sec, each measurement in its own process:
Objects with many properties, ops/sec:
What the tables say:
Note:
lib/projection.js, not a small fix: reusing the objects was the cause of the memory kept, and it turned out not to pay in speed.