optimise binary operands with evaluate()#1427
Conversation
|
Regarding the changes to the functionality of The AST_Node method If you're changing Not sure that the hoisting of evaluate for Be aware that the tests do not cover all the code paths, so any change to these optimizations could result in potential failures if not careful. You'll be the new authority on compress and will likely have to support it after release. |
|
@kzc there should be no logic changes to (I'll think about the other points in your comment and get back to you in a moment.) |
|
@kzc actually, would you mind sharing the cases where RegExp and The two places |
|
It's just code cleanup regarding RegExp. Moving the regexp check into is_constant is the correct thing to do as it is mutable and there's also instance identity ( |
It appears to be calling evaluate() |
Isn't it already calling |
My bad. You are correct. |
|
@kzc started afresh with a commit that should address the concerns mentioned in your first comment.
I realise that making extensive changes to |
|
I think your latest changes are safer. Sorry for the misinterpretation of past compress behavior. |
|
Apologies in advance for being redundant - calling |
Good to know. |
|
@kzc thanks for the review as always 👍 |
|
No problem. You have as good or better understanding of the code base than I do. |
@alexlamsl The code base could indeed use a clean up which is what you were doing - and that's a good thing from both a long term maintenance and code efficiency point of view. Because Uglify is used by a lot of projects I personally try to err on the side of caution and make fairly conservative changes. But if you're willing to support the code base after making sweeping improvements, please go ahead. I just don't have as much time to commit to this project going forward. |
|
@kzc thanks for the kind words. I also think we should err on the conservative side and make incremental changes. So if/when regressions pop up they would be easier to track down. Please don't hesitate to point out potential areas of improvement as I'm not familiar with the development history of this code base. |
lib/compress.js
Outdated
| compressor.warn("Condition left of && always false [{file}:{line},{col}]", self.start); | ||
| return maintain_this_binding(compressor.parent(), self, ll[0]); | ||
| } | ||
| if (compressor.option("conditionals")) switch (self.operator) { |
There was a problem hiding this comment.
I originally wrote that condition if (compressor.option("conditionals")) and it's incorrect. I later learned that the term "conditional" in a javascript context means "ternary operator" which has nothing to do with short-circuiting conditions. The "booleans" option is not appropriate either because it deals specifically with the true/false -> !0/!1 transform. I suggest to simply remove if (compressor.option("conditionals")) altogether and unconditionally run the body. Might impact some tests though.
There was a problem hiding this comment.
If too many test lines are changed, don't worry about it. "conditionals" is enabled by default and nobody noticed this issue.
There was a problem hiding this comment.
@kzc looks pretty straightforward - I've separated the tests out to a new file (and removed conditionals flag in those tests) for clarity.
There was a problem hiding this comment.
It being governed solely by the compress option "evaluate" makes more sense.
There was a problem hiding this comment.
@kzc sure, though my "no_evaluate" tests need revision then
There was a problem hiding this comment.
@alexlamsl It's your call - leave as you now have it or put it under "evaluate".
There was a problem hiding this comment.
@kzc evaluate as requested - I wouldn't let a few tests stop us from doing The Right Thing 😎
There was a problem hiding this comment.
That's the spirit! Thanks again.
- remove call to evaluate() in is_constant() and let nested optimize() does its job instead - reject RegExp in is_constant() and remove special case logic under collapse_vars - operands to conditionals optimisation are now always evaluate()-ed - throw error in constant_value() instead of returning undefined to catch possible bugs, similar to make_node_from_constant() - optimise binary boolean operators under `evaluate` instead of `conditionals`
|
Looking good once again. |
This one evolves out of #1425 (comment) - adding
[WIP]in title so @kzc and others can throw corner cases at this and see what sticks 😉