Skip to content

Improve duplicate clinit removal - #10383

Draft
niloc132 wants to merge 14 commits into
gwtproject:mainfrom
niloc132:9731-dup-clinits-commas-test
Draft

Improve duplicate clinit removal#10383
niloc132 wants to merge 14 commits into
gwtproject:mainfrom
niloc132:9731-dup-clinits-commas-test

Conversation

@niloc132

Copy link
Copy Markdown
Member

Explore comma expression contents when finding class initializers, and mark them as seen to avoid their inclusion later in method bodies. Also improved try block handling when there are no catches or finallys, and left notes for how this can be later improved when control flow is more structured (#10248, #10381)

Moved this pass into the main JS optimization loop - inlining methods tends to produce grouped clinits, and clinit removal can leave behind dead code that needs to be simplified away. In some cases, clinit removal make also make a method eligible for inlining.

This code previously had no tests, so added a test class, and improved the JS OptimizerTestBase to allow for minor changes (newlines, etc) between input and expected contents, as well as asserting for "no change" when appropriate. This more closely mirrors the Java version of this test base.

Also cleans up some "XO" references ("eXactly Once") in favor of "clinit" as referenced elsewhere.

Fixes #9731

@niloc132 niloc132 added this to the 2.14 milestone Jul 22, 2026
@niloc132

Copy link
Copy Markdown
Member Author

Marked as draft for now, some additional tests are required, and some text blocks can be used in place of separate string vararg params. Showcase improves by about 0.1%, or about 300 bytes over 300-400kb permutations.

List<JsCatch> catches = x.getCatches();
for (int i = 0; i < catches.size(); i++) {
JsCatch aCatch = catches.get(i);
JsCatch c = accept(aCatch);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

this should almost certainly be a branch(), we can't guarantee a catch runs.

public void test2() throws Exception {
// Don't coalesces cases 0 and 2 since 2 can be reached via fallthrough from 1
String input = "function a(x){var y;switch(x){case 0:return 17;case 1:y=18;case 2:return 17;case 3:y=18;}return y}";
String expected = "function a(x){var y;switch(x){case 0:return 17;case 1:y=18;case 2:return 17;case 3:y=18;}return y}\n";

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

In this PR or another, these would all make good text blocks.

* Several tests use an expression like "x + (clinit_A(), y) > 0" to avoid short-circuiting but
* still allow for side effects so the clinit won't be moved out before the for condition.
*/
public class JsDuplicateClinitRemoverTest extends OptimizerTestBase {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Possibly rename the DuplicateClinitRemover to have a Js prefix like this does? that seems to be the convention for other passes.

"alert(cond1 && (clinit_A(), cond2));"
)
.into(CLINIT_DECL,
"clinit_A();",

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Text blocks all through here for sure.

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.

dead code not eliminated when two Boolean.parseBoolean in same function

1 participant