google / closure-compiler

A JavaScript checker and optimizer.
https://developers.google.com/closure/compiler/
Apache License 2.0
7.41k stars 1.15k forks source link

Keep long bigint literals in hexadecimal format #4165

Open KimlikDAO-bot opened 6 months ago

KimlikDAO-bot commented 6 months ago

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.

rishipal commented 6 months ago

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

KimlikDAO-bot commented 6 months ago

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.

rishipal commented 6 months ago

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.

KimlikDAO-bot commented 6 months ago

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

brad4d commented 6 months ago

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

KimlikDAO-bot commented 6 months ago

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

lauraharker commented 6 months ago

(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 commented 6 months ago

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

KimlikDAO-bot commented 6 months ago

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