dflook/python-minifier

const-folding

Opened this issue · 1 comments

kotnen commented

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

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