Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(visit): Introduce Pass API and adjust visitor APIs for it #9680

Merged
merged 187 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from 180 commits
Commits
Show all changes
187 commits
Select commit Hold shift + click to select a range
5d4dd73
Remove `chain!`
kdy1 Oct 25, 2024
ad032c4
Patch `swc_common`
kdy1 Oct 25, 2024
4b3faf2
Fix `swc_ecma_visit`
kdy1 Oct 25, 2024
6253539
Take impl
kdy1 Oct 25, 2024
f4b5876
`Pass`
kdy1 Oct 25, 2024
a591a63
Consistent
kdy1 Oct 25, 2024
25065a7
fix more
kdy1 Oct 25, 2024
c40f40a
Fix `swc_ecma_visit`
kdy1 Oct 25, 2024
4c2fc70
Make `Pass` take `self`
kdy1 Oct 25, 2024
9db41e8
DropSpan
kdy1 Oct 25, 2024
74a88a5
Fix hygiene
kdy1 Oct 25, 2024
eadaef7
Expose visitor
kdy1 Oct 25, 2024
e03be5b
ProgramExt
kdy1 Oct 25, 2024
0c1ca9f
Move `Pass` to `swc_ecma_ast`
kdy1 Oct 25, 2024
e01128c
More work
kdy1 Oct 25, 2024
451916a
More work
kdy1 Oct 25, 2024
8049001
More work
kdy1 Oct 25, 2024
a1a1226
More work
kdy1 Oct 25, 2024
92d18c8
More work
kdy1 Oct 25, 2024
3e2d52c
More work
kdy1 Oct 25, 2024
084e650
More work
kdy1 Oct 25, 2024
ff363c8
Rename
kdy1 Oct 25, 2024
a75bd5f
More work
kdy1 Oct 25, 2024
d600acc
More work
kdy1 Oct 25, 2024
357f7f1
More work
kdy1 Oct 25, 2024
da8b380
Fix API
kdy1 Oct 25, 2024
18ab6aa
more
kdy1 Oct 25, 2024
d61756a
more
kdy1 Oct 25, 2024
02718ea
more
kdy1 Oct 25, 2024
abb2d0c
more
kdy1 Oct 25, 2024
175e165
more
kdy1 Oct 25, 2024
b73cf27
inline for AST
kdy1 Oct 25, 2024
9b6bee6
more
kdy1 Oct 25, 2024
91274fa
Improve Visitor API
kdy1 Oct 25, 2024
9a2cbcd
Improve visitor API
kdy1 Oct 25, 2024
1d27315
more
kdy1 Oct 25, 2024
377227c
more
kdy1 Oct 25, 2024
cce5e6f
more
kdy1 Oct 25, 2024
c68cad7
more
kdy1 Oct 25, 2024
46bc407
Replace
kdy1 Oct 25, 2024
ead5357
Move `from_fn`
kdy1 Oct 25, 2024
efbb968
Rename
kdy1 Oct 25, 2024
8eee307
noop_pass
kdy1 Oct 25, 2024
e83a8d9
inline attr
kdy1 Oct 25, 2024
86de8ea
Fix AST
kdy1 Oct 25, 2024
3dca4c8
More work
kdy1 Oct 25, 2024
a75d994
More work
kdy1 Oct 25, 2024
b992b15
More work
kdy1 Oct 25, 2024
e2d769f
Rename `visit_mut_pass`
kdy1 Oct 25, 2024
a265312
Rename `viistor`
kdy1 Oct 25, 2024
c511347
Rename `from_fold`
kdy1 Oct 25, 2024
fe2fba4
more work
kdy1 Oct 25, 2024
a8b3e37
more work
kdy1 Oct 25, 2024
0572103
more work
kdy1 Oct 25, 2024
15fd473
more work
kdy1 Oct 25, 2024
fe4ae14
more work
kdy1 Oct 25, 2024
ad8bad1
more work
kdy1 Oct 25, 2024
2763b7c
more work
kdy1 Oct 25, 2024
0c614b8
more work
kdy1 Oct 25, 2024
283a260
more work
kdy1 Oct 25, 2024
2ccfaf9
more work
kdy1 Oct 25, 2024
e6ca08d
more work
kdy1 Oct 25, 2024
e0dcb57
more work
kdy1 Oct 25, 2024
13a8991
more work
kdy1 Oct 25, 2024
7d7d4f9
more work
kdy1 Oct 25, 2024
977047f
more work
kdy1 Oct 25, 2024
f4b3ee7
more work
kdy1 Oct 25, 2024
c8d6666
more work
kdy1 Oct 25, 2024
474b35e
more work
kdy1 Oct 25, 2024
0dbd6a9
more work
kdy1 Oct 25, 2024
06cd5fd
more
kdy1 Oct 25, 2024
a18a990
more
kdy1 Oct 25, 2024
afa1665
more
kdy1 Oct 25, 2024
ae63398
more
kdy1 Oct 25, 2024
f582514
more
kdy1 Oct 25, 2024
c859333
more
kdy1 Oct 25, 2024
67cf6b3
more
kdy1 Oct 25, 2024
b929b6d
more
kdy1 Oct 25, 2024
a1dce2e
more
kdy1 Oct 25, 2024
4eef92e
more
kdy1 Oct 25, 2024
fe74c4f
more
kdy1 Oct 25, 2024
cf0f2eb
more
kdy1 Oct 25, 2024
f29fc29
impl Pass for Option<P: Pass>
kdy1 Oct 25, 2024
83cb704
more
kdy1 Oct 25, 2024
a3778d4
more
kdy1 Oct 25, 2024
34f5a26
more
kdy1 Oct 25, 2024
ed86e8a
more
kdy1 Oct 25, 2024
c23751e
more
kdy1 Oct 25, 2024
af4f7a7
more
kdy1 Oct 25, 2024
c4dc590
more
kdy1 Oct 25, 2024
138f0a2
more
kdy1 Oct 25, 2024
2d52db9
Improve Pass API
kdy1 Oct 25, 2024
740a29e
more
kdy1 Oct 25, 2024
8bd30d1
more
kdy1 Oct 25, 2024
8288697
more
kdy1 Oct 25, 2024
8abadf9
more
kdy1 Oct 25, 2024
05ebfc9
more
kdy1 Oct 25, 2024
c8b581d
more
kdy1 Oct 25, 2024
1572bd7
more
kdy1 Oct 25, 2024
19d47f1
more
kdy1 Oct 25, 2024
a2a816a
more
kdy1 Oct 25, 2024
867e412
more
kdy1 Oct 25, 2024
28e6fd1
more
kdy1 Oct 25, 2024
026305d
Pass : Sized
kdy1 Oct 25, 2024
3cd855c
more
kdy1 Oct 25, 2024
d401986
more
kdy1 Oct 25, 2024
86d9ea9
more
kdy1 Oct 25, 2024
a15693d
Repeated passes
kdy1 Oct 25, 2024
cc71ba6
more
kdy1 Oct 25, 2024
f66d48c
Make pass not : Sized
kdy1 Oct 25, 2024
0f8019a
fixup
kdy1 Oct 25, 2024
4c4db20
fixup
kdy1 Oct 25, 2024
73bfea0
more
kdy1 Oct 25, 2024
2093562
more
kdy1 Oct 25, 2024
2620767
Pass: `impl_pass_for_tuple`
kdy1 Oct 25, 2024
45e7313
More `impl_pass_for_tuple` invocation
kdy1 Oct 25, 2024
f0d1f91
Improve `chain!` macro
kdy1 Oct 25, 2024
8631581
More
kdy1 Oct 25, 2024
7449d7f
More
kdy1 Oct 25, 2024
d375b8c
More
kdy1 Oct 25, 2024
5fbd9e8
More
kdy1 Oct 25, 2024
1e62b6e
More
kdy1 Oct 25, 2024
4013444
CompilerPass
kdy1 Oct 25, 2024
6a880f9
More
kdy1 Oct 25, 2024
d759a7e
More
kdy1 Oct 25, 2024
ffc8f22
More
kdy1 Oct 25, 2024
509e251
More
kdy1 Oct 25, 2024
bd453ae
More
kdy1 Oct 25, 2024
8e05f2d
Work by myself
kdy1 Oct 25, 2024
95ac3b4
Fix more
kdy1 Oct 25, 2024
2aab07f
more
kdy1 Oct 25, 2024
37b1580
more fix
kdy1 Oct 25, 2024
6cd729b
Remove chain!
kdy1 Oct 25, 2024
f165438
chain
kdy1 Oct 25, 2024
6e93fe2
Pass for Either
kdy1 Oct 25, 2024
fc99a45
Fix more
kdy1 Oct 25, 2024
59ff145
Fix more
kdy1 Oct 25, 2024
2ed695d
Fix more
kdy1 Oct 25, 2024
f7f3f04
Fix more
kdy1 Oct 25, 2024
15de1f3
replace
kdy1 Oct 25, 2024
6ac173b
More fix
kdy1 Oct 25, 2024
1a519e5
More fix
kdy1 Oct 25, 2024
7291cfe
More fix
kdy1 Oct 25, 2024
2c3c7d7
More fix
kdy1 Oct 25, 2024
d77fcf6
More fix
kdy1 Oct 25, 2024
212eaa4
cargo lockfile
kdy1 Oct 25, 2024
a89b750
More fix
kdy1 Oct 25, 2024
6bcf858
More fix
kdy1 Oct 25, 2024
25aa2c6
Document
kdy1 Oct 25, 2024
46988ea
Document
kdy1 Oct 25, 2024
52bf044
done
kdy1 Oct 25, 2024
0ab16cf
fix
kdy1 Oct 25, 2024
ed43585
fix
kdy1 Oct 25, 2024
479f403
More work
kdy1 Oct 25, 2024
aafaa25
visitmut
kdy1 Oct 25, 2024
bbb86aa
Is
kdy1 Oct 25, 2024
79b770c
More woerk
kdy1 Oct 25, 2024
4923868
More work
kdy1 Oct 25, 2024
75b3d81
More work
kdy1 Oct 25, 2024
f1f58f2
More work
kdy1 Oct 25, 2024
30f86ef
More workd
kdy1 Oct 25, 2024
bc1650d
More work
kdy1 Oct 25, 2024
e281782
More work
kdy1 Oct 25, 2024
e466be3
More work
kdy1 Oct 25, 2024
6546cb9
More work
kdy1 Oct 25, 2024
6212bfd
More work
kdy1 Oct 25, 2024
543f866
More work
kdy1 Oct 25, 2024
ecb5ae0
Pass
kdy1 Oct 25, 2024
bfe1007
Pass
kdy1 Oct 25, 2024
ec87036
Pass
kdy1 Oct 25, 2024
c37be51
Pass
kdy1 Oct 25, 2024
d8350fa
Pass
kdy1 Oct 25, 2024
9d60d7e
Pass
kdy1 Oct 25, 2024
2cfeee2
Pass
kdy1 Oct 25, 2024
94705bd
Pass
kdy1 Oct 25, 2024
30cd5fb
Pass
kdy1 Oct 25, 2024
aacccc7
*
kdy1 Oct 26, 2024
4a4164a
visit_mut_ident
kdy1 Oct 27, 2024
3a834d1
fix visit/fold
kdy1 Oct 27, 2024
3067d0d
Merge branch 'main' into visitor-api
kdy1 Oct 27, 2024
ee3b7aa
Create few-pigs-shout.md
kdy1 Oct 27, 2024
1b570ee
Fix
kdy1 Oct 27, 2024
dc823ab
fix
kdy1 Oct 27, 2024
a153f20
Update few-pigs-shout.md
kdy1 Oct 29, 2024
a51ec2c
fix
kdy1 Oct 29, 2024
7c153c0
fix
kdy1 Oct 29, 2024
078e9f2
fix
kdy1 Oct 29, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

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

