drop unused: toplevel, assign-only#1450
Conversation
|
How can your optimization be disabled for the Can your optimization be put behind a new option? |
test/compress/drop-unused.js
Outdated
| var a; | ||
| a = 1; | ||
| } | ||
|
|
There was a problem hiding this comment.
Please remove the blank lines between the functions in this test so it's easier to compare the input to the output. Thanks.
|
Perhaps a new option |
|
@kzc any suggestions with a shorter option name? It's a chore adding white spaces to all the other options... 😝 |
|
Feel free to come up with a shorter name. |
|
Those suggested names are not the greatest. What's the longest option name currently? |
|
|
|
I'm stumped to come up with a better, shorter name. |
|
@kzc no worries - I'll think of something after/while updating the code and tests 😉 |
|
What if
|
|
@kzc sounds pretty cool actually, which makes this new feature off by default as well. |
|
We can have the new feature enabled by default with |
|
@kzc yes, I misread your previous comment (again) |
|
People tend use default options. Not many people set
You didn't misread it - I rewrote it. Sorry about that. |
|
What if there was yet another option setting that can remove unused top level vars?
This should be off by default as some people expect this grandfathered behavior in uglified scripts in the wild. |
| function f2(x) { var z = x, a = ++z; return z += a; } | ||
| function f3(x) { var a = (x -= 3); return x + a; } | ||
| function f4(x) { var a = (x -= 3); return x + a; } | ||
| function f5(x) { var w = e1(), v = e2(), c = v = --x; return (w = x) - c; } |
There was a problem hiding this comment.
Can you please set unused: 2 in the collapse_vars tests with different results?
There was a problem hiding this comment.
I copied these original tests to the bottom with unused=2 - guess I should do it the other way round 😅
| var a = 2; | ||
| do { | ||
| fn(a = 7); | ||
| fn(7); |
There was a problem hiding this comment.
Can you please set unused: 2 in the collapse_vars tests with different results?
test/compress/collapse_vars.js
Outdated
| } | ||
| } | ||
|
|
||
| keep_assign: { |
There was a problem hiding this comment.
I see what you've done. Please keep the original test names in the original place. Just change unused to 2 in the original tests that have different results.
There was a problem hiding this comment.
You may add a new test below the original.
There was a problem hiding this comment.
Would you like me to copy the whole section below each of the two original blocks of tests, or just the affected case?
There was a problem hiding this comment.
The latest collapse_vars test changes look good - thanks!
lib/compress.js
Outdated
| if (node instanceof AST_Assign && node.operator == "=" | ||
| && node.left instanceof AST_SymbolRef | ||
| && scope === self | ||
| && assign_as_unused) { |
There was a problem hiding this comment.
Would you mind making assign_as_unused the first condition in the if? It's faster if assign_as_unused is not in effect, and the same speed otherwise.
lib/compress.js
Outdated
| } | ||
| if (node instanceof AST_Assign && node.operator == "=" | ||
| && node.left instanceof AST_SymbolRef | ||
| && assign_as_unused) { |
There was a problem hiding this comment.
Would you mind making assign_as_unused the first condition in the if? It's faster if assign_as_unused is not in effect, and the same speed otherwise.
| } | ||
|
|
||
| collapse_vars_regexp: { | ||
| options = { |
| } | ||
| } | ||
|
|
||
| collapse_vars_regexp: { |
There was a problem hiding this comment.
Why was this regex test dropped?
There was a problem hiding this comment.
This test was added originally as result of a bug fix. Just because the implementation with respect to is_const changed does not mean the test should be dropped.
There was a problem hiding this comment.
it's cool. i was just confused.
There was a problem hiding this comment.
I was trying to remove my own tests at the end of the file, and forgot I've done so already 🙄
Tests restored.
|
There's a comment above that might have been lost in the shuffle: #1450 (comment) Would welcome your thoughts on the best option to remove unused top level vars. |
6cc2615 to
95ccf22
Compare
|
@kzc yeah I am still chewing on that thought - the code change seems trivial (famous last words...) |
|
Recycling PR state to re-run timed-out CI tests. |
|
Dropping unused top-level vars may warrant its own option. |
|
@kzc I've given it a go with string-based option value. Worse case, we can just break both of these features back out as separate flags. I was hoping the code is the more challenging part of this exercise... |
|
So I guess the only outstanding things are |
|
@kzc ah so it's just a CLI limitation then. On that note I did have an urge to rewrite that in I've just finished updating the docs - will update code now and push in a moment. |
|
Ah, I see now - this is how one enters a string argument into yargs from the CLI: |
|
So it's all a big PTAL at the latest changes. |
|
The CLI and the various options work perfectly. Uglify users will definitely benefit from this new functionality. Great work! |
- assign statement does not count towards variable usage by default - only works with assignments on the same scope level as declaration - can be disabled with `unused` set to "keep_assign" - `toplevel` to drop unused top-level variables and/or functions - `top_retain` to whitelist top-level exceptions
|
@kzc thanks for all the help, as always 👍 |
d08a00d to
cebff96
Compare
- assign statement does not count towards variable usage by default - only works with assignments on the same scope level as declaration - can be disabled with `unused` set to "keep_assign" - `toplevel` to drop unused top-level variables and/or functions - `top_retain` to whitelist top-level exceptions closes mishoo#1450
When mishoo#1450 optimises `a=b=42`, it stops after the first variable even if both are unused. fixes mishoo#1539
similar case as mishoo#1578 but against mishoo#1450 instead closes mishoo#1580
- similar case as mishoo#1578 but against mishoo#1450 instead - fix `this` binding in mishoo#1450 as well closes mishoo#1580
Only works when assignment is on the same scope level as declaration for now (see new tests in
drop-unused.js).Run against
angular.jsand managed to shave one extra variable off without any measureable change in performance.This can obviously be extended in the future, but given how long I spent reading up
drop_unused()let's take one step at a time. Putting it here as I run out of tests to throw at it (for now).