Skip to content

Commit

Permalink
feat: Check the number of direct dependants while tree shaking (#8473)
Browse files Browse the repository at this point in the history
### Description

<img width="285" alt="image" src="https://github.com/vercel/turbo/assets/29931815/c37d81fb-ab6e-482a-8e6f-3784a62e923b">

In the graph above, `x` and `y` can both reach `item 5`, but grouping from `item 5` to `item 9` is fine
  • Loading branch information
kdy1 authored Jun 14, 2024
1 parent 3d9c2de commit 5336fd6
Show file tree
Hide file tree
Showing 31 changed files with 875 additions and 1,279 deletions.
12 changes: 10 additions & 2 deletions crates/turbopack-ecmascript/src/tree_shake/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,14 +473,22 @@ impl DepGraph {
continue;
}

let count = global_done
// The number of nodes that this node is dependent on.
let dependant_count = self
.g
.idx_graph
.neighbors_directed(ix as _, petgraph::Direction::Incoming)
.count();

// The number of starting points that can reach to this node.
let count_of_startings = global_done
.iter()
.filter(|&&staring_point| {
has_path_connecting(&self.g.idx_graph, staring_point, ix as _, None)
})
.count();

if count >= 2 {
if dependant_count >= 2 && count_of_startings >= 2 {
groups.push((vec![id.clone()], FxHashSet::default()));
global_done.insert(ix as u32);
}
Expand Down
106 changes: 45 additions & 61 deletions crates/turbopack-ecmascript/tests/tree-shaker/analyzer/1/output.md
Original file line number Diff line number Diff line change
Expand Up @@ -332,31 +332,29 @@ graph TD
N4["Items: [ItemId(Export((&quot;external2&quot;, #2), &quot;external2&quot;)), ItemId(11, Normal)]"];
N5["Items: [ItemId(1, VarDeclarator(0))]"];
N6["Items: [ItemId(2, VarDeclarator(0))]"];
N7["Items: [ItemId(3, VarDeclarator(0))]"];
N8["Items: [ItemId(4, Normal)]"];
N9["Items: [ItemId(5, VarDeclarator(0))]"];
N10["Items: [ItemId(6, Normal)]"];
N11["Items: [ItemId(0, ImportBinding(0)), ItemId(9, Normal)]"];
N0 --> N9;
N0 --> N11;
N7["Items: [ItemId(3, VarDeclarator(0)), ItemId(4, Normal)]"];
N8["Items: [ItemId(5, VarDeclarator(0))]"];
N9["Items: [ItemId(6, Normal)]"];
N10["Items: [ItemId(0, ImportBinding(0)), ItemId(9, Normal)]"];
N0 --> N8;
N0 --> N10;
N0 --> N9;
N0 --> N6;
N1 --> N10;
N1 --> N9;
N2 --> N6;
N3 --> N11;
N3 --> N10;
N3 --> N9;
N4 --> N6;
N4 --> N8;
N4 --> N9;
N4 --> N10;
N6 --> N5;
N7 --> N5;
N7 --> N6;
N8 --> N7;
N8 --> N5;
N8 --> N6;
N9 --> N7;
N9 --> N6;
N9 --> N8;
N10 --> N8;
N10 --> N6;
N10 --> N9;
N11 --> N10;
```
# Entrypoints

Expand All @@ -383,13 +381,13 @@ graph TD
## Part 0
```js
import { foobarCopy } from "__TURBOPACK_PART__" assert {
__turbopack_part__: 9
__turbopack_part__: 8
};
import "__TURBOPACK_PART__" assert {
__turbopack_part__: 11
__turbopack_part__: 10
};
import "__TURBOPACK_PART__" assert {
__turbopack_part__: 10
__turbopack_part__: 9
};
import "__TURBOPACK_PART__" assert {
__turbopack_part__: 6
Expand All @@ -403,7 +401,7 @@ console.log(foobarCopy);
## Part 1
```js
import { foobar } from "__TURBOPACK_PART__" assert {
__turbopack_part__: 10
__turbopack_part__: 9
};
export { foobar };

Expand All @@ -419,10 +417,10 @@ export { foo };
## Part 3
```js
import { internal } from "__TURBOPACK_PART__" assert {
__turbopack_part__: 11
__turbopack_part__: 10
};
import { foobar } from "__TURBOPACK_PART__" assert {
__turbopack_part__: 10
__turbopack_part__: 9
};
export { external1 };
function external1() {
Expand All @@ -439,10 +437,10 @@ import "__TURBOPACK_PART__" assert {
__turbopack_part__: 6
};
import "__TURBOPACK_PART__" assert {
__turbopack_part__: 9
__turbopack_part__: 8
};
import { foobar } from "__TURBOPACK_PART__" assert {
__turbopack_part__: 10
__turbopack_part__: 9
};
export { external2 };
function external2() {
Expand Down Expand Up @@ -474,61 +472,54 @@ export { foo } from "__TURBOPACK_VAR__" assert {
```
## Part 7
```js
const bar = "bar";
export { bar } from "__TURBOPACK_VAR__" assert {
__turbopack_var__: true
};

```
## Part 8
```js
import { bar } from "__TURBOPACK_PART__" assert {
__turbopack_part__: 7
};
import { foobar } from "__TURBOPACK_PART__" assert {
__turbopack_part__: 5
};
import "__TURBOPACK_PART__" assert {
__turbopack_part__: 6
};
const bar = "bar";
foobar += bar;
export { bar } from "__TURBOPACK_VAR__" assert {
__turbopack_var__: true
};
export { foobar } from "__TURBOPACK_VAR__" assert {
__turbopack_var__: true
};

```
## Part 9
## Part 8
```js
import { foobar } from "__TURBOPACK_PART__" assert {
__turbopack_part__: 8
__turbopack_part__: 7
};
let foobarCopy = foobar;
export { foobarCopy } from "__TURBOPACK_VAR__" assert {
__turbopack_var__: true
};

```
## Part 10
## Part 9
```js
import { foobar } from "__TURBOPACK_PART__" assert {
__turbopack_part__: 8
__turbopack_part__: 7
};
import "__TURBOPACK_PART__" assert {
__turbopack_part__: 6
};
import "__TURBOPACK_PART__" assert {
__turbopack_part__: 9
__turbopack_part__: 8
};
foobar += "foo";
export { foobar } from "__TURBOPACK_VAR__" assert {
__turbopack_var__: true
};

```
## Part 11
## Part 10
```js
import { foobar } from "__TURBOPACK_PART__" assert {
__turbopack_part__: 10
__turbopack_part__: 9
};
import { upper } from "module";
function internal() {
Expand All @@ -542,13 +533,13 @@ export { internal } from "__TURBOPACK_VAR__" assert {
## Merged (module eval)
```js
import { foobarCopy } from "__TURBOPACK_PART__" assert {
__turbopack_part__: 9
__turbopack_part__: 8
};
import "__TURBOPACK_PART__" assert {
__turbopack_part__: 11
__turbopack_part__: 10
};
import "__TURBOPACK_PART__" assert {
__turbopack_part__: 10
__turbopack_part__: 9
};
import "__TURBOPACK_PART__" assert {
__turbopack_part__: 6
Expand Down Expand Up @@ -584,7 +575,7 @@ console.log(foobarCopy);
## Part 0
```js
import { foobar } from "__TURBOPACK_PART__" assert {
__turbopack_part__: 7
__turbopack_part__: 6
};
"module evaluation";
import "module";
Expand All @@ -598,7 +589,7 @@ export { foobarCopy } from "__TURBOPACK_VAR__" assert {
## Part 1
```js
import { foobar } from "__TURBOPACK_PART__" assert {
__turbopack_part__: 8
__turbopack_part__: 7
};
export { foobar };

Expand All @@ -618,7 +609,7 @@ export { foo } from "__TURBOPACK_VAR__" assert {
## Part 3
```js
import { foobar } from "__TURBOPACK_PART__" assert {
__turbopack_part__: 8
__turbopack_part__: 7
};
export { external1 };
import { upper } from "module";
Expand Down Expand Up @@ -657,30 +648,23 @@ export { foobar } from "__TURBOPACK_VAR__" assert {
```
## Part 6
```js
const bar = "bar";
export { bar } from "__TURBOPACK_VAR__" assert {
__turbopack_var__: true
};

```
## Part 7
```js
import { bar } from "__TURBOPACK_PART__" assert {
__turbopack_part__: 6
};
import { foobar } from "__TURBOPACK_PART__" assert {
__turbopack_part__: 5
};
const bar = "bar";
foobar += bar;
export { bar } from "__TURBOPACK_VAR__" assert {
__turbopack_var__: true
};
export { foobar } from "__TURBOPACK_VAR__" assert {
__turbopack_var__: true
};

```
## Part 8
## Part 7
```js
import { foobar } from "__TURBOPACK_PART__" assert {
__turbopack_part__: 7
__turbopack_part__: 6
};
foobar += "foo";
export { foobar } from "__TURBOPACK_VAR__" assert {
Expand All @@ -691,7 +675,7 @@ export { foobar } from "__TURBOPACK_VAR__" assert {
## Merged (module eval)
```js
import { foobar } from "__TURBOPACK_PART__" assert {
__turbopack_part__: 7
__turbopack_part__: 6
};
import "module";
"module evaluation";
Expand Down
Loading

0 comments on commit 5336fd6

Please sign in to comment.