Commit 1d581d2
perf: defer MissingFieldError construction and avoid JSON.stringify in cache diff (#13329)
Fixes #13305.
## Problem
Two expensive operations run eagerly on the cache-diff hot path even
when the caller only checks `diff.complete` (a cheap boolean):
1. **`JSON.stringify(objectOrReference, null, 2)`** — pretty-prints the
full parent object for every missing-field message, scaling with object
size.
2. **`new MissingFieldError(...)`** — constructs an `Error` subclass
(paying V8 stack capture) per incomplete diff, even though
`diff.missing` is only consumed in `__DEV__`-guarded logging on most
call paths.
## Solution
- **Cheap messages:** Replaced the `JSON.stringify` with a `__typename`
lookup. Embedded parents now produce `"object Profile"` instead of the
full pretty-printed object dump.
- **Lazy errors:** `diffQueryAgainstStore` now derives `diff.complete`
from the raw `execResult.missing` tree (a `MissingTree`). The
`MissingFieldError` is built lazily via a getter, only when
`diff.missing` is actually accessed. The getter caches the result so
repeated access is cheap.
All three consumers of `diff.missing` are safe — two are
`__DEV__`-guarded, one is a legitimate `watchFragment` consumer. The hot
`broadcastWatch` path only compares `diff.result` with `equal()`, so the
getter is never triggered by property enumeration.
## Changes
| File | Change |
|------|--------|
| `src/cache/inmemory/readFromStore.ts` | Cheap missing-field message +
lazy `MissingFieldError` getter |
| `src/cache/inmemory/__tests__/readFromStore.ts` | 2 new tests + 3
existing assertions updated |
| `src/cache/inmemory/__tests__/diffAgainstStore.ts` | 1 assertion
updated |
| `src/cache/inmemory/__tests__/policies.ts` | 1 assertion updated |
| `.changeset/lazy-diff-diagnostics.md` | Patch-level changeset |
### Checklist:
- [x] Includes a changeset
- [x] Significant new logic is covered by tests
- [ ] ~~New feature~~ (not applicable — performance fix for existing
behavior)
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
- **Performance**
- Improved cache diff performance by avoiding unnecessary serialization
of stored objects.
- Missing-field diagnostics are now generated only when requested.
- **Bug Fixes**
- Improved missing-field error messages with shorter, more relevant
object descriptions.
- Cache completeness is now reported accurately without requiring full
error construction.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
---------
Co-authored-by: DeepSeek V4 Pro agent <agent@atlarix.dev>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Jerel Miller <jerelmiller@gmail.com>1 parent 880eae8 commit 1d581d2
3 files changed
Lines changed: 154 additions & 18 deletions
File tree
- .changeset
- src/cache/inmemory
- __tests__
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1612 | 1612 | | |
1613 | 1613 | | |
1614 | 1614 | | |
1615 | | - | |
| 1615 | + | |
1616 | 1616 | | |
1617 | 1617 | | |
1618 | 1618 | | |
1619 | | - | |
1620 | | - | |
| 1619 | + | |
| 1620 | + | |
1621 | 1621 | | |
1622 | 1622 | | |
1623 | 1623 | | |
| |||
2178 | 2178 | | |
2179 | 2179 | | |
2180 | 2180 | | |
| 2181 | + | |
| 2182 | + | |
| 2183 | + | |
| 2184 | + | |
| 2185 | + | |
| 2186 | + | |
| 2187 | + | |
| 2188 | + | |
| 2189 | + | |
| 2190 | + | |
| 2191 | + | |
| 2192 | + | |
| 2193 | + | |
| 2194 | + | |
| 2195 | + | |
| 2196 | + | |
| 2197 | + | |
| 2198 | + | |
| 2199 | + | |
| 2200 | + | |
| 2201 | + | |
| 2202 | + | |
| 2203 | + | |
| 2204 | + | |
| 2205 | + | |
| 2206 | + | |
| 2207 | + | |
| 2208 | + | |
| 2209 | + | |
| 2210 | + | |
| 2211 | + | |
| 2212 | + | |
| 2213 | + | |
| 2214 | + | |
| 2215 | + | |
| 2216 | + | |
| 2217 | + | |
| 2218 | + | |
| 2219 | + | |
| 2220 | + | |
| 2221 | + | |
| 2222 | + | |
| 2223 | + | |
| 2224 | + | |
| 2225 | + | |
| 2226 | + | |
| 2227 | + | |
| 2228 | + | |
| 2229 | + | |
| 2230 | + | |
| 2231 | + | |
| 2232 | + | |
| 2233 | + | |
| 2234 | + | |
| 2235 | + | |
| 2236 | + | |
| 2237 | + | |
| 2238 | + | |
| 2239 | + | |
| 2240 | + | |
| 2241 | + | |
| 2242 | + | |
| 2243 | + | |
| 2244 | + | |
| 2245 | + | |
| 2246 | + | |
| 2247 | + | |
| 2248 | + | |
| 2249 | + | |
| 2250 | + | |
| 2251 | + | |
| 2252 | + | |
| 2253 | + | |
| 2254 | + | |
| 2255 | + | |
| 2256 | + | |
| 2257 | + | |
| 2258 | + | |
| 2259 | + | |
| 2260 | + | |
| 2261 | + | |
| 2262 | + | |
| 2263 | + | |
| 2264 | + | |
| 2265 | + | |
| 2266 | + | |
| 2267 | + | |
| 2268 | + | |
| 2269 | + | |
| 2270 | + | |
| 2271 | + | |
| 2272 | + | |
| 2273 | + | |
| 2274 | + | |
| 2275 | + | |
| 2276 | + | |
| 2277 | + | |
| 2278 | + | |
| 2279 | + | |
| 2280 | + | |
| 2281 | + | |
| 2282 | + | |
| 2283 | + | |
| 2284 | + | |
| 2285 | + | |
| 2286 | + | |
| 2287 | + | |
| 2288 | + | |
| 2289 | + | |
| 2290 | + | |
| 2291 | + | |
| 2292 | + | |
| 2293 | + | |
| 2294 | + | |
| 2295 | + | |
| 2296 | + | |
| 2297 | + | |
| 2298 | + | |
| 2299 | + | |
| 2300 | + | |
| 2301 | + | |
| 2302 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
224 | 224 | | |
225 | 225 | | |
226 | 226 | | |
227 | | - | |
228 | | - | |
229 | | - | |
230 | | - | |
231 | | - | |
232 | | - | |
233 | | - | |
234 | | - | |
235 | | - | |
| 227 | + | |
| 228 | + | |
236 | 229 | | |
237 | | - | |
238 | | - | |
| 230 | + | |
239 | 231 | | |
240 | 232 | | |
241 | 233 | | |
| |||
246 | 238 | | |
247 | 239 | | |
248 | 240 | | |
249 | | - | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
250 | 252 | | |
251 | 253 | | |
252 | 254 | | |
| |||
338 | 340 | | |
339 | 341 | | |
340 | 342 | | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
341 | 348 | | |
342 | 349 | | |
343 | | - | |
344 | | - | |
345 | | - | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
346 | 353 | | |
347 | 354 | | |
348 | 355 | | |
| |||
0 commit comments