turbopack: use reverse postorder priority in compute_binding_usage_info (#100440)
Stacked on #100430 (#100428 → #100429 → #100430 → this).
### What?
Two changes in `compute_binding_usage_info`:
1. **Reverse postorder priority.** The fixed-point traversal now uses
`Reverse(rank)` from `ModuleGraphSnapshot::reverse_postorder_ranks`
(added in #100428) as its priority, instead of the constant `0`. Modules
are visited roots first, so in an acyclic graph each module is processed
exactly once, after all of its importers and with its final usage.
Cycles still rely on the fixed point.
2. **No clone of the parent's usage on passthrough edges.** The
`ExportUsage::Passthrough` branch cloned the parent's
`ModuleExportUsageInfo` to work around borrowing two entries of
`used_exports` at once. It now inserts the target's entry, borrows both
entries with `HashMap::get_disjoint_mut`, and merges directly. A
self-edge (`parent == target`) is handled explicitly, because merging a
usage into itself never changes it and `get_disjoint_mut` would panic on
the duplicate key.
### Why?
With the constant priority, the traversal pops the most recently queued
node first, which makes the order DFS-like. That gives no ordering
guarantee: a shared module can be processed before all of its importers,
and is then revisited whenever a later importer adds new export names.
On the apps measured below this happens rarely, so there is no speedup
here. The extra rank traversal makes the analysis **slower** on both
apps. The change is meant as protection against worst cases in apps
where shared modules are imported with different names at many different
depths. In those, the number of revisits is no longer order-dependent on
DAG-shaped graphs.
### Benchmarks
Real Turbopack `next build --experimental-build-mode compile` runs with
release `next-swc` builds (LTO off, 16 codegen units). Builds were
interleaved on the same machine, 3 runs each. Times are the
binding-usage analysis (rank pass + fixed point), summed over both
invocations per build: median [min–max].
| | base (#100430) | + RPO priority | + clone removal (this PR) |
|---|---|---|---|
| generated app router app, 300 routes | 11.2 ms [10.8–11.7] | 14.7 ms
[13.2–16.0] | 14.1 ms [13.4–15.7] |
| `bench/nested-deps-app-router-many-pages` | 36.6 ms [35.5–37.6] | 52.4
ms [51.0–68.8] | 55.7 ms [52.9–57.3] |
| | visits (base → this PR) | rank pass (median) |
|---|---|---|
| generated app | 27,128 → 24,228 | 4.7 ms |
| many-pages | 64,894 → 64,606 | 14.2 ms |
- **Revisits:** RPO removes almost all of them; 25 remain, inside or
right after cycles. Under the constant priority, though, 88% and 99.6%
of modules were already visited only once, so there is little to save.
- **Rank pass:** it is a near-full extra traversal, and it costs more
than the saved revisits on both apps.
- **Clone removal:** no measurable difference within noise on these
apps.
### How was it verified?
- `cargo test -p turbopack-core --lib`, `cargo test -p turbopack-tests
--test snapshot` (no snapshot changes) and `cargo test -p
turbopack-tests --test execution` all pass.
- `cargo clippy -p turbopack-core --all-targets -- -D warnings` and
`RUSTDOCFLAGS='-D warnings' cargo doc -p turbopack-core --no-deps` are
clean.
- Output equivalence on both benchmark apps: I compared a local, not
committed, fingerprint of the analysis result between the base and this
PR, covering per-module used exports, partial-namespace modules and
unused-reference targets. It is identical for both invocations.
<!-- NEXT_JS_LLM -->
<!-- fleet b56269ed-2145-4f30-b0fe-dc9762520521 -->
---------
Co-authored-by: vercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com>
Co-authored-by: Tobias Koppers <1365881+sokra@users.noreply.github.com>