Skip to content

Avoid creating argument tuples when calling slot wrappers #162258

Open

Description

@cmaloney

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.

  1. In _PyObject_MakeTpCall special case PyWrapperDescr_Type and call its slot wrapper directly. Change the slot wrapper core implementation to be vectorcall shaped and add a small tp_call wrapper around it for compatibility.

    This avoids building an args tuple containing self that is then sliced.

    prototype: ba2f645

  2. Expand the slotdefs array private pytype_slotdef entries to contain two parallel members. The existing wrapperfunc, 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.

  3. Migrate slot definitions to provide both members. The argument tuple used to keep self and 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.

Prototype: https://github.com/python/cpython/compare/main...cmaloney:cpython:shared/exp-descrwrappper-tuple?diff=split&w

Alternatives Considered

  • Add a member to PyWrapperDescrObject which implements vectorcall. Adds a pointer to every PyWrapperDescrObject object. 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:

Activity

  1. self-assigned this
    on Nov 19, 2026
  2. added
    type-featureA feature request or enhancement
    performancePerformance or resource usage
    interpreter-core(Objects, Python, Grammar, and Parser dirs)
    on Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

interpreter-core(Objects, Python, Grammar, and Parser dirs)performancePerformance or resource usagetype-featureA feature request or enhancement

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions