perf: Share each file's text across its trace errors (#14632)
Stacked on #14627. The numbers below are measured against that branch.
Every import a file trace can't resolve becomes a `TraceError::Resolve`
that carries the whole source text of the importing file, so the report
can show a snippet. The text was copied into each error, so a file with
ten unresolved imports held ten copies of itself. Each copy was then
cloned again when the report was rendered.
A `dependents` query on cal.com, checked out without its dependencies,
reports 22,307 such errors. Those copies come to roughly 110 MB of
duplicated file text, and that dominates the query's peak memory.
This PR stores the text as an `Arc<str>` that is created at most once
per file, and only once the file has an unresolved import. Its errors
share it, so cloning an error for rendering is a reference-count
increment:
```diff
TraceError::Resolve {
span,
- text: String, // a copy of the file per error
+ text: Arc<str>, // one shared copy per file
...
}
```
Reports render from the same bytes, so the output is unchanged.
## Measurements
Peak RSS of a `turbo query` run, median of five runs:
| Query | Host | `main` | #14627 | This PR |
| --- | --- | ---: | ---: | ---: |
| cal.com `dependents` of `packages/lib/logger.ts` | 1 | 642 MB | 724 MB
| 560 MB |
| cal.com `dependents` of `packages/lib/logger.ts` | 2 | 686 MB | 739 MB
| 568 MB |
| payload `dependents` of `packages/payload/src/utilities/isNumber.ts` |
1 | — | 676 MB | 624 MB |
| payload `dependents` of `packages/payload/src/utilities/isNumber.ts` |
2 | 652 MB | 681 MB | 646 MB |
Wall time is unchanged or slightly faster. The setup was
`release-turborepo` builds of #14627 and this branch on Linux x86_64, 8
vCPU / 16 GB, with `hyperfine -N` and 8 blocks per comparison. The
blocks alternated both the command order and which slot directory each
binary ran from. Both binaries were freshly copied, and the page cache
was dropped, before every block. Each block ran 2 warmups and 3 runs per
binary for the `dependents` queries, and 5 warmups and 10 runs for the
others. Results are reported as the per-block median speedup with a
bootstrap 95% CI.
| Query | Host 1 | Speedup [95% CI] | Host 2 | Speedup [95% CI] |
| --- | --- | --- | --- | --- |
| cal.com `dependents` | 411 → 430 ms | +1.3% [-2.4, +4.8] | 446 → 413
ms | +9.0% [+4.1, +14.5] |
| payload `dependents` | 360 → 340 ms | +3.5% [-1.3, +8.4] | 366 → 372
ms | -0.8% [-3.5, +1.7] |
| payload `dependencies` | 144 → 141 ms | +3.1% [+0.9, +5.5] | 139 → 140
ms | -0.1% [-2.7, +2.9] |
| `turbo --version` | 5.96 → 6.01 ms | -0.5% [-2.2, +1.2] | 6.43 → 6.36
ms | +0.3% [-1.5, +2.4] |
With #14618, #14623, and #14627 applied, a cal.com `dependents` query
goes from 1,706 ms on `main` to 459 ms (+73.4% [+72.3, +74.6], second
host), but its peak memory rises above `main`'s. This PR keeps that
speedup and brings peak memory back below `main`.
Workloads: `payloadcms/payload@24ac8955`, `calcom/cal.com@176037d0`.
## Manual verification
- I ran the six file-trace queries (`dependencies` and `dependents` in
payload, cal.com, and vercel/turborepo) with #14627 and this branch.
stdout is byte-identical.
- For the forward traces, stderr is byte-identical. For `dependents`,
stderr was compared as a sorted multiset of reports.