[Python-Dev] Possible optimization for LOAD_FAST ? (original) (raw)
Cesare Di Mauro cesare.di.mauro at gmail.com
Fri Dec 31 08:02:20 CET 2010
- Previous message: [Python-Dev] Possible optimization for LOAD_FAST ?
- Next message: [Python-Dev] [Python-checkins] r87537 - in python/branches/py3k: Doc/library/struct.rst Doc/whatsnew/3.2.rst Lib/test/test_struct.py Lib/wave.py Misc/NEWS Modules/_struct.c
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
2010/12/28 Lukas Lueg <lukas.lueg at googlemail.com>
Consider the following code:
def foobar(x): for i in range(5): x[i] = i The bytecode in python 2.7 is the following: 2 0 SETUPLOOP 30 (to 33) 3 LOADGLOBAL 0 (range) 6 LOADCONST 1 (5) 9 CALLFUNCTION 1 12 GETITER >> 13 FORITER 16 (to 32) 16 STOREFAST 1 (i) 3 19 LOADFAST 1 (i) 22 LOADFAST 0 (x) 25 LOADFAST 1 (i) 28 STORESUBSCR 29 JUMPABSOLUTE 13 >> 32 POPBLOCK >> 33 LOADCONST 0 (None) 36 RETURNVALUE Can't we optimize the LOADFAST in lines 19 and 25 to a single load and put the reference twice on the stack? There is no way that the reference of i might change in between the two lines. Also, the loadfast in lne 22 to reference x could be taken out of the loop as x will always point to the same object....
Yes, you can, but you need:
- a better AST evaluator (to mark symbols/variables with proper attributes);
- a better optimizer (usually located on compile.c) which has a "global vision" (not limited to single instructions and/or single expressions).
It's not that simple, and the results aren't guaranteed to be good.
Also, consider that Python, as a dynamic-and-not-statically-compiled language need to find a good trade-off between compilation time and execution.
Just to be clear, a C program is usually compiled once, then executed, so you can spend even hours to better optimize the final binary code.
With a dynamic language, usually the code is compiled and the executed as needed, in "realtime". So it isn't practical neither desirable having to wait too much time before execution begins (the "startup" problem).
Python stays in a "gray area", because modules are usually compiled once (when they are first used), and executed many times, but it isn't the only case.
You cannot assume that optimization techniques used on other (static) languages can be used/ported in Python.
Cesare -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20101231/cf8f3296/attachment.html>
- Previous message: [Python-Dev] Possible optimization for LOAD_FAST ?
- Next message: [Python-Dev] [Python-checkins] r87537 - in python/branches/py3k: Doc/library/struct.rst Doc/whatsnew/3.2.rst Lib/test/test_struct.py Lib/wave.py Misc/NEWS Modules/_struct.c
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]