Skip to content

Commit

Permalink
perf(es/resolver): Remove needless allocations (#10120)
Browse files Browse the repository at this point in the history
**Description:**

Just use iter_mut().for_each directly.
  • Loading branch information
xusd320 authored Feb 28, 2025
1 parent dc50ba5 commit f019d53
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 30 deletions.
6 changes: 6 additions & 0 deletions .changeset/fifty-grapes-remain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
swc_core: minor
swc_ecma_transforms_base: minor
---

perf: remove needless collect in resolver
51 changes: 21 additions & 30 deletions crates/swc_ecma_transforms_base/src/resolver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1957,39 +1957,30 @@ impl VisitMut for Hoister<'_, '_> {
/// that there is already an global declaration of Ic when deal with the try
/// block.
fn visit_mut_module_items(&mut self, items: &mut Vec<ModuleItem>) {
let others = items
.iter_mut()
.filter_map(|item| match item {
ModuleItem::Stmt(Stmt::Decl(Decl::Var(v)))
| ModuleItem::ModuleDecl(ModuleDecl::ExportDecl(ExportDecl {
decl: Decl::Var(v),
..
})) if matches!(
&**v,
VarDecl {
kind: VarDeclKind::Var,
..
}
) =>
{
item.visit_mut_with(self);
None
}

ModuleItem::Stmt(Stmt::Decl(Decl::Fn(..)))
| ModuleItem::ModuleDecl(ModuleDecl::ExportDecl(ExportDecl {
decl: Decl::Fn(..),
items.iter_mut().for_each(|item| match item {
ModuleItem::Stmt(Stmt::Decl(Decl::Var(v)))
| ModuleItem::ModuleDecl(ModuleDecl::ExportDecl(ExportDecl {
decl: Decl::Var(v),
..
})) if matches!(
&**v,
VarDecl {
kind: VarDeclKind::Var,
..
})) => {
item.visit_mut_with(self);
None
}
_ => Some(item),
})
.collect::<Vec<_>>();
) =>
{
item.visit_mut_with(self);
}

others.into_iter().for_each(|item| {
item.visit_mut_with(self);
ModuleItem::Stmt(Stmt::Decl(Decl::Fn(..)))
| ModuleItem::ModuleDecl(ModuleDecl::ExportDecl(ExportDecl {
decl: Decl::Fn(..),
..
})) => {
item.visit_mut_with(self);
}
_ => item.visit_mut_with(self),
});
}

Expand Down

0 comments on commit f019d53

Please sign in to comment.