Commit 0555379
authored
turbo-tasks: task-storage memory wins (#93720)
## Summary
Four small, independent changes that shrink `TaskStorage` and the data
it owns:
Recommend reviewing commit-by-commit
1. **`Arc<CachedTaskType>` → `triomphe::Arc<CachedTaskType>`.**
`triomphe::Arc` is already a workspace dep used in `ReadRef` /
`SharedReference`. `CachedTaskType` never appears in a `Weak<...>`, so
we can drop the weak count and the CAS in `drop_slow`. Saves one `usize`
per allocation. Migrated via a `CachedTaskTypeArc` newtype so the
bincode `Encode`/`Decode` impls don't need to cross the orphan rule.
2. **Niche-encode `CellDependency`.** The `cell_dependencies` /
`cell_dependents` sets used to hold `(CellRef, Option<u64>)` tuples —
`Option<u64>` cost a full 16 B (8 B discriminant + 8 B value, aligned),
making each element 32 B. A `CellDependency` enum with two variants
(`All(CellRef)` / `Hash(CellRef, u64)`) lets the layout algorithm reuse
the niche on `ValueTypeId` (`NonZero<u16>`) inside
`CellRef.cell.type_id` for the variant tag. Element size drops 32 → 24
B; `LazyField` from 56 → 48 B. The same enum backs both forward and
reverse edges — for `cell_dependents` we re-point `CellRef.task` at the
dependent task.
Added `CellDependency::into_parts()` and use it in
`iter_cell_dependents` / `iter_cell_dependencies` hot loops so the
discriminant is checked once instead of twice via back-to-back
`cell_ref()` + `key()` calls.
3. **`TaskStorage::lazy: Vec<LazyField>` → `TinyVec<LazyField>`.** The
lazy vec only ever holds ~25 elements (one per declared lazy field in
the schema). Swapping `Vec`'s 24 B `(ptr, len, cap)` header for `(ptr,
len: u8, cap: u8)` + 6 B padding gives 16 B. Drops
`size_of::<TaskStorage>()` from 136 → 128 B.
`TinyVec` is hand-rolled so I added a push/iter micro-benchmark to
confirm it doesn't lose performance vs std `Vec`. Results below.
4. **Rightsize collections** → Explore the `AutoSet`/`AutoMap` types in
storage_schema and ensure each one is maximally sized for its natural
alignment.
## Benchmark results
### `next build` on a representative app (15 runs each, M4 Pro,
`caffeinate -dimsu nice -n -20`)
Fresh same-day baseline against branch:
| metric | canary | branch | Δ | 95% CI | significant? |
|---|---:|---:|---:|---|:---:|
| wall time | 40.83s | 41.12s | +0.7% | [−1.07s, +1.64s] | no |
| user time | 282.27s | 283.21s | +0.3% | [−1.02s, +2.89s] | no |
| sys time | 69.38s | 71.26s | +2.7% | [−1.54s, +5.32s] | no |
| **MaxRSS** | **12.47 GB** | **12.04 GB** | **−3.4%** | **[−0.48 GB,
−0.38 GB]** | **yes** |
**MaxRSS is the headline.** −0.43 GB on a 12.5 GB working set, with
t=−17.86 (every branch run lower than every canary run, CV ≤ 0.6% on
both sides). Wall / user / sys are all within noise — this PR is a
memory win with no measurable timing impact.
### `TinyVec` vs `Vec` micro-bench (`turbo-tasks/benches/tiny_vec.rs`,
200 samples each)
| n | Vec push | TinyVec push | Δ% | Vec iter | TinyVec iter | Δ% |
|---:|---:|---:|---:|---:|---:|---:|
| 0 | 1.31ns | 894ps | **−31.8%** | 598ps | 596ps | −0.4% |
| 1 | 16.92ns | 14.75ns | **−12.9%** | 964ps | 952ps | −1.2% |
| 4 | 17.93ns | 15.93ns | **−11.1%** | 1.49ns | 1.50ns | +0.5% |
| 8 | 63.13ns | 45.24ns | **−28.3%** | 1.97ns | 1.96ns | −0.2% |
| 16 | 97.35ns | 79.91ns | **−17.9%** | 3.16ns | 3.14ns | −0.5% |
| 24 | 137.41ns | 119.88ns | **−12.8%** | 4.30ns | 4.30ns | +0.0% |
TinyVec push is 11–32% faster than Vec push across all realistic sizes;
iter is identical. Run with `cargo bench -p turbo-tasks --bench
tiny_vec`.
### `task_overhead/turbo` Criterion bench (M4 Pro, `--sample-size 200`)
| variant | dur | canary | branch | Δ | significant? |
|---|---:|---:|---:|---:|:---:|
| turbo-uncached | 1µs | 9.77 µs | 9.68 µs | −1.0% | yes |
| turbo-uncached | 1000µs | 1.01 ms | 1.01 ms | −0.1% | yes |
| turbo-cached-same-keys | 1µs | 198.6 ns | 191.9 ns | −3.4% | yes |
| turbo-cached-same-keys | 100µs | 226.5 ns | 208.1 ns | −8.1% | yes |
| turbo-cached-different-keys | 1µs | 233.8 ns | 224.1 ns | −4.2% | yes
|
| turbo-cached-different-keys | 100µs | 305.3 ns | 246.9 ns | −19.1% |
yes |
| turbo-uncached-parallel | 10µs | 1.63 µs | 1.54 µs | −5.8% | yes |
| turbo-uncached-parallel | 100µs | 8.41 µs | 7.88 µs | −6.3% | yes |
<!-- NEXT_JS_LLM_PR -->1 parent dadc154 commit 0555379
14 files changed
Lines changed: 1094 additions & 155 deletions
File tree
- turbopack/crates
- turbo-tasks-backend/src
- backend
- operation
- turbo-tasks-macros/src/derive
- turbo-tasks
- benches
- src
Lines changed: 33 additions & 30 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
15 | | - | |
| 15 | + | |
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
21 | 24 | | |
22 | | - | |
| 25 | + | |
23 | 26 | | |
24 | | - | |
| 27 | + | |
25 | 28 | | |
26 | 29 | | |
27 | 30 | | |
28 | 31 | | |
29 | 32 | | |
30 | | - | |
| 33 | + | |
31 | 34 | | |
32 | 35 | | |
33 | 36 | | |
34 | 37 | | |
35 | 38 | | |
36 | | - | |
| 39 | + | |
37 | 40 | | |
38 | 41 | | |
39 | 42 | | |
40 | 43 | | |
41 | 44 | | |
42 | | - | |
| 45 | + | |
43 | 46 | | |
44 | 47 | | |
45 | 48 | | |
| |||
80 | 83 | | |
81 | 84 | | |
82 | 85 | | |
83 | | - | |
| 86 | + | |
84 | 87 | | |
85 | 88 | | |
86 | 89 | | |
| |||
138 | 141 | | |
139 | 142 | | |
140 | 143 | | |
141 | | - | |
| 144 | + | |
142 | 145 | | |
143 | | - | |
| 146 | + | |
144 | 147 | | |
145 | 148 | | |
146 | 149 | | |
147 | 150 | | |
148 | 151 | | |
149 | 152 | | |
150 | | - | |
| 153 | + | |
151 | 154 | | |
152 | 155 | | |
153 | 156 | | |
| |||
297 | 300 | | |
298 | 301 | | |
299 | 302 | | |
300 | | - | |
| 303 | + | |
301 | 304 | | |
302 | 305 | | |
303 | 306 | | |
304 | 307 | | |
305 | 308 | | |
306 | 309 | | |
307 | 310 | | |
308 | | - | |
| 311 | + | |
309 | 312 | | |
310 | 313 | | |
311 | 314 | | |
| |||
314 | 317 | | |
315 | 318 | | |
316 | 319 | | |
317 | | - | |
| 320 | + | |
318 | 321 | | |
319 | 322 | | |
320 | 323 | | |
| |||
324 | 327 | | |
325 | 328 | | |
326 | 329 | | |
327 | | - | |
| 330 | + | |
328 | 331 | | |
329 | 332 | | |
330 | 333 | | |
331 | 334 | | |
332 | 335 | | |
333 | 336 | | |
334 | 337 | | |
335 | | - | |
| 338 | + | |
336 | 339 | | |
337 | 340 | | |
338 | 341 | | |
339 | 342 | | |
340 | 343 | | |
341 | 344 | | |
342 | | - | |
| 345 | + | |
343 | 346 | | |
344 | 347 | | |
345 | 348 | | |
346 | 349 | | |
347 | 350 | | |
348 | 351 | | |
349 | 352 | | |
350 | | - | |
| 353 | + | |
351 | 354 | | |
352 | 355 | | |
353 | 356 | | |
354 | 357 | | |
355 | 358 | | |
356 | 359 | | |
357 | 360 | | |
358 | | - | |
| 361 | + | |
359 | 362 | | |
360 | 363 | | |
361 | 364 | | |
362 | 365 | | |
363 | 366 | | |
364 | 367 | | |
365 | 368 | | |
366 | | - | |
| 369 | + | |
367 | 370 | | |
368 | 371 | | |
369 | 372 | | |
370 | 373 | | |
371 | 374 | | |
372 | 375 | | |
373 | | - | |
| 376 | + | |
374 | 377 | | |
375 | 378 | | |
376 | 379 | | |
377 | 380 | | |
378 | 381 | | |
379 | 382 | | |
380 | 383 | | |
381 | | - | |
| 384 | + | |
382 | 385 | | |
383 | 386 | | |
384 | 387 | | |
385 | 388 | | |
386 | 389 | | |
387 | 390 | | |
388 | 391 | | |
389 | | - | |
| 392 | + | |
390 | 393 | | |
391 | 394 | | |
392 | 395 | | |
| |||
395 | 398 | | |
396 | 399 | | |
397 | 400 | | |
398 | | - | |
| 401 | + | |
399 | 402 | | |
400 | 403 | | |
401 | 404 | | |
| |||
404 | 407 | | |
405 | 408 | | |
406 | 409 | | |
407 | | - | |
| 410 | + | |
408 | 411 | | |
409 | 412 | | |
410 | 413 | | |
| |||
413 | 416 | | |
414 | 417 | | |
415 | 418 | | |
416 | | - | |
| 419 | + | |
417 | 420 | | |
418 | 421 | | |
419 | 422 | | |
420 | 423 | | |
421 | 424 | | |
422 | 425 | | |
423 | | - | |
| 426 | + | |
424 | 427 | | |
425 | 428 | | |
426 | 429 | | |
427 | 430 | | |
428 | 431 | | |
429 | 432 | | |
430 | 433 | | |
431 | | - | |
| 434 | + | |
432 | 435 | | |
433 | 436 | | |
434 | 437 | | |
435 | 438 | | |
436 | 439 | | |
437 | 440 | | |
438 | 441 | | |
439 | | - | |
| 442 | + | |
440 | 443 | | |
441 | 444 | | |
442 | 445 | | |
443 | 446 | | |
444 | 447 | | |
445 | 448 | | |
446 | | - | |
| 449 | + | |
447 | 450 | | |
448 | 451 | | |
449 | 452 | | |
| |||
457 | 460 | | |
458 | 461 | | |
459 | 462 | | |
460 | | - | |
| 463 | + | |
461 | 464 | | |
462 | 465 | | |
463 | 466 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
37 | | - | |
38 | | - | |
| 37 | + | |
| 38 | + | |
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
| |||
70 | 70 | | |
71 | 71 | | |
72 | 72 | | |
73 | | - | |
74 | | - | |
| 73 | + | |
| 74 | + | |
75 | 75 | | |
76 | 76 | | |
77 | 77 | | |
| |||
785 | 785 | | |
786 | 786 | | |
787 | 787 | | |
788 | | - | |
| 788 | + | |
| 789 | + | |
789 | 790 | | |
790 | 791 | | |
791 | 792 | | |
| |||
797 | 798 | | |
798 | 799 | | |
799 | 800 | | |
800 | | - | |
801 | | - | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
802 | 804 | | |
803 | 805 | | |
804 | 806 | | |
| |||
1545 | 1547 | | |
1546 | 1548 | | |
1547 | 1549 | | |
1548 | | - | |
| 1550 | + | |
1549 | 1551 | | |
1550 | 1552 | | |
1551 | 1553 | | |
| |||
1776 | 1778 | | |
1777 | 1779 | | |
1778 | 1780 | | |
1779 | | - | |
| 1781 | + | |
1780 | 1782 | | |
1781 | 1783 | | |
1782 | 1784 | | |
| |||
2216 | 2218 | | |
2217 | 2219 | | |
2218 | 2220 | | |
2219 | | - | |
| 2221 | + | |
2220 | 2222 | | |
2221 | 2223 | | |
2222 | 2224 | | |
| |||
2255 | 2257 | | |
2256 | 2258 | | |
2257 | 2259 | | |
2258 | | - | |
| 2260 | + | |
2259 | 2261 | | |
2260 | 2262 | | |
2261 | 2263 | | |
| |||
Lines changed: 16 additions & 15 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
20 | | - | |
| 20 | + | |
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
| |||
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
51 | | - | |
| 51 | + | |
52 | 52 | | |
53 | 53 | | |
54 | 54 | | |
| |||
166 | 166 | | |
167 | 167 | | |
168 | 168 | | |
169 | | - | |
170 | | - | |
171 | | - | |
172 | | - | |
173 | | - | |
174 | | - | |
175 | | - | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
176 | 177 | | |
177 | 178 | | |
178 | | - | |
179 | | - | |
180 | | - | |
181 | | - | |
182 | | - | |
| 179 | + | |
183 | 180 | | |
184 | | - | |
| 181 | + | |
185 | 182 | | |
186 | 183 | | |
187 | 184 | | |
188 | 185 | | |
189 | 186 | | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
190 | 191 | | |
191 | 192 | | |
192 | 193 | | |
| |||
0 commit comments