7 changes: 3 additions & 4 deletions crates/swc/benches/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use swc_compiler_base::PrintArgs;
use swc_ecma_ast::{EsVersion, Program};
use swc_ecma_parser::Syntax;
use swc_ecma_transforms::{fixer, resolver, typescript};
use swc_ecma_visit::FoldWith;

static SOURCE: &str = include_str!("assets/Observable.ts");

Expand Down Expand Up @@ -52,8 +51,8 @@ fn as_es(c: &swc::Compiler) -> Program {
let top_level_mark = Mark::new();

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

fn base_tr_group(c: &mut Criterion) {
Expand All @@ -73,7 +72,7 @@ fn base_tr_fixer(b: &mut Bencher) {
GLOBALS.set(&Default::default(), || {
let handler = Handler::with_emitter_writer(Box::new(stderr()), Some(c.cm.clone()));
black_box(c.run_transform(&handler, true, || {
module.clone().fold_with(&mut fixer(Some(c.comments())))
module.clone().apply(&mut fixer(Some(c.comments())))
}))
})
});
Expand Down
96 changes: 46 additions & 50 deletions crates/swc/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ use either::Either;
use rustc_hash::FxHashMap;
use swc_atoms::JsWord;
use swc_common::{
chain, comments::Comments, errors::Handler, sync::Lrc, util::take::Take, FileName, Mark,
SourceMap,
comments::Comments, errors::Handler, sync::Lrc, util::take::Take, FileName, Mark, SourceMap,
};
use swc_ecma_ast::{EsVersion, Module, Script};
use swc_ecma_ast::{EsVersion, Module, Pass, Script};
use swc_ecma_minifier::option::{terser::TerserTopLevelOptions, MinifyOptions};
use swc_ecma_parser::Syntax;
use swc_ecma_transforms::{
Expand All @@ -19,15 +18,15 @@ use swc_ecma_transforms::{
hygiene::{self, hygiene_with_config},
modules::{self, path::ImportResolver},
optimization::const_modules,
pass::Optional,
resolver, Assumptions,
};
use swc_ecma_visit::{as_folder, noop_visit_mut_type, VisitMut, VisitMutWith};
use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith};
use swc_visit::Optional;

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

/// Builder is used to create a high performance `Compiler`.
pub struct PassBuilder<'a, 'b, P: swc_ecma_visit::Fold> {
pub struct PassBuilder<'a, 'b, P: Pass> {
cm: &'a Arc<SourceMap>,
handler: &'b Handler,
env: Option<swc_ecma_preset_env::Config>,
Expand All @@ -48,7 +47,7 @@ pub struct PassBuilder<'a, 'b, P: swc_ecma_visit::Fold> {
regenerator: regenerator::Config,
}

impl<'a, 'b, P: swc_ecma_visit::Fold> PassBuilder<'a, 'b, P> {
impl<'a, 'b, P: Pass> PassBuilder<'a, 'b, P> {
pub fn new(
cm: &'a Arc<SourceMap>,
handler: &'b Handler,
Expand Down Expand Up @@ -76,11 +75,11 @@ impl<'a, 'b, P: swc_ecma_visit::Fold> PassBuilder<'a, 'b, P> {
}
}

pub fn then<N>(self, next: N) -> PassBuilder<'a, 'b, swc_visit::AndThen<P, N>>
pub fn then<N>(self, next: N) -> PassBuilder<'a, 'b, (P, N)>
where
N: swc_ecma_visit::Fold,
N: Pass,
{
let pass = chain!(self.pass, next);
let pass = (self.pass, next);
PassBuilder {
cm: self.cm,
handler: self.handler,
Expand Down Expand Up @@ -126,15 +125,12 @@ impl<'a, 'b, P: swc_ecma_visit::Fold> PassBuilder<'a, 'b, P> {
pub fn const_modules(
self,
globals: FxHashMap<JsWord, FxHashMap<JsWord, String>>,
) -> PassBuilder<'a, 'b, impl swc_ecma_visit::Fold> {
) -> PassBuilder<'a, 'b, (P, impl Pass)> {
let cm = self.cm.clone();
self.then(const_modules(cm, globals))
}

pub fn inline_globals(
self,
c: GlobalPassOption,
) -> PassBuilder<'a, 'b, impl swc_ecma_visit::Fold> {
pub fn inline_globals(self, c: GlobalPassOption) -> PassBuilder<'a, 'b, (P, impl Pass)> {
let pass = c.build(self.cm, self.handler);
self.then(pass)
}
Expand Down Expand Up @@ -172,7 +168,7 @@ impl<'a, 'b, P: swc_ecma_visit::Fold> PassBuilder<'a, 'b, P> {
module: Option<ModuleConfig>,
comments: Option<&'cmt dyn Comments>,
resolver: Option<(FileName, Arc<dyn ImportResolver>)>,
) -> impl 'cmt + swc_ecma_visit::Fold
) -> impl 'cmt + Pass
where
P: 'cmt,
{
Expand Down Expand Up @@ -206,7 +202,7 @@ impl<'a, 'b, P: swc_ecma_visit::Fold> PassBuilder<'a, 'b, P> {

feature_flag = enable_available_feature_from_es_version(self.target);

Either::Right(chain!(
Either::Right((
Optional::new(
compat::class_fields_use_set::class_fields_use_set(assumptions.pure_getters),
assumptions.set_public_class_fields,
Expand All @@ -221,60 +217,60 @@ impl<'a, 'b, P: swc_ecma_visit::Fold> PassBuilder<'a, 'b, P> {
no_document_all: assumptions.no_document_all,
static_blocks_mark: Mark::new(),
pure_getter: assumptions.pure_getters,
}
},
},
self.unresolved_mark
self.unresolved_mark,
),
should_enable(self.target, EsVersion::Es2022)
should_enable(self.target, EsVersion::Es2022),
),
Optional::new(
compat::es2021::es2021(),
should_enable(self.target, EsVersion::Es2021)
should_enable(self.target, EsVersion::Es2021),
),
Optional::new(
compat::es2020::es2020(
compat::es2020::Config {
nullish_coalescing: compat::es2020::nullish_coalescing::Config {
no_document_all: assumptions.no_document_all
no_document_all: assumptions.no_document_all,
},
optional_chaining: compat::es2020::optional_chaining::Config {
no_document_all: assumptions.no_document_all,
pure_getter: assumptions.pure_getters
}
pure_getter: assumptions.pure_getters,
},
},
self.unresolved_mark
self.unresolved_mark,
),
should_enable(self.target, EsVersion::Es2020)
should_enable(self.target, EsVersion::Es2020),
),
Optional::new(
compat::es2019::es2019(),
should_enable(self.target, EsVersion::Es2019)
should_enable(self.target, EsVersion::Es2019),
),
Optional::new(
compat::es2018(compat::es2018::Config {
object_rest_spread: compat::es2018::object_rest_spread::Config {
no_symbol: assumptions.object_rest_no_symbols,
set_property: assumptions.set_spread_properties,
pure_getters: assumptions.pure_getters
}
pure_getters: assumptions.pure_getters,
},
}),
should_enable(self.target, EsVersion::Es2018)
should_enable(self.target, EsVersion::Es2018),
),
Optional::new(
compat::es2017(
compat::es2017::Config {
async_to_generator: compat::es2017::async_to_generator::Config {
ignore_function_name: assumptions.ignore_function_name,
ignore_function_length: assumptions.ignore_function_length
ignore_function_length: assumptions.ignore_function_length,
},
},
self.unresolved_mark
self.unresolved_mark,
),
should_enable(self.target, EsVersion::Es2017)
should_enable(self.target, EsVersion::Es2017),
),
Optional::new(
compat::es2016(),
should_enable(self.target, EsVersion::Es2016)
should_enable(self.target, EsVersion::Es2016),
),
Optional::new(
compat::es2015(
Expand All @@ -286,36 +282,36 @@ impl<'a, 'b, P: swc_ecma_visit::Fold> PassBuilder<'a, 'b, P> {
no_class_calls: assumptions.no_class_calls,
set_class_methods: assumptions.set_class_methods,
super_is_callable_constructor: assumptions
.super_is_callable_constructor
.super_is_callable_constructor,
},
computed_props: compat::es2015::computed_props::Config {
loose: self.loose
loose: self.loose,
},
for_of: compat::es2015::for_of::Config {
assume_array: false,
loose: self.loose
loose: self.loose,
},
spread: compat::es2015::spread::Config { loose: self.loose },
destructuring: compat::es2015::destructuring::Config {
loose: self.loose
loose: self.loose,
},
regenerator: self.regenerator,
template_literal: compat::es2015::template_literal::Config {
ignore_to_primitive: assumptions.ignore_to_primitive_hint,
mutable_template: assumptions.mutable_template_object
mutable_template: assumptions.mutable_template_object,
},
parameters: compat::es2015::parameters::Config {
ignore_function_length: assumptions.ignore_function_length,
},
typescript: syntax.typescript()
}
typescript: syntax.typescript(),
},
),
should_enable(self.target, EsVersion::Es2015)
should_enable(self.target, EsVersion::Es2015),
),
Optional::new(
compat::es3(true),
cfg!(feature = "es3") && self.target == EsVersion::Es3
)
cfg!(feature = "es3") && self.target == EsVersion::Es3,
),
))
};

Expand All @@ -325,21 +321,21 @@ impl<'a, 'b, P: swc_ecma_visit::Fold> PassBuilder<'a, 'b, P> {
.map(|v| v.mangle.is_obj() || v.mangle.is_true())
.unwrap_or(false);

chain!(
(
self.pass,
Optional::new(
paren_remover(comments.map(|v| v as &dyn Comments)),
self.fixer
self.fixer,
),
compat_pass,
// module / helper
Optional::new(
modules::import_analysis::import_analyzer(import_interop, ignore_dynamic),
need_analyzer
need_analyzer,
),
Optional::new(
helpers::inject_helpers(self.unresolved_mark),
self.inject_helpers
self.inject_helpers,
),
ModuleConfig::build(
self.cm.clone(),
Expand All @@ -349,7 +345,7 @@ impl<'a, 'b, P: swc_ecma_visit::Fold> PassBuilder<'a, 'b, P> {
feature_flag,
resolver,
),
as_folder(MinifierPass {
visit_mut_pass(MinifierPass {
options: self.minify,
cm: self.cm.clone(),
comments,
Expand All @@ -360,7 +356,7 @@ impl<'a, 'b, P: swc_ecma_visit::Fold> PassBuilder<'a, 'b, P> {
top_level_mark: self.top_level_mark,
..self.hygiene.clone().unwrap_or_default()
}),
self.hygiene.is_some() && !is_mangler_enabled
self.hygiene.is_some() && !is_mangler_enabled,
),
Optional::new(fixer(comments.map(|v| v as &dyn Comments)), self.fixer),
)
Expand Down
Loading
Loading