FYI.
> Peephole optimizer of Python 2.7 (...) only optimizes 2**100, but not 2**(2**100).
This optimizer is dummy, it's only able to replace "LOAD_CONST x; LOAD_CONST y; OPERATION" with the result, whereas the optimizer replaces the bytecode with "NOP; NOP; NOP; NOP; LOAD_CONST z".
So "LOAD_CONST x; LOAD_CONST y; LOAD_CONST z; OPERATION1; OPERATION2" cannot be optimized. But it's enough to optimize 1+2+3 or 1*2*3 for example.
Python 3 peephole optimize does better thanks to a better design:
---
changeset: 68375:14205d0fee45
user: Antoine Pitrou <solipsis@pitrou.net>
date: Fri Mar 11 17:27:02 2011 +0100
files: Lib/test/test_peepholer.py Misc/NEWS Python/peephole.c
description:
Issue #11244: The peephole optimizer is now able to constant-fold
arbitrarily complex expressions. This also fixes a 3.2 regression where
operations involving negative numbers were not constant-folded.
---
It uses a stack for constants, so it's able to optimize more cases. |