closure-compiler icon indicating copy to clipboard operation
closure-compiler copied to clipboard

Keep long bigint literals in hexadecimal format

Open KimlikDAO-bot opened this issue 9 months ago • 9 comments

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.

KimlikDAO-bot avatar May 13 '24 21:05 KimlikDAO-bot

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

rishipal avatar May 14 '24 05:05 rishipal

Actually, in the advanced more, both numbers and bigints 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.

KimlikDAO-bot avatar May 14 '24 06:05 KimlikDAO-bot

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.

rishipal avatar May 15 '24 16:05 rishipal

Could you point me to the file where literals are emitted?

KimlikDAO-bot avatar May 15 '24 18:05 KimlikDAO-bot

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 avatar May 15 '24 18:05 brad4d

@brad4d Thank you. Where would the tests for this go?

KimlikDAO-bot avatar May 25 '24 12:05 KimlikDAO-bot

(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!

lauraharker avatar May 28 '24 16:05 lauraharker

(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)

lauraharker avatar May 28 '24 18:05 lauraharker

Thank you. Added some tests. Let me know if there other testing surfaces

KimlikDAO-bot avatar May 28 '24 22:05 KimlikDAO-bot