Skip to content

introduce reduce_vars#1301

Closed
alexlamsl wants to merge 7 commits intomishoo:masterfrom
alexlamsl:reduce_vars
Closed

introduce reduce_vars#1301
alexlamsl wants to merge 7 commits intomishoo:masterfrom
alexlamsl:reduce_vars

Conversation

@alexlamsl
Copy link
Copy Markdown
Collaborator

With a variable which is only assigned a constant value at initialisation and never gets modified elsewhere, this new flag allows evaluate to treat occurrence of said variable as constant when performing optimisation.

Motivated by minification using global_defs for places such as this.

@kzc
Copy link
Copy Markdown
Contributor

kzc commented Sep 20, 2016

PR looks good. Didn't see any obvious issues.

I agree reduce_vars should be false by default, at least for several months of testing in the wild after release.

Could you add some negative tests exercising all the cases where sym.modified = true preventing the optimization?

@kzc
Copy link
Copy Markdown
Contributor

kzc commented Sep 20, 2016

Could you please document the new option as well?

@alexlamsl
Copy link
Copy Markdown
Collaborator Author

@kzc thanks for the quick review, will get onto more tests and documentation now.

BTW, I find this case of collapse_vars rather surprising:

var A = 1;
(function() {
  console.log(A - 2);
})();

becomes:

(function() {
  console.log(-1);
})();

Evaluation to -1 is okay, but removal of the top-level var A seems odd as unused flag would have left that untouched.

@kzc
Copy link
Copy Markdown
Contributor

kzc commented Sep 20, 2016

It is as intended. unused is not consulted by a few optimizations.

Google Closure transforms that example into console.log(-1); in advanced mode.

@alexlamsl
Copy link
Copy Markdown
Collaborator Author

@kzc I see - thanks for the clarification. I have left out any global/top-level variables with reduce_vars for now as I am concerned with their values being modified externally, but we can always remove that !d.global at a later date if we want to I guess.

Added tests and documentation, PTAL.

@kzc
Copy link
Copy Markdown
Contributor

kzc commented Sep 20, 2016

 +            if (parent instanceof AST_Unary && (parent.operator === '++' || parent.operator === '--')
 +             || parent instanceof AST_Assign && parent.left === node) {
 +                sym.modified = true;
              }

I see there's a ++ postfix modification test. Could you please add a -- prefix test case?

I appreciate that operators like += and are instances of AST_Assign for which you already have = tests, but could you add one such case of a complex assign op in the modified test?

@kzc
Copy link
Copy Markdown
Contributor

kzc commented Sep 20, 2016

I have left out any global/top-level variables with reduce_vars for now as I am concerned with their values being modified externally

I wouldn't be concerned with that. If any such variables were modified externally then the reduce_vars optimizations would be invalid anyway.

@alexlamsl
Copy link
Copy Markdown
Collaborator Author

Added tests for -- prefix, *= compound assignment, and for global/top-level variables. reduce_vars now applies to top-level variables as well.

console.log(a + b);
console.log(b + c);
// TODO: as "modified" is determined in "figure_out_scope",
// even "passes" would improve this any further
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo?
s/would/wouldn't/

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops.

@kzc
Copy link
Copy Markdown
Contributor

kzc commented Sep 20, 2016

LGTM

Looks like it'd be a useful feature.

@avdg
Copy link
Copy Markdown
Contributor

avdg commented Sep 21, 2016

Looking gook but I reviewed the pr from my smartphone and couldn't test it throughoutly.

@alexlamsl
Copy link
Copy Markdown
Collaborator Author

alexlamsl commented Sep 25, 2016

Just FYI, found one case where reduce_vars would cause suboptimal minification, i.e. .$injectorProvider is longer than the original ['$injector'+v]

@kzc
Copy link
Copy Markdown
Contributor

kzc commented Sep 25, 2016

I wouldn't worry about it. That's a variation of the issue observed here: #1194

I have a fix but it needs refinement.

@rvanvelzen
Copy link
Copy Markdown
Collaborator

Nice, thanks! Merged as 4761d07

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants