Unverified Commit 536581c0 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

turbopack: speed up and share reverse postorder ranks (#100452)

Stacked on #100440. The lower PRs of the stack (#100428, #100429,
#100430) are merged.

### What?

Two changes to the reverse postorder ranks that the RPO stack computes
before each fixed-point traversal:

1. **A faster `ModuleGraphSnapshot::reverse_postorder_ranks`, with
identical ranks.**
- Visited nodes are tracked in one `FixedBitSet` per graph instead of an
`FxHashSet<GraphNodeIndex>`. `DiGraph` node indices are dense, and
`VisitedModule` aliases are resolved first.
- `postorder` is preallocated, and the result map is created with
capacity. Collecting a `Result` iterator into a map gives it no capacity
hint, so it rehashes as it grows.
   - One DFS stack is reused across roots.
   - DFS order and rank numbering are unchanged.
- The method now asserts it's called on the whole graph, not on an
individual graph layer, where `VisitedModule` nodes can't be resolved.
This matches the existing check in
`traverse_edges_fixed_point_with_priority`.
2. **One shared rank computation for chunk group info and merged
modules.**
- The new `ModuleGraphSnapshot::entry_ranks` computes the ranks from the
entries of the graph (derived from its chunk groups) on first use and
caches them in a `OnceLock` on the snapshot.
- `compute_chunk_group_info` and `compute_merged_modules` already
computed exactly these ranks (same entries, same order) on the same
graph, so they now share one computation.

### Why?

The RPO stack adds a rank pass in front of each fixed-point analysis, 5
per build. This removes most of that added cost.

The cache lives on the immutable `ModuleGraph` snapshot that the
analyses already read through the shared `ReadRef`:
- the ranks always belong to exactly the graph being traversed;
- there is no extra turbo-tasks function or cell, and no second,
separately versioned read inside a task;
- there is nothing extra to invalidate, since a changed graph is a new
snapshot with an empty cache;
- the ranks are computed inside `OnceLock::get_or_init`, so concurrent
cold callers wait for one computation instead of racing;
- an error is cached too (as a cloneable `SharedError`) and returned to
every caller of that snapshot.

Not shared:
- **`collect_graph`** roots its DFS only at `Entry` chunk groups.
Sharing all-entry ranks would change its order on cyclic graphs, so it
keeps computing its own ranks (with the faster implementation).
- **`compute_binding_usage_info`** runs on different graphs, before
unused references are removed, so it keeps computing its own ranks as
well.

### Benchmarks

> **Historical, not rerun:** measured in the previous review round,
before this stack was rebased onto canary `502b6b7d` and before the
latest review changes. This round only reran the `turbo-bitset`
microbenchmarks in #100566.

Real Turbopack `next build --experimental-build-mode compile` with
release `next-swc` builds (LTO off, 16 codegen units). #100440 and this
PR were each built with the same local timing instrumentation and run 5
times per app, interleaved in randomized order. Times are medians
[min–max], summed over all rank computations of a build:

| | rank passes per build | #100440 | this PR |
|---|---|---|---|
| generated app router app, 300 routes | 5 → 4 | 21.1 ms [20.0–23.6] |
14.2 ms [12.7–15.5] |
| `bench/nested-deps-app-router-many-pages` | 5 → 4 | 67.9 ms
[58.8–70.7] | 25.5 ms [25.1–32.5] |

Per caller (many-pages, median):

| | binding usage (2×) | chunk group info | collect | merged modules |
|---|---|---|---|---|
| #100440 | 26.6 ms | 12.4 ms | 11.6 ms | 12.0 ms |
| this PR | 12.7 ms | 6.5 ms (shared) | 6.4 ms | cached |

- **Visit counts:** identical between both versions, for every
fixed-point analysis, in all runs.
- **Fixed-point time:** expected to be unchanged. The measured
differences are within the run-to-run noise.
- **Caveats:**
  - These are sums of rank-helper timers, not wall-clock savings.
- This machine was slower than in the earlier PRs of the stack, so
absolute numbers aren't comparable with those PRs.

### How was it verified?

- **Exact ranks:** while developing, the previous implementation was
kept as a reference and compared against the new one on every call, with
`assert_eq!` on the rank maps. The comparison ran across the
`turbopack-core` lib tests, the snapshot and execution suites, and real
builds of both benchmark apps (4 rank computations per build). There
were no mismatches. The reference was removed afterwards.
- **New unit tests:**
- the shared ranks equal
`reverse_postorder_ranks(all_chunk_group_entry_modules())` on a graph
with a diamond and a cycle, and a second call returns the same map;
- a new snapshot of an edited graph gets fresh ranks, including the new
module, while the old snapshot keeps its cached map;
  - calling `reverse_postorder_ranks` on a graph layer panics.
- **Suites and checks:** `cargo test -p turbopack-core --lib`, `cargo
test -p turbopack-tests --test snapshot` (no snapshot changes) and
`cargo test -p turbopack-tests --test execution` pass. `cargo clippy -p
turbopack-core --all-targets -- -D warnings` and `RUSTDOCFLAGS='-D
warnings' cargo doc -p turbopack-core --no-deps` are clean.
- **Real builds:** the emitted chunks of both benchmark apps are
byte-identical to #100440 (sha256 over all JS files in
`.next/server/chunks` and `.next/static/chunks`).

