Following system colour scheme Selected dark colour scheme Selected light colour scheme

Python Enhancement Proposals

PEP 590 – Vectorcall: a fast calling protocol for CPython

Author:
Mark Shannon <mark at hotpy.org>, Jeroen Demeyer <J.Demeyer at UGent.be>
BDFL-Delegate:
Petr Viktorin <encukou at gmail.com>
Status:
Accepted
Type:
Standards Track
Created:
29-Mar-2019
Python-Version:
3.8
Post-History:


Table of Contents

Important

This PEP is a historical document. The up-to-date, canonical documentation can now be found atThe Vectorcall Protocol.

×

SeePEP 1for how to propose changes.

Abstract

This PEP introduces a new C API to optimize calls of objects. It introduces a new “vectorcall” protocol and calling convention. This is based on the “fastcall” convention, which is already used internally by CPython. The new features can be used by any user-defined extension class.

Most of the new API is private in CPython 3.8. The plan is to finalize semantics and make it public in Python 3.9.

NOTE:This PEP deals only with the Python/C API, it does not affect the Python language or standard library.

Motivation

The choice of a calling convention impacts the performance and flexibility of code on either side of the call. Often there is tension between performance and flexibility.

The currenttp_call[2]calling convention is sufficiently flexible to cover all cases, but its performance is poor. The poor performance is largely a result of having to create intermediate tuples, and possibly intermediate dicts, during the call. This is mitigated in CPython by including special-case code to speed up calls to Python and builtin functions. Unfortunately, this means that other callables such as classes and third party extension objects are called using the slower, more generaltp_callcalling convention.

This PEP proposes that the calling convention used internally for Python and builtin functions is generalized and published so that all calls can benefit from better performance. The new proposed calling convention is not fully general, but covers the large majority of calls. It is designed to remove the overhead of temporary object creation and multiple indirections.

Another source of inefficiency in thetp_callconvention is that it has one function pointer per class, rather than per object. This is inefficient for calls to classes as several intermediate objects need to be created. For a classcls,at least one intermediate object is created for each call in the sequence type.__call__,cls.__new__,cls.__init__.

This PEP proposes an interface for use by extension modules. Such interfaces cannot effectively be tested, or designed, without having the consumers in the loop. For that reason, we provide private (underscore-prefixed) names. The API may change (based on consumer feedback) in Python 3.9, where we expect it to be finalized, and the underscores removed.

Specification

The function pointer type

Calls are made through a function pointer taking the following parameters:

  • PyObject*callable:The called object
  • PyObject*const*args:A vector of arguments
  • size_tnargs:The number of arguments plus the optional flagPY_VECTORCALL_ARGUMENTS_OFFSET(see below)
  • PyObject*kwnames:EitherNULLor a tuple with the names of the keyword arguments

This is implemented by the function pointer type: typedefPyObject*(*vectorcallfunc)(PyObject*callable,PyObject*const*args,size_tnargs,PyObject*kwnames);

Changes to thePyTypeObjectstruct

The unused slotprintfunctp_printis replaced withtp_vectorcall_offset.It has the typePy_ssize_t. A newtp_flagsflag is added,_Py_TPFLAGS_HAVE_VECTORCALL, which must be set for any class that uses the vectorcall protocol.

If_Py_TPFLAGS_HAVE_VECTORCALLis set, thentp_vectorcall_offsetmust be a positive integer. It is the offset into the object of the vectorcall function pointer of typevectorcallfunc. This pointer may beNULL,in which case the behavior is the same as if_Py_TPFLAGS_HAVE_VECTORCALLwas not set.

