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.

Title: tiny addition to peephole optimizer
Type: Stage:
Components: Interpreter Core Versions: Python 2.6
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: doublep, rhettinger
Priority: normal Keywords: patch

Created on 2007-07-30 23:06 by doublep, last changed 2022-04-11 14:56 by admin. This issue is now closed.

File name Uploaded Description Edit
peephole-optimization.diff doublep, 2007-07-30 23:06 patch against svn repository
Messages (3)
msg52968 - (view) Author: Paul Pogonyshev (doublep) Date: 2007-07-30 23:06
Tiny optimization that replaces Replace STORE_FAST x LOAD_FAST x with DUP_TOP STORE_FAST x (for the same slot x).  Targeted at quite common case:

    foo =
    if foo > 0:

No regression, seems to do what it is supposed to do on several test cases.

However, there is drawback:

    def f(x):
        y = x.y
        if y is not None:
            print y

is disassembled as follows:

  2           0 LOAD_FAST                0 (x)
              3 LOAD_ATTR                0 (y)
              6 DUP_TOP

  3           7 STORE_FAST               1 (y)
             10 LOAD_CONST               0 (None)
             13 COMPARE_OP               9 (is not)
             16 JUMP_IF_FALSE            9 (to 28)
             19 POP_TOP

  4          20 LOAD_FAST                1 (y)
             23 PRINT_ITEM
             24 PRINT_NEWLINE
             25 JUMP_FORWARD             1 (to 29)
        >>   28 POP_TOP
        >>   29 LOAD_CONST               0 (None)
             32 RETURN_VALUE

Note that STORE_FAST "migrated" to another line.  I'm not sure how important that is.
msg52969 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2007-07-31 06:58
I'll look at this one again.  It was tried once and rejected for some reason that I've since forgotten (lack of measurable speed-up, interference with some other optimization, or some such).

msg67564 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2008-05-31 04:52
This was previously rejected for two reasons.  1) It provided almost no 
measureable speed-up (the code for LOAD_FAST and DUP_TOP is 
substantially the same, the only real difference is the time to fetch 
the oparg).  2) The optimization typically crosses lines of source 
code, making it difficult to follow in a trace or disassembly.
Date User Action Args
2022-04-11 14:56:25adminsetgithub: 45258
2008-05-31 04:52:59rhettingersetstatus: open -> closed
resolution: rejected
messages: + msg67564
2007-07-30 23:06:40doublepcreate