fix: objectMechanism 'native' keeps no data between calls #940
All Vouch review confidence checks passed
Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.52 · 650 lines diff across 8 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.388
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #940"
general
weight: 0.35
gurgunday
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:
| Scenario | fast-json-stringify v7.3.10 (default mechanism) | fast-json-stringify v7.3.10 (objectMechanism native) | This PR (objectMechanism native) |
|---|---|---|---|
| one serializer, a list of 100,000 rows once | 0.28 MB | 35.21 MB | 0.18 MB |
| same, then a list of 10 rows (the next request) | 0.03 MB | 34.44 MB | 0.03 MB |
| one serializer, an object with a 20 MB string once | 0.01 MB | 20.01 MB | 0.01 MB |
| same, then a short string (the next request) | 0.00 MB | 0.00 MB | 0.00 MB |
| 1,000 serializers, each called once with a list of 20 rows | 0.53 MB | 6.08 MB | 0.70 MB |
| 1,000 serializers, each called once with a list of 50 rows | 0.40 MB | 14.56 MB | 0.62 MB |
| 1,000 serializers, each called once with a list of 200 rows | 0.40 MB | 52.23 MB | 0.62 MB |
| 1,000 serializers, each called once with a list of 1,000 rows | 0.00 MB | 258.68 MB | 0.62 MB |
Heap of a Fastify application with 300 routes, after each phase of traffic:
| Serializer | After the ordinary traffic | After the pages of 500 rows | After the exports of 20,000 rows | Back to the ordinary traffic |
|---|---|---|---|---|
| fast-json-stringify v7.3.10 (default mechanism) | 21.5 MB | 23.1 MB | 23.3 MB | 25.8 MB |
| fast-json-stringify v7.3.10 (objectMechanism native) | 23.1 MB | 48.7 MB | 164.2 MB | 163.4 MB |
| This PR (objectMechanism native) | 22.3 MB | 24.0 MB | 24.1 MB | 26.6 MB |
Speed, ops/sec, each measurement in its own process:
| Case | JSON.stringify | fast-json-stringify v7.3.10 (default mechanism) | fast-json-stringify v7.3.10 (objectMechanism native) | This PR (objectMechanism native) | Difference | This PR vs default mechanism | This PR vs JSON.stringify |
|---|---|---|---|---|---|---|---|
| one object, 8 properties | 6.04M | 3.59M | 5.37M | 5.71M | +6.4% | +59.2% | -5.5% |
| order with 5 items (nested objects and arrays) | 1.63M | 671.7k | 1.38M | 1.40M | +1.5% | +107.8% | -14.5% |
| 50 rows x 8 columns | 203.8k | 67.8k | 177.2k | 184.1k | +3.9% | +171.8% | -9.6% |
| 1000 rows x 8 columns | 9.5k | 3.6k | 8.9k | 9.3k | +5.0% | +161.4% | -1.8% |
| 10000 rows x 8 columns | 1.0k | 0.3k | 0.9k | 0.9k | +7.5% | +205.9% | -4.2% |
| 20 orders with 5 items each | 91.8k | 33.5k | 78.6k | 80.1k | +1.9% | +139.1% | -12.7% |
| object with an array of 1000 strings | 121.3k | 93.7k | 108.1k | 113.9k | +5.4% | +21.6% | -6.0% |
| object with an array of 1000 integers | 263.1k | 215.5k | 238.7k | 222.6k | -6.7% | +3.3% | -15.4% |
Objects with many properties, ops/sec:
| Case | JSON.stringify | fast-json-stringify v7.3.10 (default mechanism) | fast-json-stringify v7.3.10 (objectMechanism native) | This PR (objectMechanism native) | Difference | This PR vs default mechanism |
|---|---|---|---|---|---|---|
| object, 64 properties | 1.51M | 514.5k | 1.14M | 1.12M | -2.2% | +116.8% |
| object, 100 properties | 1.02M | 333.2k | 741.5k | 721.6k | -2.7% | +116.6% |
| object, 127 properties | 805.0k | 256.4k | 578.2k | 563.5k | -2.5% | +119.8% |
| object, 128 properties | 167.6k | 214.2k | 140.4k | 215.8k | +53.7% | +0.8% |
| object, 300 properties | 68.1k | 89.7k | 56.5k | 90.2k | +59.8% | +0.6% |
| object, 500 properties | 39.7k | 54.4k | 33.5k | 54.8k | +63.6% | +0.8% |
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.