[New-bugs-announce] [issue39281] The CO_NESTED flag is associated with a significant performance cost

Dan Snider report at bugs.python.org
Thu Jan 9 20:26:34 EST 2020


New submission from Dan Snider <mr.assume.away at gmail.com>:

The title was carefully worded as I have no idea how or why what is happening is happening, only that it has been like this since a least 3.6.0. That version in particular, by the way, is able to execute a call to a python function with 1 argument 25% faster than 3.8.0 but that may be due at least in part by whatever it is that makes it much faster to a call a unary function wrapped by functools.partial by utilizing the subcript operator on an instance of a partial subtype whose __getitem__ has been set to the data descriptor partial.func... Eg:

    class Party(partial): __getitem__ = partial.func
    fast = Party(hash)
    slow = partial(hash)

    # the expression `fast[""]` runs approximately 28% faster than
    # the expression `slow("")`, and partial.func as __getitem__ is
    # confusingly 139% faster than partial.__call__...

That rather large digression aside, here's a demonstration of two functions identical in every way except the CO_NESTED bit and perhaps the names:

if 1:
    def Slow():
        global Slow
        class  Slow:
            global slow
            def slow(self): return self
        return Slow
    if  Slow():
        class Fast:
            global fast
            def fast(self): return self
    import dis
    dis.show_code(slow)
    print()
    dis.show_code(fast)

----------
messages: 359700
nosy: bup
priority: normal
severity: normal
status: open
title: The CO_NESTED flag is associated with a significant performance cost
type: performance
versions: Python 3.8

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue39281>
_______________________________________


More information about the New-bugs-announce mailing list