Skip to content

Simplify boolean expressions in Java AST#10279

Open
zbynek wants to merge 2 commits intogwtproject:mainfrom
zbynek:dce-bool
Open

Simplify boolean expressions in Java AST#10279
zbynek wants to merge 2 commits intogwtproject:mainfrom
zbynek:dce-bool

Conversation

@zbynek
Copy link
Collaborator

@zbynek zbynek commented Feb 20, 2026

Split from #10261 (see comments in that PR for discussion).

Fixes #10278

Comment on lines +432 to +435
} else {
// if side effect, allow rewriting a() && false && b() -> (a(), false) && b()
// -> (a(), false && b()) -> (a(), false)
return lhs.hasSideEffects() ? new JMultiExpression(info, lhs, rhs) : rhs;
Copy link
Member

Choose a reason for hiding this comment

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

to be clear, this is really just a() && false to (a(), false) right? Then on the way out we would handle the next stage (and test stuff address that so we know this change composes well with existing &&, ?: optimizations in DCE

For my part, I think it would read better if we had another else/if in here rather than a ternary within an else

Suggested change
} else {
// if side effect, allow rewriting a() && false && b() -> (a(), false) && b()
// -> (a(), false && b()) -> (a(), false)
return lhs.hasSideEffects() ? new JMultiExpression(info, lhs, rhs) : rhs;
} else if (!lhs.hasSideEffects()) {
return rhs;
} else {
// if side effect, allow rewriting a() && false -> (a(), false)
// which will enable other optimizations to eval them separately
return new JMultiExpression(info, lhs, rhs);

+ "static boolean randomBooleanWithSideEffects() { createA(); return random() > .5;}"
+ "static boolean booleanWithoutSideEffects() { return true;}"
+ "static int arithmeticWithSideEffects() { createA(); return 4;}"
+ "static native double random() /*-{ }-*/;"
Copy link
Member

Choose a reason for hiding this comment

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

Discussed in the other PR, but this can be simpler if you split to another test, or turn off runMethodInliner before your specific tests. All methods are assumed to have side effects by default in GWT, but once they are inlined away they might no longer have side effects. You can also cheat your way out of this not with JSNI, but just a @DoNotInline. It makes the tests easier to read/write if you don't have to mess with this stuff

optimizeExpressions(false, "boolean", "false && A.booleanWithSideEffects()")
.intoString("return false;");

optimizeExpressions(false, "int", "A.randomBooleanWithSideEffects() && false && A.randomBooleanWithSideEffects() ? 1 : 2")
Copy link
Member

Choose a reason for hiding this comment

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

Add a test for just plain && and || that fails before this patch - the more complex test is great to have as well, but we should test the simpler case.

@zbynek
Copy link
Collaborator Author

zbynek commented Feb 23, 2026

Stats for GeoGebra main chunk

before:
pretty:21671810
obfuscated:7864283
gzipped:2554348

after:
pretty:21670169
obfuscated:7863399
gzipped:2553679

So this seems to be just .03% improvement.

@niloc132
Copy link
Member

So this seems to be just .03% improvement.

Right, most of these are going to be tiny on their own, or will depend on the specific code being compiled repeating this pattern.

I still think changes like this in pattern matching simple code is worth it - we're adding a single if in an edge case, and DCE is specifically designed to converge in a single pass (though other optimizations can interact with it and require another later pass). The theory is that if we find other code later that can be rewritten trivially to false/true, we will get to propagate that out and clean up other code - and now that will even work if the boolean is trapped in a multiexpr.

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.

Improve simplification of boolean expressions in Java AST

2 participants