optapy / optapy

OptaPy is an AI constraint solver for Python to optimize planning and scheduling problems.

Home Page:https://www.optapy.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

jpyinterpreter - make FOR_ITER use Java iterator loop format when possible

Christopher-Chianelli opened this issue · comments

Currently, in order to fully support all the forms a Python iterator can take, jpyinterpreter generates the following code for FOR_ITER:

try {
    do {
        TOS' = next(TOS)
         // code in for block
    } while(true);
} catch (StopIteration e) {
    // code after for block
}

This is highly atypical in Java, and the JVM probably would have a harder time optimizing its standard for iterator loop:

while (TOS.hasNext()) {
    TOS' = TOS.next();
    // code in for block
}
 // code after for block

We can look at TOS to see if it a known iterator type (i.e. PythonIterator), and if so, generate the more typical Java loop.