Skip to content

Minify boolean and numeric literals in obfuscated mode#10291

Open
zbynek wants to merge 4 commits intogwtproject:mainfrom
zbynek:minify-literals
Open

Minify boolean and numeric literals in obfuscated mode#10291
zbynek wants to merge 4 commits intogwtproject:mainfrom
zbynek:minify-literals

Conversation

@zbynek
Copy link
Copy Markdown
Collaborator

@zbynek zbynek commented Mar 4, 2026

Fixes #10281

Also cleans up some minor code style issues in the modified files.

Does NOT implement

  • Infinity -> 1/0
  • undefined -> void 0
  • 1.2E100 -> 12E99

@zbynek zbynek force-pushed the minify-literals branch 5 times, most recently from a569b03 to eb7f316 Compare March 4, 2026 23:53
@zbynek zbynek marked this pull request as ready for review March 4, 2026 23:54
@zbynek zbynek force-pushed the minify-literals branch from eb7f316 to 1330ec3 Compare March 5, 2026 19:49
@zbynek zbynek force-pushed the minify-literals branch from 1330ec3 to f50f053 Compare March 5, 2026 21:37
@zbynek zbynek added Category-Compiler ready This PR has been reviewed by a maintainer and is ready for a CI run. labels Mar 5, 2026
Comment thread dev/core/src/com/google/gwt/dev/js/JsToStringGenerationVisitor.java Outdated
Comment on lines +836 to +840
if (spaceReturn(expr)) {
_space();
} else {
_spaceOpt();
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't have a great solution for this, but I just wanted to call out briefly that this seems gross - we have to toString the child node twice, so that we know how to concat them together. Perf-wise (though probably not logic-wise) it would seem better to just go ahead and not accept(expr) if we know it is a literal, but append directly.

Better still would be to signal to the TextOutput "hey you already know if space is optional or not... I just finished writing a keyword, make sure the next token gets a space if it needs it". Then, when the next token is written, TextOutput decides if it needs a space?

One missing case in the current impl is to do with parens - in the terser sample you showed me, there are some cases of extra parens being required in order to maintain operator precedence:

return(t=h.h>>19)!=(u=n.h>>19)?...

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.

Perf-wise (though probably not logic-wise) it would seem better to just go ahead and not accept(expr) if we know it is a literal

That's implemented in my last commit.

One missing case in the current impl is to do with parens

I don't have a good solution for that -- would it be OK to leave that for another time?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah, I think we wait, rethink that as we implement other size enhancements.

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.

Uh, so we need to accept the literal node so that subclasses can handle it (for billing). For now I reverted to the duplicated serialization -- we could introduce a flag that lets us accept the node without printing, but it's not obvious if that can save time.

Comment thread dev/core/src/com/google/gwt/dev/js/JsToStringGenerationVisitor.java Outdated
Comment thread dev/core/src/com/google/gwt/dev/util/AbstractTextOutput.java
Comment thread dev/core/test/com/google/gwt/dev/js/JsToStringGenerationVisitorTest.java Outdated
Comment thread dev/core/src/com/google/gwt/dev/jjs/JavaToJavaScriptCompiler.java Outdated
@zbynek zbynek requested a review from niloc132 April 1, 2026 21:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Category-Compiler ready This PR has been reviewed by a maintainer and is ready for a CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Obfuscate boolean literals in output

3 participants