[Python-ideas] Disable all peephole optimizations
Steven D'Aprano
steve at pearwood.info
Thu May 22 19:59:10 CEST 2014
On Thu, May 22, 2014 at 03:41:31PM +0000, Steve Dower wrote:
> Why not move the existing optimisation into -O mode and put future
> optimisations in there too? It may just start having enough value that
> people switch to using it.
I just had the same idea, you beat me to it.
There's a steady but small stream of people asking "why do we have -O,
it does so little we might as well get rid of it". If I remember
correctly (and apologies if I do not), Guido has even suggested getting
rid of simple constant folding. So let's make -O more attractive, while
simplifying the default behaviour:
* By default, no optimizations operate at all.
* With -O, you get assert disabling, the tricky string concatenation
optimization, constant folding, and whatever else the peepholer does.
* The double -OO switch should be deprecated, for eventual removal
in the very distant future. (4.0? 5.0?)
* Instead, a separate switch for removing docstrings can be added,
to support implementations in low-memory devices or other
constrained situations.
This will make Python's compilation model a little more familiar to
people coming from other languages. It will make -O more attractive,
instead of being viewed by some as a waste of effort, and ensure that by
default there are no tricks played with byte-code.
A big advantage: we already have separate .pyo and .pyc files, so no
risk of confusion.
Downside of this suggestion:
- To the extent that constant folding and other optimizations actually
lead to a speed-up, turning them off by default will be a performance
regression.
- Experienced programmers ought to know not to rely on the string
concatenation optimization, as it is non-portable and prone to
surprising failures even in CPython. The optimization really only
exists for naive programmers, but they are unlikely to know about, or
bother using, -O to get that optimization.
- Surely I don't expect PyPy to perform no optimizations at all unless
the -O switch is given? I'd have to be mad to suggest that.
--
Steven
More information about the Python-ideas
mailing list