Author vstinner
Recipients ArthurGoldberg, Sergey.Kirpichev, THRlWiTi, Trip.Volpe, ajaksu2, alex, amaury.forgeotdarc, barry, belopolsky, brett.cannon, diana, eric.araujo, eric.snow, ethan.furman, flox, nedbat, pgimeno, rhettinger, serhiy.storchaka, terry.reedy, tshepang, vstinner, yselivanov
Date 2019-05-28.09:35:52
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1559036153.17.0.58711265835.issue2506@roundup.psfhosted.org>
In-reply-to
Content
My PR 13600 works as expected.

I simplified attached continue.py: see attached traceme.py.

Output with optimizations
---
$ ./python traceme.py  
  6           0 LOAD_CONST               1 (0)
              2 STORE_FAST               0 (a)

  7           4 LOAD_GLOBAL              0 (range)
              6 LOAD_CONST               2 (3)
              8 LOAD_CONST               3 (4)
             10 CALL_FUNCTION            2
             12 GET_ITER
        >>   14 FOR_ITER                30 (to 46)
             16 STORE_FAST               1 (n)

  8          18 LOAD_FAST                1 (n)
             20 LOAD_CONST               4 (2)
             22 BINARY_MODULO
             24 POP_JUMP_IF_FALSE       14

  9          26 LOAD_FAST                1 (n)
             28 LOAD_CONST               2 (3)
             30 BINARY_MODULO
             32 POP_JUMP_IF_FALSE       14

 10          34 LOAD_FAST                0 (a)
             36 LOAD_CONST               5 (1)
             38 INPLACE_ADD
             40 STORE_FAST               0 (a)

 11          42 JUMP_ABSOLUTE           14
             44 JUMP_ABSOLUTE           14
        >>   46 LOAD_CONST               0 (None)
             48 RETURN_VALUE
 --- modulename: traceme, funcname: func
traceme.py(6):     a = 0
traceme.py(7):     for n in range(3, 4):
traceme.py(8):         if n % 2:
traceme.py(9):             if n % 3:
traceme.py(7):     for n in range(3, 4):
---

Output without optimizations (-X noopt):
---
$ ./python -X noopt traceme.py  
  6           0 LOAD_CONST               1 (0)
              2 STORE_FAST               0 (a)

  7           4 LOAD_GLOBAL              0 (range)
              6 LOAD_CONST               2 (3)
              8 LOAD_CONST               3 (4)
             10 CALL_FUNCTION            2
             12 GET_ITER
        >>   14 FOR_ITER                30 (to 46)
             16 STORE_FAST               1 (n)

  8          18 LOAD_FAST                1 (n)
             20 LOAD_CONST               4 (2)
             22 BINARY_MODULO
             24 POP_JUMP_IF_FALSE       44

  9          26 LOAD_FAST                1 (n)
             28 LOAD_CONST               2 (3)
             30 BINARY_MODULO
             32 POP_JUMP_IF_FALSE       42

 10          34 LOAD_FAST                0 (a)
             36 LOAD_CONST               5 (1)
             38 INPLACE_ADD
             40 STORE_FAST               0 (a)

 11     >>   42 JUMP_ABSOLUTE           14
        >>   44 JUMP_ABSOLUTE           14
        >>   46 LOAD_CONST               0 (None)
             48 RETURN_VALUE
 --- modulename: traceme, funcname: func
traceme.py(6):     a = 0
traceme.py(7):     for n in range(3, 4):
traceme.py(8):         if n % 2:
traceme.py(9):             if n % 3:
traceme.py(11):             continue
traceme.py(7):     for n in range(3, 4):
---


The difference on the trace is that using -X noopt, "traceme.py(11):             continue" line is traced as expected.

The difference on the bytecode is that jumps are no longer optimized using -X noopt:

* Optimized:

  "32 POP_JUMP_IF_FALSE       14"

* Not optimized:

  "32 POP_JUMP_IF_FALSE       42"
  ">>   42 JUMP_ABSOLUTE           14"

The peephole optimizer replaces a jump to an unconditional jump with a jump directly to the target of the unconditional jump.

I documented peephole optimizations in my reimplementation in pure Python:
https://bytecode.readthedocs.io/en/latest/peephole.html#optimizations
(I'm not sure that it's up to date, but it should give you an idea of which kinds of optimizations are implemented.)
History
Date User Action Args
2019-05-28 09:35:53vstinnersetrecipients: + vstinner, barry, brett.cannon, rhettinger, terry.reedy, amaury.forgeotdarc, belopolsky, ajaksu2, nedbat, eric.araujo, alex, flox, THRlWiTi, ethan.furman, tshepang, eric.snow, serhiy.storchaka, yselivanov, diana, Trip.Volpe, pgimeno, Sergey.Kirpichev, ArthurGoldberg
2019-05-28 09:35:53vstinnersetmessageid: <1559036153.17.0.58711265835.issue2506@roundup.psfhosted.org>
2019-05-28 09:35:53vstinnerlinkissue2506 messages
2019-05-28 09:35:52vstinnercreate