[Python-Dev] constant folding of -0

Eugene Toder eltoder at gmail.com
Thu Mar 10 03:17:34 CET 2011


> Indeed, see http://bugs.python.org/issue11244

Yes, I've noticed that too. However, if I'm not missing something, your patches
do not address folding of -0.

Btw, there's an alternative approach to allow "recursive" constant folding.
Instead of keeping a stack of last constants, you can keep a pointer to the
start of the last (LOAD_CONSTs + NOPs) run and the number of LOAD_CONSTs in
that run (called lastlc in the current version). When you want Nth constant
from the end, you start from that pointer and skip lastlc-N constants. You
also make a function to get next constant from that point. This approach has
worse time complexity for searching in a long run of LOAD_CONSTs, however,
there are upsides:
- very long runs of constants are rare in real code
- it uses less memory and doesn't have arbitrary limits on the size of the run
- it's book-keeping overhead is smaller, so when you don't have long runs of
constants (common case, I believe), it should be faster
- I think it's simpler to implement
(There's also an optimization -- if
(current_position - start_of_run) / 3 == lastlc
there are no NOPs in the run and you can get constants by simple indexing).

Eugene




More information about the Python-Dev mailing list