Repository navigation
Description
Feature or enhancement
Python wrapper_descriptor objects often wrap simple operations that are fast such as setting an attribute on an object. Calling them, however, currently constructs two tuples which often takes longer than the operation.
This is particularly visible with frozen dataclasses. The decorator generates an __init__ that calls object.__setattr__(self, name, value) once per field.
from dataclasses import dataclass
@dataclass(frozen=True)
class frozen_triple:
a: int
b: int
c: int
frozen_triple(1, 2, 3)On main on my machine the frozen_triple instance takes ~156ns to construct. With my prototype which avoids the tuples it takes ~110ns (1.42x faster). This work generalizes gh-156250 which focused on a single slot.
I propose removing the unnecessary tuples across all wrapper_descriptor objects by using what the interpreter already knows about exact ones with no new public API. Wrappers that take keywords, __init__, and __call__, keep one tuple.
-
In
_PyObject_MakeTpCallspecial casePyWrapperDescr_Typeand call its slot wrapper directly. Change the slot wrapper core implementation to be vectorcall shaped and add a smalltp_callwrapper around it for compatibility.This avoids building an args tuple containing
selfthat is then sliced.prototype: ba2f645
-
Expand the
slotdefsarray privatepytype_slotdefentries to contain two parallel members. The existingwrapperfunc, which takes a tuple, and a new variant which takes vectorcall args. If the vectorcall version is non-NULL, call it. This allows for gradual migration of slots. Custom slot wrapper descriptors, such as Sage and Cython use, keep working as before.This makes the second tuple construction optional. Until descriptor wrappers move it is still constructed when necessary.
-
Migrate slot definitions to provide both members. The argument tuple used to keep
selfand the arguments alive during the call. In the prototype I move that responsibility to the shared call as it is relatively low overhead. Having each wrapper hold only what it needs is a possible refinement.
Step 2 & 3 prototype: cmaloney@193f25c328a
Performance
| Statement | main | step 1 only | all three steps |
|---|---|---|---|
frozen_triple(1, 2, 3) |
156 ns | 132 ns (1.19x faster) | 110 ns (1.42x faster) |
| frozen dataclass, 8 fields | 356 ns | 285 ns (1.25x faster) | 222 ns (1.60x faster) |
object.__setattr__(o, 'x', 1) |
39.6 ns | 30.4 ns (1.30x faster) | 25.0 ns (1.59x faster) |
int.__add__(1, 2) |
27.1 ns | 21.5 ns (1.26x faster) | 17.6 ns (1.55x faster) |
c['x'] = 1 (collections.Counter) |
36.4 ns | 29.4 ns (1.24x faster) | 23.2 ns (1.57x faster) |
Linux x86-64, clang PGO+LTO build, pyperf.
Alternatives Considered
- Add a member to
PyWrapperDescrObjectwhich implementsvectorcall. Adds a pointer to everyPyWrapperDescrObjectobject. This works out fairly similarly performance wise but with more tradeoffs. It can be added later if needed for external project integration.
Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Links to previous discussion of this feature:
- PEP 590 lists
wrapper_descriptorandmethod-wrapperamong the classes that "will use the vectorcall protocol (not all of these will be changed in the initial implementation)". - Optimize wrapper descriptors using FASTCALL #75724 and [WIP] bpo-31543: Optimize wrapper descriptors #3685 (2017): "Optimize wrapper descriptors using FASTCALL". Closed by its author: "these specific descriptors are rare, so the added complexity is not worth it".
- Add tp_fastcall to PyTypeObject: support FASTCALL calling convention for all callable objects #73445 (2017):
tp_fastcall, which included a fast wrapper instruct wrapperbase. Closed. - Certain methods that heap allocated subtypes inherit suffer a 50-80% performance penalty #78577 and bpo-34396: vectorcall for wrapper_descriptor and method-wrapper #14863 (2019): inherited methods are slow in heap subclasses; the draft PR "vectorcall for wrapper_descriptor and method-wrapper" changed the
wrapperfuncsignature and was closed unreviewed for lack of time. The issue is still open. - Implement PEP 590 #81155 (bpo-36974: PEP 590 #13185 and bpo-36974: separate vectorcall functions for each calling convention #13781): vectorcall for
method_descriptor, the closest precedent. - Crash due to borrowed references in _PyStack_UnpackDict() #81088: a crash from borrowed references during argument handling, the bug class the reference holding guards against.
- Improve performance of inherited mapping assignment slot calls #156250 and gh-156250: Speed up inherited mapping assignment slot calls #156258: the same cost for a single slot
Counter.__setitem__.
Activity