Brandt Bucher <brandtbuc...@gmail.com> added the comment:

In practice, pretty much the only thing that will do more work now is augmented 
subscription:

>>> a[b] += c

main:

  1           2 LOAD_NAME                0 (a)
              4 LOAD_NAME                1 (b)
              6 DUP_TOP_TWO
              8 BINARY_SUBSCR
             10 LOAD_NAME                2 (c)
             12 BINARY_OP               13 (+=)
             14 ROT_THREE
             16 STORE_SUBSCR

patched:

  1           2 LOAD_NAME                0 (a)
              4 LOAD_NAME                1 (b)
              6 COPY                     2
              8 COPY                     2
             10 BINARY_SUBSCR
             12 LOAD_NAME                2 (c)
             14 BINARY_OP               13 (+=)
             16 SWAP                     3
             18 SWAP                     2
             20 STORE_SUBSCR

Pattern matching is the only place where we use ROT_N, and frankly it's 
*misused* there... often, it makes the cost of storing names quadratic at 
runtime. Even though we emit *more* instructions now, the peephole optimizer 
actually cuts them down significantly, and the result is more efficient than 
before. For example:

>>> match x:
...     case (a, b, c, None):
...         pass

main:

  1           2 LOAD_NAME                0 (x)

  2           4 MATCH_SEQUENCE
              6 POP_JUMP_IF_FALSE       20 (to 40)
              8 GET_LEN
             10 LOAD_CONST               0 (4)
             12 COMPARE_OP               2 (==)
             14 POP_JUMP_IF_FALSE       20 (to 40)
             16 UNPACK_SEQUENCE          4
             18 ROT_FOUR
             20 ROT_FOUR
             22 ROT_FOUR
             24 POP_JUMP_IF_NOT_NONE    18 (to 36)
             26 STORE_NAME               1 (a)
             28 STORE_NAME               2 (b)
             30 STORE_NAME               3 (c)

  3          32 LOAD_CONST               1 (None)
             34 RETURN_VALUE

  2     >>   36 POP_TOP
             38 POP_TOP
        >>   40 POP_TOP
             42 LOAD_CONST               1 (None)
             44 RETURN_VALUE

patched:

  1           2 LOAD_NAME                0 (x)

  2           4 MATCH_SEQUENCE
              6 POP_JUMP_IF_FALSE       20 (to 40)
              8 GET_LEN
             10 LOAD_CONST               0 (4)
             12 COMPARE_OP               2 (==)
             14 POP_JUMP_IF_FALSE       20 (to 40)
             16 UNPACK_SEQUENCE          4
             18 SWAP                     2
             20 SWAP                     3
             22 SWAP                     4
             24 POP_JUMP_IF_NOT_NONE    18 (to 36)
             26 STORE_NAME               1 (a)
             28 STORE_NAME               2 (b)
             30 STORE_NAME               3 (c)

  3          32 LOAD_CONST               1 (None)
             34 RETURN_VALUE

  2     >>   36 POP_TOP
             38 POP_TOP
        >>   40 POP_TOP
             42 LOAD_CONST               1 (None)
             44 RETURN_VALUE

Replacing the ROT_FOURs with SWAPs may seem minor, but it ends up being a *lot* 
less work at runtime.

----------

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

Reply via email to