python / cpython

The Python programming language
https://www.python.org
Other
62.59k stars 30.04k forks source link

`METH_METHOD` calling convention is now not so efficient #123500

Open neonene opened 4 weeks ago

neonene commented 4 weeks ago

Bug report

Bug description:

There are callables implemented with the METH_METHOD|METH_FASTCALL signature in C. They can be 5%-15% less efficient than using only METH_FASTCALL (or METH_O) with a PyType_GetModuleByDef function call.

For example, I measured the difference on Windows PGO builds by duplicating functions:

Observations:

CPython versions tested on:

CPython main branch

Operating systems tested on:

Windows

neonene commented 4 weeks ago

I cannot confirm this issue on 3.10, so I tried profiling a function:

                                                 entry
Function Name                                    count
_sre_SRE_Pattern_sub                           1000000
cfunction_vectorcall_FASTCALL_KEYWORDS_METHOD  1000000  # 3.10
cfunction_vectorcall_FASTCALL_KEYWORDS_METHOD  1000003  # 3.14
                                                 entry
Function Name                                    count
_sre_SRE_Pattern_sub_patched                   1000000
cfunction_vectorcall_FASTCALL_KEYWORDS         1001231  # 3.10
cfunction_vectorcall_FASTCALL_KEYWORDS             918  # 3.14