Skip to content

Commit d257752

Browse files
committed
perf(turbopack): Use last side effect as the module evaluation node
1 parent 0cf1a28 commit d257752

File tree

2 files changed

+36
-25
lines changed

2 files changed

+36
-25
lines changed

Diff for: turbopack/crates/turbopack-ecmascript/src/tree_shake/graph.rs

+30-22
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ pub(crate) enum ItemId {
4444

4545
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
4646
pub(crate) enum ItemIdGroupKind {
47+
/// Used only for testing
48+
#[allow(unused)]
4749
ModuleEvaluation,
4850
/// `(local, export_name)``
4951
Export(Id, Atom),
@@ -130,6 +132,8 @@ pub(crate) struct ItemData {
130132

131133
/// Server actions breaks when we merge exports.
132134
pub disable_export_merging: bool,
135+
136+
pub is_module_evaluation: bool,
133137
}
134138

135139
impl fmt::Debug for ItemData {
@@ -145,6 +149,8 @@ impl fmt::Debug for ItemData {
145149
.field("side_effects", &self.side_effects)
146150
.field("export", &self.export)
147151
.field("explicit_deps", &self.explicit_deps)
152+
.field("disable_export_merging", &self.disable_export_merging)
153+
.field("is_module_evaluation", &self.is_module_evaluation)
148154
.finish()
149155
}
150156
}
@@ -165,6 +171,7 @@ impl Default for ItemData {
165171
binding_source: Default::default(),
166172
explicit_deps: Default::default(),
167173
disable_export_merging: Default::default(),
174+
is_module_evaluation: Default::default(),
168175
}
169176
}
170177
}
@@ -389,10 +396,10 @@ impl DepGraph {
389396
}
390397
}
391398

392-
for item in group {
393-
match item {
399+
for item_id in group {
400+
match item_id {
394401
ItemId::Group(ItemIdGroupKind::Export(..)) => {
395-
if let Some(export) = &data[item].export {
402+
if let Some(export) = &data[item_id].export {
396403
outputs.insert(Key::Export(export.as_str().into()), ix as u32);
397404

398405
let s = ExportSpecifier::Named(ExportNamedSpecifier {
@@ -418,11 +425,12 @@ impl DepGraph {
418425
));
419426
}
420427
}
421-
ItemId::Group(ItemIdGroupKind::ModuleEvaluation) => {
422-
outputs.insert(Key::ModuleEvaluation, ix as u32);
423-
}
424428

425-
_ => {}
429+
_ => {
430+
if data[item_id].is_module_evaluation {
431+
outputs.insert(Key::ModuleEvaluation, ix as u32);
432+
}
433+
}
426434
}
427435
}
428436

@@ -1369,21 +1377,21 @@ impl DepGraph {
13691377
}
13701378
}
13711379

1372-
{
1373-
// `module evaluation side effects` Node
1374-
let id = ItemId::Group(ItemIdGroupKind::ModuleEvaluation);
1375-
ids.push(id.clone());
1376-
items.insert(
1377-
id,
1378-
ItemData {
1379-
content: ModuleItem::Stmt(Stmt::Expr(ExprStmt {
1380-
span: DUMMY_SP,
1381-
expr: "module evaluation".into(),
1382-
})),
1383-
..Default::default()
1384-
},
1385-
);
1386-
}
1380+
// {
1381+
// // `module evaluation side effects` Node
1382+
// let id = ItemId::Group(ItemIdGroupKind::ModuleEvaluation);
1383+
// ids.push(id.clone());
1384+
// items.insert(
1385+
// id,
1386+
// ItemData {
1387+
// content: ModuleItem::Stmt(Stmt::Expr(ExprStmt {
1388+
// span: DUMMY_SP,
1389+
// expr: "module evaluation".into(),
1390+
// })),
1391+
// ..Default::default()
1392+
// },
1393+
// );
1394+
// }
13871395

13881396
for (local, export_name, disable_export_merging) in exports {
13891397
let id = ItemId::Group(ItemIdGroupKind::Export(local.clone(), export_name.clone()));

Diff for: turbopack/crates/turbopack-ecmascript/src/tree_shake/mod.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -354,10 +354,13 @@ impl Analyzer<'_> {
354354
if let ItemId::Group(kind) = item_id {
355355
match kind {
356356
ItemIdGroupKind::ModuleEvaluation => {
357-
// Create a strong dependency to LAST_SIDE_EFFECTS
357+
// We use last side effect as a module evaluation
358358

359-
self.g
360-
.add_strong_deps(item_id, self.last_side_effects.last());
359+
if let Some(last) = self.last_side_effects.last() {
360+
if let Some(item) = self.items.get_mut(last) {
361+
item.is_module_evaluation = true;
362+
}
363+
}
361364
}
362365
ItemIdGroupKind::Export(local, _) => {
363366
// Create a strong dependency to LAST_WRITES for this var

0 commit comments

Comments
 (0)