Unverified Commit b78f7d3f authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

Turbopack: order CSS modules by chunk-group co-occurrence in linearize (#95579)

## Summary

The graph-based CSS chunker's `linearize` step produces the global
module order that `split_into_chunks` then cuts into chunks. It
previously broke ties between ready modules using a weight-sorted stack.

It now prefers the ready module that shares the most chunk groups with
the previously placed module, so modules that are loaded together end up
adjacent in the global order and split into better-aligned chunks. On a
real world example this measurably lowers the modeled chunk-loading
cost, with fewer requests and less over-fetched CSS.

The shared-group count is read from edge weights (how many chunk groups
two modules co-occur in). Since `make_acyclic` deletes edges to break
cycles, it now returns the edges it cut so `linearize` can still count
those conflicting-order co-occurrences — reconstructing the full
co-occurrence without cloning the graph.

## Verification

- `cargo test -p turbopack-core style_groups_graph::tests` (all pass)
- Validated on a real world example that the resulting global order is
deterministic and the modeled chunk-loading cost decreases.

<!-- NEXT_JS_LLM_PR -->
parent 41b82388
...@@ -1127,7 +1127,7 @@ impl CssChunkingConfig { ...@@ -1127,7 +1127,7 @@ impl CssChunkingConfig {
} }
/// Default `requestCost` for the graph algorithm (in bytes). /// Default `requestCost` for the graph algorithm (in bytes).
const DEFAULT_REQUEST_COST: f32 = 100_000.0; const DEFAULT_REQUEST_COST: f32 = 20_000.0;
/// Default `weightDistribution` for the graph algorithm. /// Default `weightDistribution` for the graph algorithm.
const DEFAULT_WEIGHT_DISTRIBUTION: f32 = 0.1; const DEFAULT_WEIGHT_DISTRIBUTION: f32 = 0.1;
......
...@@ -140,10 +140,11 @@ pub async fn compute_style_groups_graph( ...@@ -140,10 +140,11 @@ pub async fn compute_style_groups_graph(
let module_style_types: Vec<StyleType> = module_data.iter().map(|m| m.style_type).collect(); let module_style_types: Vec<StyleType> = module_data.iter().map(|m| m.style_type).collect();
// 3. Run the synchronous chunking pipeline. // 3. Run the synchronous chunking pipeline.
let mut graph = tracing::trace_span!("create_graph") let (mut graph, module_to_groups) = tracing::trace_span!("create_graph")
.in_scope(|| algorithm::create_graph(&chunk_groups, modules_in_order.len())); .in_scope(|| algorithm::create_graph(&chunk_groups, modules_in_order.len()));
tracing::trace_span!("make_acyclic").in_scope(|| algorithm::make_acyclic(&mut graph)); tracing::trace_span!("make_acyclic").in_scope(|| algorithm::make_acyclic(&mut graph));
let global_order = tracing::trace_span!("linearize").in_scope(|| algorithm::linearize(&graph)); let global_order = tracing::trace_span!("linearize")
.in_scope(|| algorithm::linearize(&graph, &module_to_groups));
let chunks = tracing::trace_span!("split_into_chunks").in_scope(|| { let chunks = tracing::trace_span!("split_into_chunks").in_scope(|| {
algorithm::split_into_chunks( algorithm::split_into_chunks(
&global_order, &global_order,
......
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