Unverified Commit 86d3d61e authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

perf: optimize CSS graph cycle removal (#98860)

### What?

This PR changes how the graph-based CSS chunker resolves contradictory
stylesheet ordering constraints.

The previous implementation repeatedly found one cycle, removed one edge
from it, and searched again. On dense graphs this could repeat thousands
of expensive searches over almost the same data.

The new implementation handles each cyclic region as a whole:

1. Compute a good global order for the modules in that region.
2. Improve that order with a small, bounded local search.
3. Remove every edge that disagrees with the chosen order in one pass.

This preserves the existing chunking pipeline and its hard constraints
while making cycle removal much faster.

### Why?

A production reproduction contained:

- 499 CSS modules
- 104,473 directed ordering edges
- one cyclic region containing 475 modules
- 9,574 edges removed one at a time

`make_acyclic` alone took **9.61 seconds**. Each removed edge caused
several weighted bidirectional shortest-path searches over nearly the
same dense graph.

With this PR, the same reproduction spends **73.5 ms** in `make_acyclic`
under full turbo-tasks tracing, approximately **131× faster** than the
original measured phase.

### How does the CSS graph work?

This section intentionally avoids assuming graph-theory knowledge.

Suppose two routes import CSS in these orders:

```text
route A: reset.css, button.css, page-a.css
route B: reset.css, card.css, page-b.css
```

The chunker creates one node for each CSS file. For every ordered pair
in a route, it records an arrow from the later file to the earlier file:

```text
button.css → reset.css
page-a.css → button.css
page-a.css → reset.css
```

An arrow means “the file on the left must come after the file on the
right.” If several routes agree on the same relationship, the arrow gets
a larger weight. The weight is therefore a vote count: a heavier arrow
represents an ordering preference supported by more routes.

#### Why cycles appear

Different routes can disagree:

```text
route A: a.css, b.css
route B: b.css, a.css
```

That creates both:

```text
b.css → a.css
a.css → b.css
```

There is no global order that satisfies both. This is a cycle: following
the arrows eventually returns to the starting node.

Before the graph can be converted into one linear CSS order, some
contradictory arrows must be discarded. There is no universally correct
choice in this situation because the input itself asks for mutually
incompatible orders. The goal is to preserve as much heavily-supported
ordering information as possible.

### Previous algorithm

The previous implementation:

1. Found a short cycle using weighted bidirectional Dijkstra searches.
2. Removed the lightest edge on that cycle.
3. Repeated until no cycle remained.

This makes intuitive sense for small graphs, but it is expensive when a
dense cyclic region needs thousands of cuts: almost the same graph is
searched again after every single removal.

### New algorithm

#### 1. Find independent cyclic regions

The code first finds **strongly connected components** (SCCs). An SCC is
simply a group of nodes where every node can eventually reach every
other node by following arrows.

Only SCCs with more than one node can contain multi-node cycles. Acyclic
parts of the graph are not changed.

#### 2. Build a candidate order for each SCC

The algorithm constructs an order using a weighted feedback-arc
heuristic:

- A node with no remaining prerequisites can safely go at one end.
- A node that nothing else depends on can safely go at the other end.
- If neither exists, choose the node whose outgoing-arrow weight most
exceeds its incoming-arrow weight. Informally, place the node where it
satisfies the largest net number of weighted ordering votes.

Node indices break ties, so identical inputs always produce identical
output.

This produces a complete ordering of the SCC without deleting edges
during the search.

#### 3. Improve the candidate order

The initial heuristic is fast but can miss simple improvements. The code
therefore runs at most ten deterministic insertion sweeps:

1. Temporarily remove one node from the order.
2. Score every place where it could be inserted.
3. Move it only if the move strictly increases the total weight of
satisfied arrows.

Equal scores keep the existing order. The strict-improvement rule stops
as soon as a sweep makes no changes; ten is only a safety limit. In a
deterministic 5,000-case corpus every SCC converged within four sweeps,
while the supplied reproduction converged after six.

Two-node SCCs skip this refinement because their heavier direction is
already the optimal choice and no non-adjacent move is possible.

#### 4. Remove contradictory edges in bulk

Once the SCC has an order, every arrow can be classified:

- Forward arrow: agrees with the order, so keep it.
- Backward arrow: contradicts the order, so remove it.

After all backward arrows are removed, following arrows can never return
to an earlier position. Therefore the component cannot contain a cycle.

The collected petgraph edge indices are removed in descending order
because petgraph uses swap-removal; descending order keeps indices that
have not yet been removed valid.

#### 5. Continue the existing pipeline

The rest of the chunker is unchanged:

- `linearize` performs a topological sort and uses original route
co-occurrence data to keep frequently co-loaded modules adjacent.
- `split_into_chunks` balances downloaded bytes against request cost.
- Global CSS containment and maximum chunk-size constraints are still
enforced downstream.

### Correctness properties

The implementation preserves these hard properties:

- Consistent/acyclic input graphs are unchanged.
- Every multi-node SCC becomes acyclic.
- Edges outside cyclic SCCs are unchanged.
- Output is deterministic.
- Valid production input lets `linearize` emit every CSS module.
- Global CSS cannot leak into a route that did not already load it.
- Maximum chunk size remains enforced.

The acyclicity argument does not depend on the heuristic finding a
perfect order. Any total order becomes a DAG after every backward edge
is removed; the heuristic only determines which valid compromise is
preferred.

Production chunk groups deduplicate module ids before graph
construction, so they cannot create self-loops. `make_acyclic`
deliberately does not remove hand-built self-loops; that is outside the
production input contract.

### Ordering quality and compatibility

Conflicting route orders have no solution that satisfies every route.
This PR intentionally chooses a different compromise from the previous
cycle-by-cycle algorithm.

Quality was evaluated on the **final order after `make_acyclic` and
`linearize`**, not merely by counting graph edges that survived:

| Quality check | Previous | This PR |
|---|---:|---:|
| Exhaustive three-module cases reaching optimum | 3,375 / 3,375 |
**3,375 / 3,375** |
| Satisfied observations across 5,000 random cases | 142,190 |
**143,010** |
| Satisfied observations in the supplied reproduction | 103,483 |
**105,302** |
| Reproduction chunks | 1 × 499 modules | **1 × 499 modules** |
| Reproduction requests per group | 1 | **1** |

On the 5,000-case corpus, ordering quality was better in 570 cases,
equal in 4,388, and worse in 42.

There is a measured downstream trade-off: total requests increased from
25,518 to 25,646 (**+0.50%**). Requests were better in 153 cases, equal
in 4,699, and worse in 148. The implementation prioritizes aggregate
import-order satisfaction while keeping that request increase bounded.

Applications that accidentally depended on the previous arbitrary
compromise for contradictory imports can observe a different CSS
cascade. Consistent import orders are unaffected.

### Performance

#### Reported reproduction and regression fixtures

| Fixture | Previous | This PR | Improvement |
|---|---:|---:|---:|
| Supplied 499-module reproduction (full tracing) | 9.61 s | **73.5 ms**
| ~131× |
| Dense synthetic fixture | 2.248 s | **24.0 ms** | ~94× |
| 25,600-node fragmented graph | 86.4 ms | **5.69 ms** | ~15× |

Scratch arrays are reused across SCCs. The two-node fast path is
important for fragmented graphs, where allocating refinement state for
thousands of tiny SCCs would otherwise dominate runtime. Refinement
scores candidate positions through slices and moves an accepted node
with an in-place rotation, avoiding a full vector clone/removal for
every node.

#### Large sparse SCC comparison

A second review tested a true sparse directed ring: `n` nodes, exactly
`n` input edges, and one edge that must be removed.

| Ring nodes | This PR | Previous |
|---:|---:|---:|
| 1,000 | **13.8 ms** | 103 ms |
| 5,000 | **366 ms** | 2.69 s |
| 10,000 | **1.47 s** | 10.87 s |
| 20,000 | **5.79 s** | 50.50 s |

The new algorithm is approximately 7–9× faster at every measured size on
this sparse shape.

#### Very large dense SCC scaling

A synthetic dense fixture was also tested beyond the production
reproduction's 454-node SCC:

| Modules | SCC size | This PR |
|---:|---:|---:|
| 500 | 496 | 34 ms |
| 1,000 | 1,000 | 203 ms |
| 1,500 | 1,498 | 491 ms |
| 2,000 | 1,999 | 1.45 s |

There was no paired previous-algorithm baseline for this exact large
fixture. These single-run synthetic measurements show that a
sufficiently large dense SCC can still take seconds. The graph itself
contains roughly a quadratic number of pairwise edges, so both graph
construction and cycle resolution become expensive at that scale.

Whether real applications produce single SCCs this large is unknown. A
possible future mitigation is to evaluate skipping insertion refinement
above a size threshold, but its ordering-quality impact has not been
measured and is not part of this PR.

### Why ten refinement sweeps?

The initial implementation used a maximum of three. Reviewer feedback
asked whether ten would improve quality and how quickly the search
normally converges, so both limits were measured with identical inputs:

| Measurement | Maximum 3 | Maximum 10 |
|---|---:|---:|
| 5,000-case corpus satisfied observations | 310,759 | 310,759 |
| 5,000-case corpus requests | 51,617 | 51,617 |
| Corpus SCCs needing more than 3 sweeps | capped | 2 |
| Reproduction satisfied observations | 105,300 | **105,302** |
| Reproduction convergence | capped at 3 | stopped after 6 |
| Reproduction full-evaluation probe | 57.4 ms | 65.8 ms |

Ten was selected because three was actively truncating improvements in
the reproduction, while ten allowed every measured input to stop
naturally. The quality gain is intentionally described as small: +2 of
105,300 observations and no change across the random corpus. The
slice/in-place refactor offsets the extra sweeps on the release fixtures
(dense: 26.9 → 24.0 ms; fragmented: 6.36–6.69 → 5.69 ms).

### Verification

- Focused `style_groups_graph` suite: **59 passed, 0 failed**, 2
benchmarks ignored
- `cargo fmt --all -- --check`
- `cargo check -p turbopack-core`
- `cargo clippy -p turbopack-core --tests -- -D warnings`
- CSS-order E2E: **291 passed, 0 failed**, 12 existing todos
- Exhaustive three-module corpus: **3,375/3,375 optimal**
- Deterministic 5,000-case corpus and supplied reproduction replay
- Dense, fragmented, and sparse release performance fixtures

<!-- NEXT_JS_LLM -->


<!-- fleet 0ed38506-67e1-4bbf-9eab-5a809a529df4 -->

---------
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>
parent 43f54d52
...@@ -12,9 +12,10 @@ ...@@ -12,9 +12,10 @@
//! 1. **`create_graph`** — for each chunk group, the ordered list of CSS modules is converted into //! 1. **`create_graph`** — for each chunk group, the ordered list of CSS modules is converted into
//! pairwise "later depends on earlier" edges in a directed weighted graph. Edge weights //! pairwise "later depends on earlier" edges in a directed weighted graph. Edge weights
//! accumulate when the same `(from, to)` pair occurs in multiple groups. //! accumulate when the same `(from, to)` pair occurs in multiple groups.
//! 2. **`make_acyclic`** — co-occurrence almost always produces cycles. Each multi-node SCC has its //! 2. **`make_acyclic`** — co-occurrence almost always produces cycles. Each multi-node SCC is
//! lowest-weight edge cut until the graph is a DAG. Heavy edges represent strong co-occurrence //! ordered with a weighted feedback-arc heuristic, refined with bounded insertion moves, then
//! and are preserved. //! all backward edges are removed in one pass. The heuristic preferentially preserves heavy
//! edges that represent strong co-occurrence.
//! 3. **`linearize`** — Kahn-style topological sort with a tie-break: when several dependents //! 3. **`linearize`** — Kahn-style topological sort with a tie-break: when several dependents
//! become unblocked at once, the heaviest edge wins (and insertion order breaks ties among equal //! become unblocked at once, the heaviest edge wins (and insertion order breaks ties among equal
//! weights). This places strongly co-occurring modules adjacent in the global order. //! weights). This places strongly co-occurring modules adjacent in the global order.
......
//! `SubgraphView`: a borrow-only filtered view over a [`petgraph::graph::DiGraph`] driven by a //! Read-only graph abstractions for the style-group algorithms.
//! [`rustc_hash::FxHashSet`] of node indices.
//! //!
//! The PoC algorithms ([`super::algorithm`]) accept any type that implements [`ReadonlyGraph`]. //! Production algorithms use [`ReadonlyGraph`] with `&DiGraph`. Tests additionally use a
//! That lets us pass either a `&DiGraph` directly or a `SubgraphView` restricted to e.g. one //! borrow-only `SubgraphView` filtered by a node-index set to exercise subset behavior without
//! SCC, without cloning the underlying graph. //! cloning the underlying graph.
use std::marker::PhantomData; use std::marker::PhantomData;
...@@ -11,10 +10,11 @@ use petgraph::{ ...@@ -11,10 +10,11 @@ use petgraph::{
Direction, Direction,
graph::{DiGraph, NodeIndex}, graph::{DiGraph, NodeIndex},
}; };
#[cfg(test)]
use rustc_hash::FxHashSet; use rustc_hash::FxHashSet;
/// Read-only view over a directed weighted graph. Implemented for `&DiGraph<N, u32>` and for /// Read-only view over a directed weighted graph. Production code uses the `&DiGraph<N, u32>`
/// [`SubgraphView`]. /// implementation; tests also use [`SubgraphView`] to exercise algorithms on filtered subsets.
#[allow(dead_code)] // Some methods are kept for symmetry with the JS PoC; not all are used yet. #[allow(dead_code)] // Some methods are kept for symmetry with the JS PoC; not all are used yet.
pub(super) trait ReadonlyGraph<'a>: Copy { pub(super) trait ReadonlyGraph<'a>: Copy {
type NodesIter: Iterator<Item = NodeIndex>; type NodesIter: Iterator<Item = NodeIndex>;
...@@ -90,24 +90,29 @@ impl<'a, N> Iterator for NeighborsWithWeight<'a, N> { ...@@ -90,24 +90,29 @@ impl<'a, N> Iterator for NeighborsWithWeight<'a, N> {
/// A live view over `graph` restricted to the nodes in `subset`. Edges whose source or target /// A live view over `graph` restricted to the nodes in `subset`. Edges whose source or target
/// is outside the subset are filtered out on iteration. /// is outside the subset are filtered out on iteration.
#[cfg(test)]
pub(super) struct SubgraphView<'a, N> { pub(super) struct SubgraphView<'a, N> {
graph: &'a DiGraph<N, u32>, graph: &'a DiGraph<N, u32>,
subset: &'a FxHashSet<NodeIndex>, subset: &'a FxHashSet<NodeIndex>,
} }
#[cfg(test)]
impl<'a, N> SubgraphView<'a, N> { impl<'a, N> SubgraphView<'a, N> {
pub(super) fn new(graph: &'a DiGraph<N, u32>, subset: &'a FxHashSet<NodeIndex>) -> Self { pub(super) fn new(graph: &'a DiGraph<N, u32>, subset: &'a FxHashSet<NodeIndex>) -> Self {
Self { graph, subset } Self { graph, subset }
} }
} }
#[cfg(test)]
impl<'a, N> Clone for SubgraphView<'a, N> { impl<'a, N> Clone for SubgraphView<'a, N> {
fn clone(&self) -> Self { fn clone(&self) -> Self {
*self *self
} }
} }
#[cfg(test)]
impl<'a, N> Copy for SubgraphView<'a, N> {} impl<'a, N> Copy for SubgraphView<'a, N> {}
#[cfg(test)]
impl<'a, N: 'a> ReadonlyGraph<'a> for SubgraphView<'a, N> { impl<'a, N: 'a> ReadonlyGraph<'a> for SubgraphView<'a, N> {
type NodesIter = SubgraphNodes<'a>; type NodesIter = SubgraphNodes<'a>;
type OutgoingIter = SubgraphNeighbors<'a, N>; type OutgoingIter = SubgraphNeighbors<'a, N>;
...@@ -183,10 +188,12 @@ impl<'a, N: 'a> ReadonlyGraph<'a> for SubgraphView<'a, N> { ...@@ -183,10 +188,12 @@ impl<'a, N: 'a> ReadonlyGraph<'a> for SubgraphView<'a, N> {
} }
} }
#[cfg(test)]
pub(super) struct SubgraphNodes<'a> { pub(super) struct SubgraphNodes<'a> {
iter: std::collections::hash_set::Iter<'a, NodeIndex>, iter: std::collections::hash_set::Iter<'a, NodeIndex>,
} }
#[cfg(test)]
impl<'a> Iterator for SubgraphNodes<'a> { impl<'a> Iterator for SubgraphNodes<'a> {
type Item = NodeIndex; type Item = NodeIndex;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
...@@ -194,12 +201,14 @@ impl<'a> Iterator for SubgraphNodes<'a> { ...@@ -194,12 +201,14 @@ impl<'a> Iterator for SubgraphNodes<'a> {
} }
} }
#[cfg(test)]
pub(super) struct SubgraphNeighbors<'a, N> { pub(super) struct SubgraphNeighbors<'a, N> {
iter: Option<petgraph::graph::Neighbors<'a, u32>>, iter: Option<petgraph::graph::Neighbors<'a, u32>>,
subset: &'a FxHashSet<NodeIndex>, subset: &'a FxHashSet<NodeIndex>,
_n: PhantomData<N>, _n: PhantomData<N>,
} }
#[cfg(test)]
impl<'a, N> Iterator for SubgraphNeighbors<'a, N> { impl<'a, N> Iterator for SubgraphNeighbors<'a, N> {
type Item = NodeIndex; type Item = NodeIndex;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
...@@ -208,6 +217,7 @@ impl<'a, N> Iterator for SubgraphNeighbors<'a, N> { ...@@ -208,6 +217,7 @@ impl<'a, N> Iterator for SubgraphNeighbors<'a, N> {
} }
} }
#[cfg(test)]
pub(super) struct SubgraphNeighborsWithWeight<'a, N> { pub(super) struct SubgraphNeighborsWithWeight<'a, N> {
graph: &'a DiGraph<N, u32>, graph: &'a DiGraph<N, u32>,
iter: Option<petgraph::graph::WalkNeighbors<u32>>, iter: Option<petgraph::graph::WalkNeighbors<u32>>,
...@@ -215,6 +225,7 @@ pub(super) struct SubgraphNeighborsWithWeight<'a, N> { ...@@ -215,6 +225,7 @@ pub(super) struct SubgraphNeighborsWithWeight<'a, N> {
_n: PhantomData<N>, _n: PhantomData<N>,
} }
#[cfg(test)]
impl<'a, N> Iterator for SubgraphNeighborsWithWeight<'a, N> { impl<'a, N> Iterator for SubgraphNeighborsWithWeight<'a, N> {
type Item = (NodeIndex, u32); type Item = (NodeIndex, u32);
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
......
...@@ -8,9 +8,8 @@ use rustc_hash::FxHashSet; ...@@ -8,9 +8,8 @@ use rustc_hash::FxHashSet;
use super::{ use super::{
algorithm::{ algorithm::{
ChunkGroupIndex, ModuleChunkGroups, compute_chunked_chunk_groups, create_graph, ChunkGroupIndex, ModuleChunkGroups, compute_chunked_chunk_groups, create_graph, linearize,
find_short_cycle, linearize, make_acyclic, split_into_chunks, make_acyclic, refine_feedback_arc_order, split_into_chunks, strongly_connected_components,
strongly_connected_components,
}, },
subgraph_view::{ReadonlyGraph, SubgraphView}, subgraph_view::{ReadonlyGraph, SubgraphView},
}; };
...@@ -74,10 +73,6 @@ fn incoming_with_weight<'a, G: ReadonlyGraph<'a>>(g: G, node: NodeIndex) -> Vec< ...@@ -74,10 +73,6 @@ fn incoming_with_weight<'a, G: ReadonlyGraph<'a>>(g: G, node: NodeIndex) -> Vec<
v v
} }
fn to_set(v: &[NodeIndex]) -> FxHashSet<NodeIndex> {
v.iter().copied().collect()
}
fn ids(v: &[NodeIndex]) -> Vec<usize> { fn ids(v: &[NodeIndex]) -> Vec<usize> {
v.iter().map(|n| n.index()).collect() v.iter().map(|n| n.index()).collect()
} }
...@@ -417,92 +412,6 @@ fn to_sorted_ids(s: &FxHashSet<NodeIndex>) -> Vec<usize> { ...@@ -417,92 +412,6 @@ fn to_sorted_ids(s: &FxHashSet<NodeIndex>) -> Vec<usize> {
v v
} }
// ---------------------------------------------------------------------------
// find_short_cycle
// ---------------------------------------------------------------------------
fn is_valid_cycle<'a, G: ReadonlyGraph<'a>>(graph: G, cycle: &[NodeIndex]) -> bool {
if cycle.len() < 2 {
return false;
}
let unique: FxHashSet<_> = cycle.iter().copied().collect();
if unique.len() != cycle.len() {
return false;
}
for i in 0..cycle.len() {
let from = cycle[i];
let to = cycle[(i + 1) % cycle.len()];
if !graph.outgoing_edges(from).any(|t| t == to) {
return false;
}
}
true
}
#[test]
fn find_short_cycle_two_node_scc() {
let g = build_graph(2, |g| {
g.add_edge(n(0), n(1), 1);
g.add_edge(n(1), n(0), 1);
});
let cycle = find_short_cycle(&g, None).expect("test graph has a cycle");
assert!(is_valid_cycle(&g, &cycle));
assert_eq!(
to_set(&cycle),
[n(0), n(1)].into_iter().collect::<FxHashSet<_>>()
);
}
#[test]
fn find_short_cycle_three_cycle() {
let g = build_graph(3, |g| {
g.add_edge(n(0), n(1), 1);
g.add_edge(n(1), n(2), 1);
g.add_edge(n(2), n(0), 1);
});
let cycle = find_short_cycle(&g, None).expect("test graph has a cycle");
assert!(is_valid_cycle(&g, &cycle));
assert_eq!(cycle.len(), 3);
}
#[test]
fn find_short_cycle_prefers_two_cycle_when_longer_exists() {
// 2-cycle b ↔ c plus longer arm a → b → c → d → a. The 2-cycle should win.
let g = build_graph(4, |g| {
g.add_edge(n(0), n(1), 1);
g.add_edge(n(1), n(2), 1);
g.add_edge(n(2), n(3), 1);
g.add_edge(n(3), n(0), 1);
g.add_edge(n(2), n(1), 1);
});
let cycle = find_short_cycle(&g, None).expect("test graph has a cycle");
assert!(is_valid_cycle(&g, &cycle));
assert_eq!(cycle.len(), 2);
assert_eq!(
to_set(&cycle),
[n(1), n(2)].into_iter().collect::<FxHashSet<_>>()
);
}
#[test]
fn find_short_cycle_uses_edge_weights_to_pick_lowest_total_weight() {
let g = build_graph(3, |g| {
g.add_edge(n(0), n(1), 10);
g.add_edge(n(1), n(0), 10);
g.add_edge(n(0), n(2), 1);
g.add_edge(n(2), n(0), 1);
g.add_edge(n(1), n(2), 100);
g.add_edge(n(2), n(1), 100);
});
let cycle = find_short_cycle(&g, None).expect("test graph has a cycle");
assert!(is_valid_cycle(&g, &cycle));
assert_eq!(cycle.len(), 2);
assert_eq!(
to_set(&cycle),
[n(0), n(2)].into_iter().collect::<FxHashSet<_>>()
);
}
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// make_acyclic // make_acyclic
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
...@@ -583,6 +492,98 @@ fn make_acyclic_preserves_non_cycle_edges() { ...@@ -583,6 +492,98 @@ fn make_acyclic_preserves_non_cycle_edges() {
assert!(edges.contains(&(2, 3))); assert!(edges.contains(&(2, 3)));
} }
#[test]
fn make_acyclic_equal_weight_tie_prefers_lower_node_index() {
let mut g = build_graph(2, |g| {
g.add_edge(n(0), n(1), 1);
g.add_edge(n(1), n(0), 1);
});
make_acyclic(&mut g);
assert_eq!(edge_set(&g), [(0, 1)].into_iter().collect::<FxHashSet<_>>());
}
#[test]
fn make_acyclic_refines_non_adjacent_counterexample_move() {
let groups = [vec![0, 1], vec![2, 0], vec![0, 1, 2]];
let (graph, _) = create_graph(&groups, 3);
let scc = strongly_connected_components(&graph)
.into_iter()
.find(|component| component.len() > 1)
.unwrap();
// This is the seed heuristic's order. Neither adjacent swap improves it, but moving node 2
// from the end to the front increases satisfied edge weight from 3 to 4.
let mut order = vec![n(1), n(0), n(2)];
refine_feedback_arc_order(&mut order, &graph, &scc);
assert_eq!(ids(&order), vec![2, 1, 0]);
}
#[test]
fn make_acyclic_refines_node_toward_end() {
let groups = [vec![1, 0], vec![0, 2], vec![2, 1, 0]];
let (graph, _) = create_graph(&groups, 3);
let scc = strongly_connected_components(&graph)
.into_iter()
.find(|component| component.len() > 1)
.unwrap();
// Reverse of the preceding fixture: moving node 2 from the front to the end increases the
// satisfied edge weight from 3 to 4.
let mut order = vec![n(2), n(0), n(1)];
refine_feedback_arc_order(&mut order, &graph, &scc);
assert_eq!(ids(&order), vec![0, 1, 2]);
}
#[test]
fn make_acyclic_refinement_keeps_equal_score_order() {
let mut graph = build_graph(3, |g| {
for from in 0..3 {
for to in 0..3 {
if from != to {
g.add_edge(n(from), n(to), 1);
}
}
}
});
let scc: FxHashSet<_> = [n(0), n(1), n(2)].into_iter().collect();
let mut order = vec![n(2), n(0), n(1)];
refine_feedback_arc_order(&mut order, &graph, &scc);
assert_eq!(ids(&order), vec![2, 0, 1]);
make_acyclic(&mut graph);
assert_acyclic(&graph);
}
#[test]
fn make_acyclic_counterexample_reaches_optimal_final_order() {
let groups = [vec![0, 1], vec![2, 0], vec![0, 1, 2]];
let (mut graph, module_to_groups) = create_graph(&groups, 3);
make_acyclic(&mut graph);
let order = linearize(&graph, &module_to_groups);
assert_eq!(ids(&order), vec![0, 1, 2]);
}
#[test]
fn make_acyclic_dense_equal_weight_graph_is_deterministic() {
fn run() -> FxHashSet<(usize, usize)> {
let mut g = build_graph(64, |g| {
for from in 0..64 {
for to in 0..64 {
if from != to {
g.add_edge(n(from), n(to), 1);
}
}
}
});
make_acyclic(&mut g);
assert_acyclic(&g);
edge_set(&g)
}
let edges = run();
assert_eq!(edges, run());
assert_eq!(edges.len(), 64 * 63 / 2);
assert!(edges.iter().all(|&(from, to)| from < to));
}
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// linearize // linearize
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
...@@ -812,6 +813,79 @@ fn split_weight_distribution_protects_small_groups_from_overshipping() { ...@@ -812,6 +813,79 @@ fn split_weight_distribution_protects_small_groups_from_overshipping() {
assert_eq!(split(3.0), vec![vec![0], vec![1]]); assert_eq!(split(3.0), vec![vec![0], vec![1]]);
} }
// ---------------------------------------------------------------------------
// Performance benchmark
// ---------------------------------------------------------------------------
fn dense_scc_fixture() -> Vec<Vec<usize>> {
let mut groups = vec![(0..454).collect::<Vec<_>>()];
for group_idx in 0..14 {
let mut group = Vec::with_capacity(24);
// Reverse ten well-spaced nodes from the giant group. Across fourteen groups this creates
// roughly the supplied number of reciprocal pairs while connecting most giant-group nodes.
for offset in (0..10).rev() {
group.push((group_idx * 31 + offset * 47) % 454);
}
// Reuse the 45 outside nodes cyclically so they participate in overlapping constraints.
for offset in 0..14 {
group.push(454 + (group_idx * 7 + offset) % 45);
}
groups.push(group);
}
groups
}
#[test]
#[ignore]
fn bench_make_acyclic_dense_scc() {
use std::time::Instant;
let groups = dense_scc_fixture();
let (mut graph, _) = create_graph(&groups, 499);
let cyclic_sccs: Vec<_> = strongly_connected_components(&graph)
.into_iter()
.filter(|scc| scc.len() > 1)
.collect();
eprintln!(
"BENCH input groups={} nodes={} edges={} cyclic_sccs={} largest_scc={}",
groups.len(),
graph.node_count(),
graph.edge_count(),
cyclic_sccs.len(),
cyclic_sccs.iter().map(FxHashSet::len).max().unwrap_or(0)
);
let start = Instant::now();
make_acyclic(&mut graph);
let elapsed = start.elapsed();
eprintln!(
"BENCH elapsed={elapsed:?} remaining_edges={}",
graph.edge_count()
);
assert_acyclic(&graph);
}
#[test]
#[ignore]
fn bench_make_acyclic_many_small_sccs() {
use std::time::Instant;
for &size in &[1600, 3200, 6400, 12800, 25600] {
let mut g = build_graph(size, |g| {
for i in (0..size).step_by(2) {
g.add_edge(n(i), n(i + 1), 1);
g.add_edge(n(i + 1), n(i), 2);
}
});
let start = Instant::now();
make_acyclic(&mut g);
eprintln!(
"BENCH many_small_sccs size={size} elapsed={:?}",
start.elapsed()
);
assert_acyclic(&g);
}
}
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// e2e (PoC pipeline) // e2e (PoC pipeline)
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
......
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