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.

classification
Title: The CO_NESTED flag is associated with a significant performance cost
Type: performance Stage:
Components: Versions: Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: bup
Priority: normal Keywords:

Created on 2020-01-10 01:26 by bup, last changed 2022-04-11 14:59 by admin.

Messages (1)
msg359700 - (view) Author: Dan Snider (bup) * Date: 2020-01-10 01:26
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)
History
Date User Action Args
2022-04-11 14:59:25adminsetgithub: 83462
2020-01-10 01:26:34bupcreate