closure-compiler
closure-compiler copied to clipboard
Keep long bigint literals in hexadecimal format
Long bigint literals written in hexadecimal format (such as 0x123456789ABDEFn
) should be kept as is. Currently GCC expands them to decimal bigint literals, which increases the compiled size significantly (up to ~20%).
Example:
const P = 0x2fadbe2852044d028597455bc2abbd1bc873af205dfabb8a304600f3e09eeba8n;
console.log(P);
is transformed to
console.log(21565680844461314807147611702860246336805372493508489110556896454939225549736n)
Many crypto libraries (hashing, verifiable computation, etc) include long list of large constants. Keeping such constants in hexadecimal form makes a big difference.
Writing the literals as BigInt("0xabc")
is a workaround but is not ideal due to increased compiled size and runtime cost.
This incurs up to 20% blow up in compiled code size.
Repro - https://closure-compiler.appspot.com/home#code%3D%252F%252F%2520%253D%253DClosureCompiler%253D%253D%250A%252F%252F%2520%2540compilation_level%2520ADVANCED_OPTIMIZATIONS%250A%252F%252F%2520%2540output_file_name%2520default.js%250A%252F%252F%2520%253D%253D%252FClosureCompiler%253D%253D%250A%250A%252F%252F%2520ADD%2520YOUR%2520CODE%2520HERE%250Aconst%2520P%2520%253D%25200x2fadbe2852044d028597455bc2abbd1bc873af205dfabb8a304600f3e09eeba8n%253B%250Aconsole.log(P)%253B%250A%250A
Actually, in the advanced more, both number
s and bigint
s can be emitted in hexadecimal if it makes the output shorter (literals larger than a certain threshold which can be calculated)
If true
is transformed to !0, this optimization can also be considered, which may be equally effective.
This is a fair optimization request. We may not be able to prioritize this but we'd be happy to receive a PR of this change.
Could you point me to the file where literals are emitted?
I think you'd modify the code approximately here.
https://github.com/google/closure-compiler/blob/15c5a2dd3e55c2bc4ed4e1eb63e537c90f0bce7c/src/com/google/javascript/jscomp/CodeGenerator.java#L421
@brad4d Thank you. Where would the tests for this go?
(Responding since Bradford is out) ~~tests should go in https://github.com/google/closure-compiler/blob/master/test/com/google/javascript/jscomp/parsing/ParserTest.java.~~ (correction - https://github.com/google/closure-compiler/blob/master/test/com/google/javascript/jscomp/CodePrinterTest.java) Thanks for the PR!
(Correction to my previous comment - I should have said https://github.com/google/closure-compiler/blob/master/test/com/google/javascript/jscomp/CodePrinterTest.java, sorry)
Thank you. Added some tests. Let me know if there other testing surfaces