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.

Author brandtbucher
Recipients Mark.Shannon, barry, brandtbucher
Date 2022-01-26.02:11:23
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1643163083.28.0.715395020987.issue46528@roundup.psfhosted.org>
In-reply-to
Content
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.
History
Date User Action Args
2022-01-26 02:11:23brandtbuchersetrecipients: + brandtbucher, barry, Mark.Shannon
2022-01-26 02:11:23brandtbuchersetmessageid: <1643163083.28.0.715395020987.issue46528@roundup.psfhosted.org>
2022-01-26 02:11:23brandtbucherlinkissue46528 messages
2022-01-26 02:11:23brandtbuchercreate