Skip to content

Commit

Permalink
perf(es/visit): Introduce standard_only_* macros (#9207)
Browse files Browse the repository at this point in the history
**Description:**

These macros are optimization hint on release builds.

**Related issue:**

 - Closes #9204
  • Loading branch information
kdy1 authored Jul 11, 2024
1 parent 91cef80 commit 92879b1
Show file tree
Hide file tree
Showing 250 changed files with 1,120 additions and 8,339 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions crates/dbg-swc/src/util/minifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use swc_common::{FileName, SourceMap};
use swc_ecma_ast::*;
use swc_ecma_minifier::option::{CompressOptions, MangleOptions, MinifyOptions};
use swc_ecma_transforms_base::fixer::fixer;
use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith};
use swc_ecma_visit::{standard_only_visit_mut, VisitMut, VisitMutWith};

use super::{parse_js, print_js, wrap_task, ModuleRecord};

Expand Down Expand Up @@ -136,7 +136,7 @@ pub fn get_esbuild_output(file: &Path, mangle: bool) -> Result<String> {
struct Normalizer {}

impl VisitMut for Normalizer {
noop_visit_mut_type!();
standard_only_visit_mut!();

fn visit_mut_prop(&mut self, p: &mut Prop) {
p.visit_mut_children_with(self);
Expand Down
2 changes: 1 addition & 1 deletion crates/swc/benches/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ fn as_es(c: &swc::Compiler) -> Program {

program
.fold_with(&mut resolver(unresolved_mark, top_level_mark, true))
.fold_with(&mut typescript::strip(top_level_mark))
.fold_with(&mut typescript::strip(unresolved_mark, top_level_mark))
}

fn base_tr_group(c: &mut Criterion) {
Expand Down
4 changes: 2 additions & 2 deletions crates/swc/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use swc_ecma_transforms::{
pass::Optional,
resolver, Assumptions,
};
use swc_ecma_visit::{as_folder, noop_visit_mut_type, VisitMut, VisitMutWith};
use swc_ecma_visit::{as_folder, standard_only_visit_mut, VisitMut, VisitMutWith};

use crate::config::{CompiledPaths, GlobalPassOption, JsMinifyOptions, ModuleConfig};

Expand Down Expand Up @@ -382,7 +382,7 @@ struct MinifierPass<'a> {
}

impl VisitMut for MinifierPass<'_> {
noop_visit_mut_type!();
standard_only_visit_mut!();

fn visit_mut_module(&mut self, m: &mut Module) {
if let Some(options) = &self.options {
Expand Down
1 change: 1 addition & 0 deletions crates/swc/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,7 @@ impl Options {
),
},
comments.map(|v| v as _),
unresolved_mark,
top_level_mark
),
syntax.typescript()
Expand Down
4 changes: 2 additions & 2 deletions crates/swc/src/dropped_comments_preserver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use swc_common::{
BytePos, Span, DUMMY_SP,
};
use swc_ecma_ast::{Module, Script};
use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith};
use swc_ecma_visit::{as_folder, standard_only_visit_mut, Fold, VisitMut, VisitMutWith};

/// Preserves comments that would otherwise be dropped.
///
Expand Down Expand Up @@ -36,7 +36,7 @@ struct DroppedCommentsPreserver {
type CommentEntries = Vec<(BytePos, Vec<Comment>)>;

impl VisitMut for DroppedCommentsPreserver {
noop_visit_mut_type!();
standard_only_visit_mut!();

fn visit_mut_module(&mut self, module: &mut Module) {
module.visit_mut_children_with(self);
Expand Down
4 changes: 2 additions & 2 deletions crates/swc/tests/rust_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ use swc_common::{comments::SingleThreadedComments, FileName};
use swc_ecma_ast::*;
use swc_ecma_parser::{EsSyntax, Syntax, TsSyntax};
use swc_ecma_transforms::pass::noop;
use swc_ecma_visit::{as_folder, noop_visit_mut_type, VisitMut};
use swc_ecma_visit::{as_folder, standard_only_visit_mut, VisitMut};

struct PanicOnVisit;

impl VisitMut for PanicOnVisit {
noop_visit_mut_type!();
standard_only_visit_mut!();

fn visit_mut_number(&mut self, n: &mut Number) {
panic!("Expected {:?}", n.value)
Expand Down
8 changes: 4 additions & 4 deletions crates/swc_bundler/src/bundler/chunk/cjs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use anyhow::Error;
use swc_common::{collections::AHashMap, Span, SyntaxContext, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_utils::{quote_ident, ExprFactory};
use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith};
use swc_ecma_visit::{standard_only_visit_mut, VisitMut, VisitMutWith};

use crate::{
bundler::{chunk::merge::Ctx, load::TransformedModule},
Expand Down Expand Up @@ -192,7 +192,7 @@ where
L: Load,
R: Resolve,
{
noop_visit_mut_type!();
standard_only_visit_mut!();

fn visit_mut_call_expr(&mut self, node: &mut CallExpr) {
node.visit_mut_children_with(self);
Expand Down Expand Up @@ -350,7 +350,7 @@ struct DefaultHandler {
}

impl VisitMut for DefaultHandler {
noop_visit_mut_type!();
standard_only_visit_mut!();

fn visit_mut_expr(&mut self, e: &mut Expr) {
e.visit_mut_children_with(self);
Expand All @@ -375,7 +375,7 @@ struct Remapper {
}

impl VisitMut for Remapper {
noop_visit_mut_type!();
standard_only_visit_mut!();

fn visit_mut_ident(&mut self, i: &mut Ident) {
if let Some(v) = self.vars.get(&i.to_id()).copied() {
Expand Down
4 changes: 2 additions & 2 deletions crates/swc_bundler/src/bundler/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use swc_atoms::JsWord;
use swc_common::{collections::ARandomState, FileName, SyntaxContext};
use swc_ecma_ast::*;
use swc_ecma_utils::find_pat_ids;
use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith};
use swc_ecma_visit::{standard_only_visit_mut, VisitMut, VisitMutWith};

use super::{
load::{Source, Specifier},
Expand Down Expand Up @@ -115,7 +115,7 @@ where
L: Load,
R: Resolve,
{
noop_visit_mut_type!();
standard_only_visit_mut!();

fn visit_mut_module_item(&mut self, item: &mut ModuleItem) {
match item {
Expand Down
4 changes: 2 additions & 2 deletions crates/swc_bundler/src/bundler/import/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use swc_common::{
};
use swc_ecma_ast::*;
use swc_ecma_utils::find_pat_ids;
use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith};
use swc_ecma_visit::{standard_only_visit_mut, VisitMut, VisitMutWith};

use super::Bundler;
use crate::{load::Load, resolve::Resolve};
Expand Down Expand Up @@ -368,7 +368,7 @@ where
L: Load,
R: Resolve,
{
noop_visit_mut_type!();
standard_only_visit_mut!();

fn visit_mut_export_named_specifier(&mut self, s: &mut ExportNamedSpecifier) {
let orig = match &s.orig {
Expand Down
4 changes: 2 additions & 2 deletions crates/swc_bundler/src/bundler/keywords.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use swc_common::{collections::AHashMap, util::take::Take};
use swc_ecma_ast::*;
use swc_ecma_utils::private_ident;
use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith};
use swc_ecma_visit::{standard_only_visit_mut, VisitMut, VisitMutWith};

use crate::id::Id;

Expand Down Expand Up @@ -31,7 +31,7 @@ impl KeywordRenamer {
}

impl VisitMut for KeywordRenamer {
noop_visit_mut_type!();
standard_only_visit_mut!();

fn visit_mut_binding_ident(&mut self, n: &mut BindingIdent) {
if let Some(new) = self.renamed(&n.id) {
Expand Down
4 changes: 2 additions & 2 deletions crates/swc_bundler/src/bundler/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use swc_ecma_ast::{
};
use swc_ecma_transforms_base::resolver;
use swc_ecma_visit::{
noop_visit_mut_type, noop_visit_type, FoldWith, Visit, VisitMut, VisitMutWith, VisitWith,
noop_visit_type, standard_only_visit_mut, FoldWith, Visit, VisitMut, VisitMutWith, VisitWith,
};

use super::{export::Exports, helpers::Helpers, Bundler};
Expand Down Expand Up @@ -486,7 +486,7 @@ impl Visit for Es6ModuleDetector {
#[derive(Clone, Copy)]
struct ClearMark;
impl VisitMut for ClearMark {
noop_visit_mut_type!();
standard_only_visit_mut!();

fn visit_mut_ident(&mut self, ident: &mut Ident) {
ident.span.ctxt = SyntaxContext::empty();
Expand Down
4 changes: 2 additions & 2 deletions crates/swc_bundler/src/inline.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use swc_common::{collections::AHashMap, SyntaxContext, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_visit::{
noop_visit_mut_type, noop_visit_type, visit_mut_obj_and_computed, Visit, VisitMut,
noop_visit_type, standard_only_visit_mut, visit_mut_obj_and_computed, Visit, VisitMut,
VisitMutWith, VisitWith,
};

Expand Down Expand Up @@ -84,7 +84,7 @@ impl Visit for Analyzer<'_> {
}

impl VisitMut for Inliner {
noop_visit_mut_type!();
standard_only_visit_mut!();

visit_mut_obj_and_computed!();

Expand Down
4 changes: 2 additions & 2 deletions crates/swc_bundler/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::hash::Hash;
use swc_common::{Span, SyntaxContext, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_utils::ident::IdentLike;
use swc_ecma_visit::{noop_visit_mut_type, VisitMut};
use swc_ecma_visit::{standard_only_visit_mut, VisitMut};

#[cfg(feature = "concurrent")]
pub(crate) type Readonly<T> = std::sync::Arc<T>;
Expand Down Expand Up @@ -147,7 +147,7 @@ where
pub(crate) struct HygieneRemover;

impl VisitMut for HygieneRemover {
noop_visit_mut_type!();
standard_only_visit_mut!();

fn visit_mut_span(&mut self, s: &mut Span) {
*s = s.with_ctxt(SyntaxContext::empty())
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_bundler/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ impl Load for Loader {
emit_metadata: Default::default(),
use_define_for_class_fields: false,
}))
.fold_with(&mut strip(top_level_mark))
.fold_with(&mut strip(unresolved_mark, top_level_mark))
.fold_with(&mut react::<SingleThreadedComments>(
self.cm.clone(),
None,
Expand Down
4 changes: 2 additions & 2 deletions crates/swc_ecma_compat_bugfixes/src/async_arrows_in_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use swc_common::{util::take::Take, Mark, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_compat_es2015::arrow;
use swc_ecma_utils::prepend_stmt;
use swc_ecma_visit::{noop_fold_type, Fold, FoldWith, InjectVars};
use swc_ecma_visit::{standard_only_fold, Fold, FoldWith, InjectVars};
use swc_trace_macro::swc_trace;

/// Safari 10.3 had an issue where async arrow function expressions within any
Expand All @@ -26,7 +26,7 @@ struct AsyncArrowsInClass {
/// TODO: VisitMut
#[swc_trace]
impl Fold for AsyncArrowsInClass {
noop_fold_type!();
standard_only_fold!();

fn fold_class_method(&mut self, n: ClassMethod) -> ClassMethod {
self.in_class_method = true;
Expand Down
4 changes: 2 additions & 2 deletions crates/swc_ecma_compat_bugfixes/src/edge_default_param.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use swc_ecma_ast::*;
use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith};
use swc_ecma_visit::{as_folder, standard_only_visit_mut, Fold, VisitMut, VisitMutWith};
use swc_trace_macro::swc_trace;

/// Converts destructured parameters with default values to non-shorthand
Expand All @@ -16,7 +16,7 @@ struct EdgeDefaultParam {

#[swc_trace]
impl VisitMut for EdgeDefaultParam {
noop_visit_mut_type!();
standard_only_visit_mut!();

fn visit_mut_arrow_expr(&mut self, n: &mut ArrowExpr) {
self.in_arrow = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use swc_atoms::JsWord;
use swc_common::{collections::AHashSet, Span};
use swc_ecma_ast::*;
use swc_ecma_transforms_base::hygiene::rename;
use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith};
use swc_ecma_visit::{as_folder, standard_only_visit_mut, Fold, VisitMut, VisitMutWith};
use swc_trace_macro::swc_trace;

pub fn safari_id_destructuring_collision_in_function_expression() -> impl Fold + VisitMut {
Expand All @@ -31,7 +31,7 @@ impl SafariIdDestructuringCollisionInFunctionExpression {

#[swc_trace]
impl VisitMut for SafariIdDestructuringCollisionInFunctionExpression {
noop_visit_mut_type!();
standard_only_visit_mut!();

fn visit_mut_assign_pat_prop(&mut self, n: &mut AssignPatProp) {
self.visit_mut_pat_id(&n.key);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use swc_common::DUMMY_SP;
use swc_ecma_ast::*;
use swc_ecma_utils::{prepend_stmt, private_ident, ExprFactory};
use swc_ecma_visit::{noop_fold_type, Fold, FoldWith};
use swc_ecma_visit::{standard_only_fold, Fold, FoldWith};
use swc_trace_macro::swc_trace;

// Converts destructured parameters with default values to non-shorthand syntax.
Expand Down Expand Up @@ -55,7 +55,7 @@ impl TemplateLiteralCaching {
/// TODO: VisitMut
#[swc_trace]
impl Fold for TemplateLiteralCaching {
noop_fold_type!();
standard_only_fold!();

fn fold_expr(&mut self, n: Expr) -> Expr {
let n = n.fold_children_with(self);
Expand Down
4 changes: 2 additions & 2 deletions crates/swc_ecma_compat_common/src/regexp.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use swc_common::util::take::Take;
use swc_ecma_ast::{CallExpr, Expr, Lit, Regex};
use swc_ecma_utils::{quote_ident, ExprFactory};
use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith};
use swc_ecma_visit::{as_folder, standard_only_visit_mut, Fold, VisitMut, VisitMutWith};

pub fn regexp(config: Config) -> impl Fold + VisitMut {
as_folder(RegExp { config })
Expand Down Expand Up @@ -32,7 +32,7 @@ struct RegExp {
}

impl VisitMut for RegExp {
noop_visit_mut_type!();
standard_only_visit_mut!();

fn visit_mut_expr(&mut self, expr: &mut Expr) {
expr.visit_mut_children_with(self);
Expand Down
6 changes: 4 additions & 2 deletions crates/swc_ecma_compat_es2015/src/arrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use swc_ecma_utils::{
function::{init_this, FnEnvHoister},
prepend_stmt,
};
use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, InjectVars, VisitMut, VisitMutWith};
use swc_ecma_visit::{
as_folder, standard_only_visit_mut, Fold, InjectVars, VisitMut, VisitMutWith,
};
use swc_trace_macro::swc_trace;

/// Compile ES2015 arrow functions to ES5
Expand Down Expand Up @@ -72,7 +74,7 @@ struct Arrow {

#[swc_trace]
impl VisitMut for Arrow {
noop_visit_mut_type!();
standard_only_visit_mut!();

fn visit_mut_class(&mut self, c: &mut Class) {
if c.super_class.is_some() {
Expand Down
4 changes: 2 additions & 2 deletions crates/swc_ecma_compat_es2015/src/block_scoped_fn.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use swc_common::{util::take::Take, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_utils::IdentUsageFinder;
use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith};
use swc_ecma_visit::{as_folder, standard_only_visit_mut, Fold, VisitMut, VisitMutWith};
use swc_trace_macro::swc_trace;

pub fn block_scoped_functions() -> impl Fold + VisitMut {
Expand All @@ -13,7 +13,7 @@ struct BlockScopedFns;

#[swc_trace]
impl VisitMut for BlockScopedFns {
noop_visit_mut_type!();
standard_only_visit_mut!();

fn visit_mut_function(&mut self, n: &mut Function) {
let Some(body) = &mut n.body else { return };
Expand Down
Loading

0 comments on commit 92879b1

Please sign in to comment.