<!-- NEXT_JS_LLM -->


<!-- fleet b56269ed-2145-4f30-b0fe-dc9762520521 -->

---------
Co-authored-by: 's avatarvercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com>
Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
Co-authored-by: 's avatarVercel Fleet <fleet@vercel.com>
parent cc0cdc8b
...@@ -9622,6 +9622,7 @@ dependencies = [ ...@@ -9622,6 +9622,7 @@ dependencies = [
"data-encoding", "data-encoding",
"either", "either",
"erased-serde", "erased-serde",
"fixedbitset",
"indexmap 2.14.0", "indexmap 2.14.0",
"itertools 0.10.5", "itertools 0.10.5",
"num-bigint", "num-bigint",
......
...@@ -24,6 +24,7 @@ const_format = { workspace = true } ...@@ -24,6 +24,7 @@ const_format = { workspace = true }
data-encoding = { workspace = true } data-encoding = { workspace = true }
either = { workspace = true } either = { workspace = true }
erased-serde = { workspace = true } erased-serde = { workspace = true }
fixedbitset = { workspace = true }
indexmap = { workspace = true } indexmap = { workspace = true }
itertools = { workspace = true } itertools = { workspace = true }
num-bigint = "0.4" num-bigint = "0.4"
......
...@@ -168,9 +168,9 @@ pub async fn compute_binding_usage_info( ...@@ -168,9 +168,9 @@ pub async fn compute_binding_usage_info(
// Visit modules in reverse postorder. In an acyclic graph, this processes each module only // Visit modules in reverse postorder. In an acyclic graph, this processes each module only
// after all modules importing it, i.e. once, with its final usage. Cycles still rely on the // after all modules importing it, i.e. once, with its final usage. Cycles still rely on the
// fixed point and may revisit modules in or after them. This avoids order-dependent // fixed point and may revisit modules in or after them. This avoids order-dependent
// revisits in DAG-shaped graphs, at the cost of an additional traversal to compute ranks. // revisits in DAG-shaped graphs, at the cost of an additional traversal to compute ranks
let module_rank = // (shared with the other analyses of this graph).
graph_ref.reverse_postorder_ranks(graph_ref.all_chunk_group_entry_modules())?; let module_rank = graph_ref.entry_ranks()?;
let visit_count = graph_ref.traverse_edges_fixed_point_with_priority( let visit_count = graph_ref.traverse_edges_fixed_point_with_priority(
graph_ref graph_ref
......
...@@ -639,9 +639,9 @@ pub async fn compute_chunk_group_info(graph: &ModuleGraph) -> Result<Vc<ChunkGro ...@@ -639,9 +639,9 @@ pub async fn compute_chunk_group_info(graph: &ModuleGraph) -> Result<Vc<ChunkGro
let entries = graph.all_chunk_group_entries().collect::<Vec<_>>(); let entries = graph.all_chunk_group_entries().collect::<Vec<_>>();
// First, rank every module in reverse postorder (so that a module is visited after the // First, rank every module in reverse postorder (so that a module is visited after the
// modules importing it), and initialize `module_chunk_groups` with empty bitmaps. // modules importing it), and initialize `module_chunk_groups` with empty bitmaps. The ranks
let module_rank = // from the entries of the graph are shared with other analyses of this graph.
graph.reverse_postorder_ranks(entries.iter().flat_map(|e| e.entries()))?; let module_rank = graph.entry_ranks()?;
module_chunk_groups.reserve(module_rank.len()); module_chunk_groups.reserve(module_rank.len());
for module in module_rank.keys() { for module in module_rank.keys() {
module_chunk_groups.insert(*module, RoaringBitmapWrapper::default()); module_chunk_groups.insert(*module, RoaringBitmapWrapper::default());
......
...@@ -210,10 +210,11 @@ pub async fn compute_merged_modules(module_graph: Vc<ModuleGraph>) -> Result<Vc< ...@@ -210,10 +210,11 @@ pub async fn compute_merged_modules(module_graph: Vc<ModuleGraph>) -> Result<Vc<
.collect::<Vec<_>>(); .collect::<Vec<_>>();
// First, rank every module in reverse postorder, so that a module is visited after the // First, rank every module in reverse postorder, so that a module is visited after the
// modules importing it. // modules importing it. The ranks from the entries of the graph are shared with other
// analyses of this graph (e.g. the chunk group info).
let module_rank = { let module_rank = {
let _inner_span = tracing::info_span!("compute reverse postorder").entered(); let _inner_span = tracing::info_span!("compute reverse postorder").entered();
module_graph.reverse_postorder_ranks(entries.iter().copied())? module_graph.entry_ranks()?
}; };
// For each module, the indices in the bitmap store which merge group entry modules // For each module, the indices in the bitmap store which merge group entry modules
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment