Batuhan Taskaya <isidenti...@gmail.com> added the comment:

There are multiple issues with doing 'static' loop invariant code motion in 
python, but the most obvious one is how would you detect if anything in the 
body is related to the loop or not? The trivial way that the compiled languages 
do this optimization is generating some sort of data dependency graph, and 
seeing whether anything in an expression refers to the loop target though in 
Python you can't know for sure unless you know every possible type in the loop 
body and the type of the loop target which is something nearly impossible to 
infer (due to the dynamic nature of python). You could theoretically do this 
only if the whole loop only uses simple values:

for x in [1, 2, 3, 4, 5]:
    a = 5
    if a > 3:
        a += 2
    continue

and in that case, it is better to just optimize the whole loop away but since 
this kind of code doesn't exist at all in the public, there is no need to make 
the compiler more complicated.

Another option is generating multiple code objects with guards, which would 
require to do first some sort of partial type inference and generate the code 
for the possible types. It would require a substantial amount of changes on 
both the compiler and the interpreter which is something I'd say can be 
deferred for later if we implement a tracing JIT. With traces, it should be way 
easier to do this sort of trivial compiler optimization (loop invariants, 
constant propagation, GVN, etc.) since we would know the types, and possible 
code paths.

----------
nosy: +BTaskaya

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue44505>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to