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 akuvfx
Recipients akuvfx
Date 2021-10-20.18:25:33
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1634754333.52.0.650287816276.issue45542@roundup.psfhosted.org>
In-reply-to
Content
For example:

    def f(x):
        return 1 < x < 3

will be slower than:

    def f(x):
        return 1 < x and x < 3

The first function will generate following bytecode:

              0 LOAD_CONST               1 (1)
              2 LOAD_FAST                0 (x)
              4 DUP_TOP
              6 ROT_THREE
              8 COMPARE_OP               0 (<)
             10 JUMP_IF_FALSE_OR_POP    18
             12 LOAD_CONST               2 (3)
             14 COMPARE_OP               0 (<)
             16 RETURN_VALUE
        >>   18 ROT_TWO
             20 POP_TOP
             22 RETURN_VALUE

Performs unnecessary stack operations: duplicates x, rotates 3 items for no reason, then re-rotates 2 items and pops a value. This is fine if the value in the middle was more complex and needed to be duplicated rather than recalculated, which would be definitely slower, but for simpler values like names or constants, it's actually bad. The bytecode for the first function should look the same as second one which is:

              0 LOAD_CONST               1 (1)
              2 LOAD_FAST                0 (x)
              4 COMPARE_OP               0 (<)
              6 JUMP_IF_TRUE_OR_POP     14
              8 LOAD_FAST                0 (x)
             10 LOAD_CONST               2 (3)
             12 COMPARE_OP               0 (<)
        >>   14 RETURN_VALUE
History
Date User Action Args
2021-10-20 18:25:33akuvfxsetrecipients: + akuvfx
2021-10-20 18:25:33akuvfxsetmessageid: <1634754333.52.0.650287816276.issue45542@roundup.psfhosted.org>
2021-10-20 18:25:33akuvfxlinkissue45542 messages
2021-10-20 18:25:33akuvfxcreate