Thetp_printslot is reused as thetp_vectorcall_offsetslot to make it easier for external projects to backport the vectorcall protocol to earlier Python versions. In particular, the Cython project has shown interest in doing that (seehttps://mail.python.org/pipermail/python-dev/2018-June/153927.html).

Descriptor behavior

One additional type flag is specified:Py_TPFLAGS_METHOD_DESCRIPTOR.

Py_TPFLAGS_METHOD_DESCRIPTORshould be set if the callable uses the descriptor protocol to create a bound method-like object. This is used by the interpreter to avoid creating temporary objects when calling methods (see_PyObject_GetMethodand theLOAD_METHOD/CALL_METHODopcodes).

Concretely, ifPy_TPFLAGS_METHOD_DESCRIPTORis set fortype(func),then:

  • func.__get__(obj,cls)(*args,**kwds)(withobjnot None) must be equivalent tofunc(obj,*args,**kwds).
  • func.__get__(None,cls)(*args,**kwds)must be equivalent tofunc(*args,**kwds).

There are no restrictions on the objectfunc.__get__(obj,cls). The latter is not required to implement the vectorcall protocol.

The call

The call takes the form((vectorcallfunc)(((char*)o)+offset))(o,args,n,kwnames)where offsetisPy_TYPE(o)->tp_vectorcall_offset. The caller is responsible for creating thekwnamestuple and ensuring that there are no duplicates in it.

nis the number of positional arguments plus possibly thePY_VECTORCALL_ARGUMENTS_OFFSETflag.

PY_VECTORCALL_ARGUMENTS_OFFSET

The flagPY_VECTORCALL_ARGUMENTS_OFFSETshould be added ton if the callee is allowed to temporarily changeargs[-1]. In other words, this can be used ifargspoints to argument 1 in the allocated vector. The callee must restore the value ofargs[-1]before returning.

Whenever they can do so cheaply (without allocation), callers are encouraged to usePY_VECTORCALL_ARGUMENTS_OFFSET. Doing so will allow callables such as bound methods to make their onward calls cheaply. The bytecode interpreter already allocates space on the stack for the callable, so it can use this trick at no additional cost.

See[3]for an example of howPY_VECTORCALL_ARGUMENTS_OFFSETis used by a callee to avoid allocation.

For getting the actual number of arguments from the parametern, the macroPyVectorcall_NARGS(n)must be used. This allows for future changes or extensions.

New C API and changes to CPython

The following functions or macros are added to the C API:

  • PyObject*_PyObject_Vectorcall(PyObject*obj,PyObject*const*args,size_tnargs,PyObject*keywords): Callsobjwith the given arguments. Note thatnargsmay include the flagPY_VECTORCALL_ARGUMENTS_OFFSET. The actual number of positional arguments is given byPyVectorcall_NARGS(nargs). The argumentkeywordsis a tuple of keyword names orNULL. An empty tuple has the same effect as passingNULL. This uses either the vectorcall protocol ortp_callinternally; if neither is supported, an exception is raised.
  • PyObject*PyVectorcall_Call(PyObject*obj,PyObject*tuple,PyObject*dict): Call the object (which must support vectorcall) with the old *argsand**kwargscalling convention. This is mostly meant to put in thetp_callslot.
  • Py_ssize_tPyVectorcall_NARGS(size_tnargs):Given a vectorcallnargsargument, return the actual number of arguments. Currently equivalent tonargs&~PY_VECTORCALL_ARGUMENTS_OFFSET.

Subclassing

Extension types inherit the type flag_Py_TPFLAGS_HAVE_VECTORCALL and the valuetp_vectorcall_offsetfrom the base class, provided that they implementtp_callthe same way as the base class. Additionally, the flagPy_TPFLAGS_METHOD_DESCRIPTOR is inherited iftp_descr_getis implemented the same way as the base class.

Heap types never inherit the vectorcall protocol because that would not be safe (heap types can be changed dynamically). This restriction may be lifted in the future, but that would require special-casing__call__intype.__setattribute__.

Finalizing the API

The underscore in the names_PyObject_Vectorcalland _Py_TPFLAGS_HAVE_VECTORCALLindicates that this API may change in minor Python versions. When finalized (which is planned for Python 3.9), they will be renamed to PyObject_VectorcallandPy_TPFLAGS_HAVE_VECTORCALL. The old underscore-prefixed names will remain available as aliases.

The new API will be documented as normal, but will warn of the above.

Semantics for the other names introduced in this PEP (PyVectorcall_NARGS, PyVectorcall_Call,Py_TPFLAGS_METHOD_DESCRIPTOR, PY_VECTORCALL_ARGUMENTS_OFFSET) are final.

Internal CPython changes

Changes to existing classes

Thefunction,builtin_function_or_method,method_descriptor,method,wrapper_descriptor,method-wrapper classes will use the vectorcall protocol (not all of these will be changed in the initial implementation).

Forbuiltin_function_or_methodandmethod_descriptor (which use thePyMethodDefdata structure), one could implement a specific vectorcall wrapper for every existing calling convention. Whether or not it is worth doing that remains to be seen.

Using the vectorcall protocol for classes

For a classcls,creating a new instance usingcls(xxx) requires multiple calls. At least one intermediate object is created for each call in the sequence type.__call__,cls.__new__,cls.__init__. So it makes a lot of sense to use vectorcall for calling classes. This really means implementing the vectorcall protocol fortype. Some of the most commonly used classes will use this protocol, probablyrange,list,str,andtype.

ThePyMethodDefprotocol and Argument Clinic

Argument Clinic[4]automatically generates wrapper functions around lower-level callables, providing safe unboxing of primitive types and other safety checks. Argument Clinic could be extended to generate wrapper objects conforming to the newvectorcallprotocol. This will allow execution to flow from the caller to the Argument Clinic generated wrapper and thence to the hand-written code with only a single indirection.

Third-party extension classes using vectorcall

To enable call performance on a par with Python functions and built-in functions, third-party callables should include avectorcallfuncfunction pointer, settp_vectorcall_offsetto the correct value and add the_Py_TPFLAGS_HAVE_VECTORCALLflag. Any class that does this must implement thetp_callfunction and make sure its behaviour is consistent with thevectorcallfuncfunction. Settingtp_calltoPyVectorcall_Callis sufficient.

Performance implications of these changes

This PEP should not have much impact on the performance of existing code (neither in the positive nor the negative sense). It is mainly meant to allow efficient new code to be written, not to make existing code faster.

Nevertheless, this PEP optimizes forMETH_FASTCALLfunctions. Performance of functions usingMETH_VARARGSwill become slightly worse.

Stable ABI

Nothing from this PEP is added to the stable ABI (PEP 384).

Alternative Suggestions

bpo-29259

PEP 590is close to what was proposed in bpo-29259[1]. The main difference is that this PEP stores the function pointer in the instance rather than in the class. This makes more sense for implementing functions in C, where every instance corresponds to a different C function. It also allows optimizingtype.__call__,which is not possible with bpo-29259.

PEP 576 and PEP 580

BothPEP 576andPEP 580are designed to enable 3rd party objects to be both expressive and performant (on a par with CPython objects). The purpose of this PEP is provide a uniform way to call objects in the CPython ecosystem that is both expressive and as performant as possible.

This PEP is broader in scope thanPEP 576and uses variable rather than fixed offset function-pointers. The underlying calling convention is similar. BecausePEP 576only allows a fixed offset for the function pointer, it would not allow the improvements to any objects with constraints on their layout.

PEP 580proposes a major change to thePyMethodDefprotocol used to define builtin functions. This PEP provides a more general and simpler mechanism in the form of a new calling convention. This PEP also extends thePyMethodDefprotocol, but merely to formalise existing conventions.

Other rejected approaches

A longer, 6 argument, form combining both the vector and optional tuple and dictionary arguments was considered. However, it was found that the code to convert between it and the oldtp_callform was overly cumbersome and inefficient. Also, since only 4 arguments are passed in registers on x64 Windows, the two extra arguments would have non-negligible costs.

Removing any special cases and making all calls use thetp_callform was also considered. However, unless a much more efficient way was found to create and destroy tuples, and to a lesser extent dictionaries, then it would be too slow.

Acknowledgements

Victor Stinner for developing the original “fastcall” calling convention internally to CPython. This PEP codifies and extends his work.

References

Reference implementation

A minimal implementation can be found athttps://github.com/markshannon/cpython/tree/vectorcall-minimal


Source:https://github.com/python/peps/blob/main/peps/pep-0590.rst

Last modified:2024-06-01 20:09:32 GMT