bpo-36904: Optimize _PyStack_UnpackDict by jdemeyer · Pull Request #14517 · python/cpython
Optimize, clean up and fix _PyStack_UnpackDict:
_PyStack_UnpackDictis made a static function insidecall.c.- The API is simplified: the new argument vector is returned directly instead of being passed via a pointer.
- Move the case of no keywords outside of
_PyStack_UnpackDict, so_PyStack_UnpackDictis now only used when there actually are keyword arguments. This speeds up the case of no keywords and it simplifies the code. - Allocate the argument vector such that it supports
PY_VECTORCALL_ARGUMENTS_OFFSET. Tests indicate that_PyStack_UnpackDictis responsible for about 60% of cases wheremethod_vectorcallneeds to do an allocation. With this change, these allocations are no longer needed. - Move the freeing code (which is not trivial) to a new function
_PyStack_UnpackDict_Free. - Fix the overflow check by using
Py_ssize_tinstead ofsize_t. In the old code, the overflow check could theoretically overflow (the right hand side could be negative).