swc-project / swc

Rust-based platform for the Web
https://swc.rs
Apache License 2.0
31.16k stars 1.23k forks source link

Merge expression simplifier and dead branch remover into the pure optimizer #4392

Open kdy1 opened 2 years ago

kdy1 commented 2 years ago
image

Currently, those passes only use the main thread, but those passes are mostly stateless and we need to move logic to minifier anyway, because of side effect check.

I profiled it, and those two passes used 7.5% of main thread execution time

cc @Austaras

kdy1 commented 2 years ago

I think this can be done by exposing shallow optimizers from swc_ecma_transforms_optimization, but with a comment that says it's not considered as a public API.

@Austaras How do you think about this?

Austaras commented 2 years ago

What shallow optimizer?

kdy1 commented 2 years ago

Code except visit_mut_children_with(self), e.g.

https://github.com/swc-project/swc/blob/63efd0089b1249b4f728c0c3a16fb2a1e97747be/crates/swc_ecma_transforms_optimization/src/simplify/expr/mod.rs#L1165-L1274

Austaras commented 2 years ago

That seems to be good enough for expr_simplier

Austaras commented 2 years ago

What would be the potential performance impact of check is global var? It seems like need ctxt.outer() which need to acquire global lock

kdy1 commented 2 years ago

We can pre-compute SyntaxContext and compare it with Ident.span.ctxt

Austaras commented 2 years ago

Come to revisit this, I believe proper solution for expr_simplifier would be separate it into two parts -- AST mangle and evaluate. Former part should be run only once before all pass, and later one should be merged with pure. I don't know if that would be a break change for the JS side.

kdy1 commented 2 years ago

I think it's good way to go and I don't think it's a breaking change for js side