[issue46533] Specialize for staticmethods and classmethods

Mark Shannon report at bugs.python.org
Wed Jan 26 06:11:01 EST 2022


Mark Shannon <mark at hotpy.org> added the comment:

For classmethods, I expect the savings to come from not creating a bound-method and from better specialization of the following call.

classmethod case:

>>> class C:
...     @classmethod
...     def m(*args):
...          pass
... 
>>> C.m
<bound method C.m of <class '__main__.C'>>
>>> C().m
<bound method C.m of <class '__main__.C'>>

So, without specialization LOAD_METHOD "m" has the following stack effect (with `x = C()`):
x -> NULL <bound method>
C -> NULL <bound method>
With specialization:
x -> C <function>
C -> C <function>

For static methods the saving is less as there is no change in stack effect, but we do save the lookup, and we can reuse existing bytecodes.


Neither classmethod or staticmethod should have Py_TPFLAGS_METHOD_DESCRIPTOR set, as they have different semantics.

----------

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


More information about the Python-bugs-list mailing list