-
-
Notifications
You must be signed in to change notification settings - Fork 501
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
feat(mangler): reuse variable names #8562
Conversation
How to use the Graphite Merge QueueAdd either label to this PR to merge it via the merge queue:
You must have a Graphite account in order to use the merge queue. Sign up using this link. An organization admin has enabled the Graphite Merge Queue in this repository. Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue. This stack of pull requests is managed by Graphite. Learn more about stacking. |
dd44926
to
4d6c3ee
Compare
CodSpeed Performance ReportMerging #8562 will degrade performances by 74.8%Comparing Summary
Benchmarks breakdown
|
4d6c3ee
to
1b9026f
Compare
The perf regression looks really bad, but if you take a step back and see the overall minifier timings, it's not that big regression. antd: 196.8ms (6.4ms is 3.3%) |
Just to raise one other concern: The addition of This is quite difficult already, and if The changes to FYI: The bigger picture is: Currently we run semantic analysis multiple times during the pipeline. The hope (well certainly my hope) is that we can eventually make all stages of the pipeline keep semantic data in sync with AST changes. Then we'll be able to remove these extra semantic passes, which will be a large perf improvement. The performance issue needs to be looked at first. But if we decide that it's worth it (very possibly it is), I would suggest the following:
It will of course be achievable with some effort, but it'd be useful to understand how much effort before we go ahead. Maybe it's easier than I think! cc @Dunqing Last thing: I'm not very familiar with the mangler. Could you possibly give a brief explanation of how this algorithm works? I'm wondering if any other way to achieve the effect without the |
This is a significant minification improvement. I've thought about this before, but I have the same concern as @overlookmotel. I am guessing the smaller output is more important than performance here? So I think we can add an extra AST pass to collect identifier references that belong to which scope, this way we can only change in the mangler crate. |
1b9026f
to
510e592
Compare
Yeah, I had that concern while writing this code. I went this way at that time to see how much this change would improve the minification rate.
This PR introduces a concept of "liveness". This is the set of scopes that a given variable / slot is used in. var top_level_a = 0;
var top_level_b = 1;
function foo() {
var foo_a = 1;
console.log(top_level_b, foo_a);
}
function bar() {
var bar_a = 1;
console.log(top_level_b, bar_a);
}
console.log(top_level_a, foo(), bar()) Each liveness of the variables will be:
Since For this example, the slots assigned to the variables will be:
|
510e592
to
faaa1ee
Compare
@sapphi-red To be honest I spent the last hour and still don't comprehend any of this ... probably due to my lack knowledge of JavaScript. May I ask you a favor. Can you update the top level documentation, reference all reading materials, and write a line by line explanation for your newly added code? Just like how I did it with the next one ... so the next person can understand what's going in. I'll do some micro optimizations. And also ... is the debug mode still useful or should it be adjusted? |
Only 5ms for typescript.js |
9c5f846
to
db394c0
Compare
I added a comment that describes the algorithm and added some test cases. I hope that my explanation makes sense, but let me know if there's any places that are unclear. |
## [0.48.1] - 2025-01-26 ### Features - b7f13e6 ast: Implement utf8 to utf16 span converter (#8687) (Boshen) - 6589c3b mangler: Reuse variable names (#8562) (翠 / green) - 29bd215 minifier: Minimize `Infinity.toString(radix)` to `'Infinity'` (#8732) (Boshen) - e0117db minifier: Replace `const` with `let` for non-exported read-only variables (#8733) (sapphi-red) - 9e32f55 minifier: Evaluate `Math.sqrt` and `Math.cbrt` (#8731) (sapphi-red) - 360d49e minifier: Replace `Math.pow` with `**` (#8730) (sapphi-red) - 2e9a560 minifier: `NaN.toString(radix)` is always `NaN` (#8727) (Boshen) - cbe0e82 minifier: Minimize `foo(...[])` -> `foo()` (#8726) (Boshen) - e9fb5fe minifier: Dce pure expressions such as `new Map()` (#8725) (Boshen) ### Bug Fixes - 0944758 codegen: Remove parens from `new (import(''), function() {})` (#8707) (Boshen) - 33de70a mangler: Handle cases where a var is declared in a block scope (#8706) (翠 / green) - d982cdb minifier: `Unknown.fromCharCode` should not be treated as `String.fromCharCode` (#8709) (sapphi-red) - e7ab96c transformer/jsx: Incorrect `isStaticChildren` argument for `Fragment` with multiple children (#8713) (Dunqing) - 3e509e1 transformer/typescript: Enum merging when same name declared in outer scope (#8691) (branchseer) ### Performance - dc0b0f2 manger: Remove useless `tmp_bindings` (#8735) (Dunqing) - e472ced mangler: Optimize handling of collecting lived scope ids (#8724) (Dunqing) - 8587965 minifier: Normalize `undefined` to `void 0` before everything else (#8699) (Boshen) ### Refactor - 58002e2 ecmascript: Remove the lifetime annotation on `MayHaveSideEffects` (#8717) (Boshen) - 10e5920 linter: Move finishing default diagnostic message to `GraphicalReporter` (#8683) (Sysix) - 52a37d0 mangler: Simplify initialization of `slots` (#8734) (Dunqing) - 6bc906c minifier: Allow mutating arguments in methods called from `try_fold_known_string_methods` (#8729) (sapphi-red) - bf8be23 minifier: Use `Ctx` (#8716) (Boshen) - 0af0267 minifier: Side effect detection needs symbols resolution (#8715) (Boshen) - 32e0e47 minifier: Clean up `Normalize` (#8700) (Boshen) - c792068 semantic: Simplify `ScopeTree::iter_bindings` (#8723) (Dunqing) ### Testing - 03229c5 minifier: Fix broken tests (#8722) (Boshen) Co-authored-by: Boshen <1430279+Boshen@users.noreply.github.com>
Changed the mangler to reuse variable names where possible.
This will reduce the code size as shorter variable names can be used in more places. But requires global information and limits parallelism in a single file and requires more memory.