Skip to content

Commit

Permalink
Tree Shaking shared state and side effects (#8370)
Browse files Browse the repository at this point in the history
### Description

adds more test cases

### Testing Instructions

<!--
  Give a quick description of steps to test your changes.
-->

---------

Co-authored-by: 강동윤 (Donny) <[email protected]>
  • Loading branch information
sokra and kdy1 committed Jun 10, 2024
1 parent eccccae commit 4061e06
Show file tree
Hide file tree
Showing 32 changed files with 5,115 additions and 393 deletions.
39 changes: 21 additions & 18 deletions crates/turbopack-ecmascript/src/tree_shake/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -698,10 +698,11 @@ impl DepGraph {
{
let used_ids = ids_used_by_ignoring_nested(
&export.decl,
[unresolved_ctxt, top_level_ctxt],
unresolved_ctxt,
top_level_ctxt,
);
let captured_ids =
ids_captured_by(&export.decl, [unresolved_ctxt, top_level_ctxt]);
ids_captured_by(&export.decl, unresolved_ctxt, top_level_ctxt);
let data = ItemData {
read_vars: used_ids.read,
eventual_read_vars: captured_ids.read,
Expand Down Expand Up @@ -736,10 +737,11 @@ impl DepGraph {

let mut used_ids = ids_used_by_ignoring_nested(
&export.expr,
[unresolved_ctxt, top_level_ctxt],
unresolved_ctxt,
top_level_ctxt,
);
let captured_ids =
ids_captured_by(&export.expr, [unresolved_ctxt, top_level_ctxt]);
ids_captured_by(&export.expr, unresolved_ctxt, top_level_ctxt);

used_ids.write.insert(default_var.to_id());

Expand Down Expand Up @@ -871,7 +873,7 @@ impl DepGraph {
};
ids.push(id.clone());

let vars = ids_used_by(&f.function, [unresolved_ctxt, top_level_ctxt]);
let vars = ids_used_by(&f.function, unresolved_ctxt, top_level_ctxt);
let var_decls = {
let mut v = IndexSet::with_capacity_and_hasher(1, Default::default());
v.insert(f.ident.to_id());
Expand Down Expand Up @@ -901,7 +903,7 @@ impl DepGraph {
};
ids.push(id.clone());

let vars = ids_used_by(&c.class, [unresolved_ctxt, top_level_ctxt]);
let vars = ids_used_by(&c.class, unresolved_ctxt, top_level_ctxt);
let var_decls = {
let mut v = IndexSet::with_capacity_and_hasher(1, Default::default());
v.insert(c.ident.to_id());
Expand Down Expand Up @@ -935,10 +937,13 @@ impl DepGraph {
let decl_ids: Vec<Id> = find_pat_ids(&decl.name);
let vars = ids_used_by_ignoring_nested(
&decl.init,
[unresolved_ctxt, top_level_ctxt],
unresolved_ctxt,
top_level_ctxt,
);
let eventual_vars =
ids_captured_by(&decl.init, [unresolved_ctxt, top_level_ctxt]);
ids_captured_by(&decl.init, unresolved_ctxt, top_level_ctxt);

let side_effects = vars.found_unresolved;

let var_decl = Box::new(VarDecl {
decls: vec![decl.clone()],
Expand All @@ -954,6 +959,7 @@ impl DepGraph {
write_vars: decl_ids.into_iter().chain(vars.write).collect(),
eventual_write_vars: eventual_vars.write,
content,
side_effects,
..Default::default()
},
);
Expand All @@ -965,25 +971,22 @@ impl DepGraph {
..
})) => {
let mut used_ids =
ids_used_by_ignoring_nested(item, [unresolved_ctxt, top_level_ctxt]);
let captured_ids = ids_captured_by(item, [unresolved_ctxt, top_level_ctxt]);
ids_used_by_ignoring_nested(item, unresolved_ctxt, top_level_ctxt);
let captured_ids = ids_captured_by(item, unresolved_ctxt, top_level_ctxt);

if assign.op != op!("=") {
used_ids.read.extend(used_ids.write.iter().cloned());

let extra_ids = ids_used_by_ignoring_nested(
&assign.left,
[unresolved_ctxt, top_level_ctxt],
unresolved_ctxt,
top_level_ctxt,
);
used_ids.read.extend(extra_ids.read);
used_ids.write.extend(extra_ids.write);
}

let side_effects = used_ids
.read
.iter()
.chain(used_ids.write.iter())
.any(|id| id.1 == unresolved_ctxt);
let side_effects = used_ids.found_unresolved;

let data = ItemData {
read_vars: used_ids.read,
Expand Down Expand Up @@ -1013,8 +1016,8 @@ impl DepGraph {
// Default to normal

let used_ids =
ids_used_by_ignoring_nested(item, [unresolved_ctxt, top_level_ctxt]);
let captured_ids = ids_captured_by(item, [unresolved_ctxt, top_level_ctxt]);
ids_used_by_ignoring_nested(item, unresolved_ctxt, top_level_ctxt);
let captured_ids = ids_captured_by(item, unresolved_ctxt, top_level_ctxt);
let data = ItemData {
read_vars: used_ids.read,
eventual_read_vars: captured_ids.read,
Expand Down
Loading

0 comments on commit 4061e06

Please sign in to comment.