Skip to content
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

tweak do-while loops #1452

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -1749,8 +1749,14 @@ merge(Compressor.prototype, {
extract_declarations_from_unreachable_code(compressor, self.body, a);
return make_node(AST_BlockStatement, self, { body: a });
}
} else {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add comment: // self instanceof AST_Do

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done.

// self instanceof AST_Do
return self.body;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@kzc this is the line which I believe is causing #1532 instead of the section below.

Copy link
Contributor

@kzc kzc Mar 2, 2017

Choose a reason for hiding this comment

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

return self; would certainly fix the do-while case in #1532. I've already confirmed this.

Copy link
Contributor

Choose a reason for hiding this comment

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

But I am not certain about the handling of breaks and continues in while(){} loops with this PR. There's insufficient test case coverage.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'll get rid of this and add new test cases to address #1532. Then we can revisit this optimisation at a later date.

}
}
if (self instanceof AST_While) {
return make_node(AST_For, self, self).transform(compressor);
}
return self;
});

Expand Down Expand Up @@ -1799,16 +1805,6 @@ merge(Compressor.prototype, {
}
};
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the equivalent logic for if_break_in_loop(self, compressor); in the new OPT(AST_DWLoop) code above? I see a newly added else clause for the self instanceof AST_Do case, but the AST_While logic is the same as it was. Was if_break_in_loop never needed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

AST_For does if_break_in_loop() already, so once AST_While is turned into AST_for and .transform(compressor), it should be covered.

It is technically not calling if_break_in_loop() in the same order (or even the same number of times) as before, but I had a scan through the method and thinks this is safe to do if not a minor performance improvement.

Copy link
Contributor

Choose a reason for hiding this comment

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

I see. Thanks.


OPT(AST_While, function(self, compressor) {
if (!compressor.option("loops")) return self;
self = AST_DWLoop.prototype.optimize.call(self, compressor);
if (self instanceof AST_While) {
if_break_in_loop(self, compressor);
self = make_node(AST_For, self, self).transform(compressor);
}
return self;
});

OPT(AST_For, function(self, compressor){
var cond = self.condition;
if (cond) {
Expand Down
29 changes: 29 additions & 0 deletions test/compress/loops.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,32 @@ keep_collapse_const_in_own_block_scope_2: {
console.log(c);
}
}

evaluate: {
options = {
loops: true,
dead_code: true,
evaluate: true,
};
input: {
while (true) {
a();
}
while (false) {
b();
}
do {
c();
} while (true);
do {
d();
} while (false);
}
expect: {
for(;;)
a();
for(;;)
c();
d();
}
}