This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: missed peephole optimization (unnecessary jump at end of function after yield)
Type: performance Stage: resolved
Components: Interpreter Core Versions: Python 3.4
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: Neal.Norwitz, ezio.melotti, serhiy.storchaka, vstinner
Priority: normal Keywords:

Created on 2013-04-01 04:45 by Neal.Norwitz, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (3)
msg185708 - (view) Author: Neal Norwitz (Neal.Norwitz) Date: 2013-04-01 04:45
>>> def foo():
...   if x:
...     yield None
... 
>>> dis.dis(foo)
  2           0 LOAD_GLOBAL              0 (x)
              3 POP_JUMP_IF_FALSE       14

  3           6 LOAD_CONST               0 (None)
              9 YIELD_VALUE         
             10 POP_TOP             
             11 JUMP_FORWARD             0 (to 14)
        >>   14 LOAD_CONST               0 (None)
             17 RETURN_VALUE        


The JUMP_FORWARD at 11 is not necessary and is not in place with a return in the code:

>>> def foo():
...  if x:
...   return None
... 
>>> dis.dis(foo)
  2           0 LOAD_GLOBAL              0 (x)
              3 POP_JUMP_IF_FALSE       10

  3           6 LOAD_CONST               0 (None)
              9 RETURN_VALUE        
        >>   10 LOAD_CONST               0 (None)
             13 RETURN_VALUE
msg185859 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2013-04-02 21:06
This issue is similar to #17430.
msg309519 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-01-05 19:57
This optimization already is implemented in 3.5+. Actually it is implemented as a part of code generation, not a peepholer.
History
Date User Action Args
2022-04-11 14:57:43adminsetgithub: 61807
2018-01-05 19:57:12serhiy.storchakasetstatus: open -> closed

nosy: + serhiy.storchaka
messages: + msg309519

resolution: out of date
stage: resolved
2013-04-10 14:38:12ezio.melottisetnosy: + ezio.melotti

versions: + Python 3.4, - Python 2.7
2013-04-02 21:06:41vstinnersetnosy: + vstinner
messages: + msg185859
2013-04-01 04:45:51Neal.Norwitzcreate