Preventing dict lookups #412
Replies: 4 comments 4 replies
-
|
Beta Was this translation helpful? Give feedback.
-
I think someone tried this before (was it Victor with his FAT Python project?) or at least I've seen this a couple of times on python-ideas or python-dev mailing lists. This isn't allowed at the compiler level due to language semantics. The variable you removed could be modified externally through the frame object. So Python must lookup every time. Also @da-woods is completely correct. In your example the local variable uses the Most of the dictionary lookups in Python are done elsewhere (e.g. looking up |
Beta Was this translation helpful? Give feedback.
-
With Python 3.11 the difference seems a little less, but it's still there (about 6.5% faster):
|
Beta Was this translation helpful? Give feedback.
-
I am guessing the overhead comes from an additional store operation? While it is probably legitimate for the compiler to elide an local variables which are consumed once and only once after being set, it is likely quite a niche optimisation on its own and doubt it occurs that frequently. Getting more aggressive would require the compiler to prove any lines between assignment and first use can not possibly access, modify, or change the computed value of the variable to be elided. With regards to the FAT Python references. My understanding is that FAT Python (if brought to fruition) would have recognized |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
When profiling my python code I noticed a lot of time is spend in
dict
access. Can we somehow let the python intepreter eliminatedict
lookups? For example convert thecollatz
below intocollatz2
automatically?Some timings (python 3.10):
I am aware that there are some issues with my proposal (if there in exception,
ii
will not be available, in the code there might beeval
statements). So perhaps this optimization can be made optional? Or the interpreter smart enough to handle these cases.One more example:
Output:
In this example it would be ok for me if the line
14 STORE_NAME 2 (ii)
is removed from the bytecode.Beta Was this translation helpful? Give feedback.
All reactions