python-minifier icon indicating copy to clipboard operation
python-minifier copied to clipboard

const-folding

Open mqyhlkahu opened this issue 1 year ago • 1 comments

A possible improvement would be const-folding expressions that would be shorter as a constant value.

Example:

SOME_VARIABLE = (((123 * 456) + 789) // 10) & 0xabcdef

can be transformed into

SOME_VARIABLE=1063

Note that the Python interpreter will do this at compile-time anyway, so it won't have any effect on the code that is actually run.

>>> import dis
>>> dis.dis("(((123 * 456) + 789) // 10) & 0xabcdef")
  0           0 RESUME                   0

  1           2 LOAD_CONST               0 (1063)
              4 RETURN_VALUE

mqyhlkahu avatar Aug 05 '23 18:08 mqyhlkahu

Thanks for creating an issue @kotnen, keep any eye out for this in the next release 😄

dflook avatar Sep 02 '23 17:09 dflook