Skip to content

Commit

Permalink
refactor(visit): Remove &dyn Node from Visit (#2984)
Browse files Browse the repository at this point in the history
swc_visit_macros:
 - Remove `&dyn Node` from `Visit`.
 - Implement `VisitWith<V>` for `[T]`.
  • Loading branch information
kdy1 authored Dec 7, 2021
1 parent f052a65 commit e48263b
Show file tree
Hide file tree
Showing 78 changed files with 1,316 additions and 1,442 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/bench.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ jobs:
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly-2021-09-30
override: true

- name: Run benchmark
run: cargo bench --all --exclude swc_plugin | tee output.txt
run: cargo +nightly-2021-09-30 bench --all --exclude swc_plugin | tee output.txt

- name: Download previous benchmark results
run: mkdir raw-data && curl -o raw-data/benchmark-data.json https://raw.githubusercontent.com/swc-project/raw-data/gh-pages/benchmark-data.json
Expand Down
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,6 @@ rust-toolchain
.husky/
.prettierrc
crates/
packages/
cspell.json
deny.toml
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ backtrace = "0.3"
napi = {version = "1", features = ["serde-json"]}
napi-derive = {version = "1"}
path-clean = "0.1"
proc-macro2 = "=1.0.32"
serde = {version = "1", features = ["derive"]}
serde_json = {version = "1", features = ["unbounded_depth"]}
swc = {path = "../swc", features = ["concurrent", "wrong-target", "plugin"]}
Expand Down
10 changes: 5 additions & 5 deletions crates/swc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ use swc_common::{
input::StringInput,
source_map::SourceMapGenConfig,
sync::Lrc,
BytePos, FileName, Globals, Mark, SourceFile, SourceMap, Spanned, DUMMY_SP, GLOBALS,
BytePos, FileName, Globals, Mark, SourceFile, SourceMap, Spanned, GLOBALS,
};
use swc_ecma_ast::{EsVersion, Ident, Invalid, Program};
use swc_ecma_ast::{EsVersion, Ident, Program};
use swc_ecma_codegen::{self, text_writer::WriteJs, Emitter, Node};
use swc_ecma_loader::resolvers::{
lru::CachingResolver, node::NodeModulesResolver, tsc::TsConfigResolver,
Expand Down Expand Up @@ -982,7 +982,7 @@ impl Compiler {
names: Default::default(),
};

module.visit_with(&Invalid { span: DUMMY_SP }, &mut v);
module.visit_with(&mut v);

v.names
};
Expand Down Expand Up @@ -1052,7 +1052,7 @@ impl Compiler {
names: Default::default(),
};

program.visit_with(&Invalid { span: DUMMY_SP }, &mut v);
program.visit_with(&mut v);

v.names
};
Expand Down Expand Up @@ -1117,7 +1117,7 @@ pub struct IdentCollector {
impl Visit for IdentCollector {
noop_visit_type!();

fn visit_ident(&mut self, ident: &Ident, _: &dyn swc_ecma_visit::Node) {
fn visit_ident(&mut self, ident: &Ident) {
self.names.insert(ident.span.lo, ident.sym.clone());
}
}
10 changes: 5 additions & 5 deletions crates/swc_bundler/src/bundler/chunk/computed_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use swc_atoms::js_word;
use swc_common::{SyntaxContext, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_utils::{find_ids, private_ident, ExprFactory};
use swc_ecma_visit::{noop_fold_type, noop_visit_type, Fold, Node, Visit};
use swc_ecma_visit::{noop_fold_type, noop_visit_type, Fold, Visit};

impl<L, R> Bundler<'_, L, R>
where
Expand Down Expand Up @@ -186,11 +186,11 @@ struct TopLevelAwaitFinder {
impl Visit for TopLevelAwaitFinder {
noop_visit_type!();

fn visit_function(&mut self, _: &Function, _: &dyn Node) {}
fn visit_arrow_expr(&mut self, _: &ArrowExpr, _: &dyn Node) {}
fn visit_class_member(&mut self, _: &ClassMember, _: &dyn Node) {}
fn visit_function(&mut self, _: &Function) {}
fn visit_arrow_expr(&mut self, _: &ArrowExpr) {}
fn visit_class_member(&mut self, _: &ClassMember) {}

fn visit_await_expr(&mut self, _: &AwaitExpr, _: &dyn Node) {
fn visit_await_expr(&mut self, _: &AwaitExpr) {
self.found = true;
}
}
Expand Down
8 changes: 4 additions & 4 deletions crates/swc_bundler/src/bundler/finalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use swc_ecma_transforms_base::{
hygiene::hygiene,
};
use swc_ecma_utils::{find_ids, private_ident, ExprFactory};
use swc_ecma_visit::{noop_fold_type, noop_visit_type, Fold, FoldWith, Node, Visit, VisitWith};
use swc_ecma_visit::{noop_fold_type, noop_visit_type, Fold, FoldWith, Visit, VisitWith};

impl<L, R> Bundler<'_, L, R>
where
Expand Down Expand Up @@ -143,7 +143,7 @@ where
}

let mut top_level_await_finder = TopLevelAwaitFinder::default();
module.visit_with(&Invalid { span: DUMMY_SP }, &mut top_level_await_finder);
module.visit_with(&mut top_level_await_finder);

let is_async = top_level_await_finder.found;

Expand Down Expand Up @@ -352,9 +352,9 @@ struct TopLevelAwaitFinder {
impl Visit for TopLevelAwaitFinder {
noop_visit_type!();

fn visit_stmts(&mut self, _: &[Stmt], _: &dyn Node) {}
fn visit_stmts(&mut self, _: &[Stmt]) {}

fn visit_await_expr(&mut self, _: &AwaitExpr, _: &dyn Node) {
fn visit_await_expr(&mut self, _: &AwaitExpr) {
self.found = true;
}
}
Expand Down
18 changes: 9 additions & 9 deletions crates/swc_bundler/src/bundler/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ use is_macro::Is;
#[cfg(feature = "rayon")]
use rayon::iter::ParallelIterator;
use swc_atoms::js_word;
use swc_common::{sync::Lrc, FileName, SourceFile, SyntaxContext, DUMMY_SP};
use swc_common::{sync::Lrc, FileName, SourceFile, SyntaxContext};
use swc_ecma_ast::{
CallExpr, Expr, ExprOrSuper, Ident, ImportDecl, ImportSpecifier, Invalid, MemberExpr, Module,
CallExpr, Expr, ExprOrSuper, Ident, ImportDecl, ImportSpecifier, MemberExpr, Module,
ModuleDecl, Str,
};
use swc_ecma_transforms_base::resolver::resolver_with_mark;
use swc_ecma_visit::{
noop_visit_mut_type, noop_visit_type, FoldWith, Node, Visit, VisitMut, VisitMutWith, VisitWith,
noop_visit_mut_type, noop_visit_type, FoldWith, Visit, VisitMut, VisitMutWith, VisitWith,
};
/// Module after applying transformations.
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -183,7 +183,7 @@ where
forced_es6: false,
found_other: false,
};
module.visit_with(&Invalid { span: DUMMY_SP } as _, &mut v);
module.visit_with(&mut v);
v.forced_es6 || !v.found_other
};

Expand Down Expand Up @@ -415,7 +415,7 @@ struct Es6ModuleDetector {
impl Visit for Es6ModuleDetector {
noop_visit_type!();

fn visit_call_expr(&mut self, e: &CallExpr, _: &dyn Node) {
fn visit_call_expr(&mut self, e: &CallExpr) {
e.visit_children_with(self);

match &e.callee {
Expand All @@ -432,11 +432,11 @@ impl Visit for Es6ModuleDetector {
}
}

fn visit_member_expr(&mut self, e: &MemberExpr, _: &dyn Node) {
e.obj.visit_with(e as _, self);
fn visit_member_expr(&mut self, e: &MemberExpr) {
e.obj.visit_with(self);

if e.computed {
e.prop.visit_with(e as _, self);
e.prop.visit_with(self);
}

match &e.obj {
Expand All @@ -462,7 +462,7 @@ impl Visit for Es6ModuleDetector {
//
}

fn visit_module_decl(&mut self, decl: &ModuleDecl, _: &dyn Node) {
fn visit_module_decl(&mut self, decl: &ModuleDecl) {
match decl {
ModuleDecl::Import(_)
| ModuleDecl::ExportDecl(_)
Expand Down
12 changes: 6 additions & 6 deletions crates/swc_bundler/src/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{id::Id, modules::Modules, util::Readonly};
use swc_common::{collections::AHashMap, SyntaxContext, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_visit::{
noop_visit_mut_type, noop_visit_type, Node, Visit, VisitMut, VisitMutWith, VisitWith,
noop_visit_mut_type, noop_visit_type, Visit, VisitMut, VisitMutWith, VisitWith,
};

#[derive(Debug, Default)]
Expand Down Expand Up @@ -60,23 +60,23 @@ impl Visit for Analyzer<'_> {
noop_visit_type!();

/// Noop
fn visit_module_decl(&mut self, _: &ModuleDecl, _: &dyn Node) {}
fn visit_module_decl(&mut self, _: &ModuleDecl) {}

/// Noop. We don't inline variables declared in subscopes.
fn visit_function(&mut self, _: &Function, _: &dyn Node) {}
fn visit_function(&mut self, _: &Function) {}

/// Noop. We don't inline variables declared in subscopes.
fn visit_block_stmt(&mut self, _: &BlockStmt, _: &dyn Node) {}
fn visit_block_stmt(&mut self, _: &BlockStmt) {}

fn visit_var_decl(&mut self, n: &VarDecl, _: &dyn Node) {
fn visit_var_decl(&mut self, n: &VarDecl) {
if n.span.ctxt != self.injected_ctxt || n.kind != VarDeclKind::Const {
return;
}

n.visit_children_with(self);
}

fn visit_var_declarator(&mut self, n: &VarDeclarator, _: &dyn Node) {
fn visit_var_declarator(&mut self, n: &VarDeclarator) {
n.visit_children_with(self);
match (&n.name, n.init.as_deref()) {
(Pat::Ident(from), Some(Expr::Ident(to))) => {
Expand Down
3 changes: 1 addition & 2 deletions crates/swc_bundler/src/modules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,7 @@ impl Modules {
where
V: Visit,
{
self.iter()
.for_each(|item| item.1.visit_with(&Invalid { span: DUMMY_SP }, v));
self.iter().for_each(|item| item.1.visit_with(v));
}

pub fn retain_mut<F>(&mut self, mut op: F)
Expand Down
Loading

1 comment on commit e48263b

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: e48263b Previous: 694d3c5 Ratio
full_es2015 180947048 ns/iter (± 7435188) 174448729 ns/iter (± 5464821) 1.04
full_es2016 145623904 ns/iter (± 4980080) 135495008 ns/iter (± 4690202) 1.07
full_es2017 150979140 ns/iter (± 5571700) 144857395 ns/iter (± 9972349) 1.04
full_es2018 150240700 ns/iter (± 5049876) 140320111 ns/iter (± 11548489) 1.07
full_es2019 148393449 ns/iter (± 5020455) 141911868 ns/iter (± 8324528) 1.05
full_es2020 147999639 ns/iter (± 4898360) 142655588 ns/iter (± 12188858) 1.04
full_es3 204631708 ns/iter (± 14322455) 214003322 ns/iter (± 21647041) 0.96
full_es5 192022259 ns/iter (± 10126170) 191582972 ns/iter (± 10595994) 1.00
parser 686465 ns/iter (± 29632) 653378 ns/iter (± 19915) 1.05

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.