Skip to content

Commit

Permalink
feat: Reduce the number of parts created by tree shaking (#8472)
Browse files Browse the repository at this point in the history
### Description

Extracted from #8420 to split diffs
for optimization vs intuitiveness

### Testing Instructions

---------

Co-authored-by: Tobias Koppers <[email protected]>
  • Loading branch information
kdy1 and sokra authored Jun 13, 2024
1 parent a839c26 commit 9925bcb
Show file tree
Hide file tree
Showing 4 changed files with 447 additions and 0 deletions.
7 changes: 7 additions & 0 deletions crates/turbopack-ecmascript/src/tree_shake/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,13 @@ impl DepGraph {
.map(|d| matches!(d, Dependency::Strong))
.unwrap_or(false)
}

pub(crate) fn has_path_connecting(&mut self, from: &ItemId, to: &ItemId) -> bool {
let from = self.g.node(from);
let to = self.g.node(to);

has_path_connecting(&self.g.idx_graph, from, to, None)
}
}

const ASSERT_CHUNK_KEY: &str = "__turbopack_part__";
Expand Down
15 changes: 15 additions & 0 deletions crates/turbopack-ecmascript/src/tree_shake/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,21 @@ impl Analyzer<'_> {
state
.last_writes
.retain(|last_write| !self.g.has_strong_dep(item_id, last_write));

// Drop all writes which are not reachable from this item.
//
// For
//
// var x = 0
// x = 1
// x = 2
// x += 3
//
// this will drop `x = 1` as not reachable from `x += 3`.

state
.last_writes
.retain(|last_write| self.g.has_path_connecting(item_id, last_write));
}

// For each var in READ_VARS:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
let x = 1;
x = 2;
x = 3;
x = 4;
x = 5;
x += 6;
x += 7;
x += 8;
x += 9;

export { x };

export const y = x;
Loading

0 comments on commit 9925bcb

Please sign in to comment.