Skip to content

gh-162258: Use vectorcall for slot wrappers - #162369

Open
cmaloney wants to merge 1 commit into
python:mainfrom
cmaloney:landable/wrapper-one-tuple
Open

cmaloney wants to merge 1 commit into
python:mainfrom
cmaloney:landable/wrapper-one-tuple

Conversation

@cmaloney

@cmaloney cmaloney commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Add a vectorcall member to PyWrapperDescrObject so slots which don't require a args tuple or kwargs dict can avoid constructing them.

Wrappers which take a keywords dict (__init__ and __call__) keep calling through tp_call, as METH_VARARGS functions do: with vectorcall, PyObject_Call() with **kwargs would unpack the dict into an array only for the wrapper to build a new dict. Removing the PyWrapperFlag_KEYWORDS check in PyDescr_NewWrapper() shows it: dict.__init__({}, **kw) goes from 70.6 ns to 99.0 ns.

The object grows by one pointer (sys.getsizeof(object.__init__): 72 -> 80 bytes) but stays in the same allocator size class: on the default build, sys.getallocatedblocks() and max RSS are unchanged at startup (about 1,000 slot wrappers) and after importing asyncio, json, dataclasses, collections, decimal, typing, re, email and http.client.

Results and error messages are unchanged.

Benchmark main PR
c['x'] = 1 35.5 ns 26.6 ns: 1.33x faster
int.__add__(1, 2) 26.9 ns 21.5 ns: 1.25x faster
object.__setattr__(o, 'x', 1) 37.2 ns 30.8 ns: 1.21x faster
frozen_triple(1, 2, 3) 156 ns 132 ns: 1.18x faster
dict.__init__({}, **kw) 68.9 ns 70.9 ns: 1.03x slower

The dict.__init__ is ~2ns which is within noise for my machine.

dict_subclass(), list_subclass() and pair(1, 2) were not significantly different.

dict_subclass(), list_subclass() and dict.__init__({}, **kw) call __init__, which takes the same path as before. Across builds of the same code they moved by up to 7 ns either way. Under callgrind, dict.__init__({}, **kw) costs about 20 more instructions per call in the call path (the self reference and type check); perf stat pinned to one core shows no cycle difference for the subclass rows beyond run-to-run variance.

Linux x86-64, clang PGO+LTO builds, pyperf:

import pyperf

runner = pyperf.Runner()
setup = """
from collections import Counter
from dataclasses import dataclass

@dataclass(frozen=True)
class frozen_triple:
    a: int
    b: int
    c: int

@dataclass
class pair:
    a: int
    b: int

class C:
    pass

class list_subclass(list):
    def __init__(self):
        super().__init__()

class dict_subclass(dict):
    def __init__(self):
        super().__init__(a=1)

o = C()
c = Counter()
kw = {'x': 1}
"""
for stmt in [
    "c['x'] = 1",
    "object.__setattr__(o, 'x', 1)",
    "int.__add__(1, 2)",
    "frozen_triple(1, 2, 3)",
    "dict_subclass()",
    "list_subclass()",
    "dict.__init__({}, **kw)",
    "pair(1, 2)",
]:
    runner.timeit(stmt, stmt, setup)

Add a `vectorcall` member to `PyWrapperDescrObject` so slots which don't
require a args tuple or kwargs dict can avoid constructing them.

Wrappers which take a keywords dict (`__init__` and `__call__`) keep calling through `tp_call`, as `METH_VARARGS` functions do: with vectorcall, `PyObject_Call()` with `**kwargs` would unpack the dict into an array only for the wrapper to build a new dict. Removing the `PyWrapperFlag_KEYWORDS` check in `PyDescr_NewWrapper()` shows it: `dict.__init__({}, **kw)` goes from 70.6 ns to 99.0 ns.

The object grows by one pointer (`sys.getsizeof(object.__init__)`: 72 -> 80 bytes) but stays in the same allocator size class: on the default build, `sys.getallocatedblocks()` and max RSS are unchanged at startup (about 1,000 slot wrappers) and after importing asyncio, json, dataclasses, collections, decimal, typing, re, email and http.client.

Results and error messages are unchanged.

| Benchmark                       | main    | PR                    |
|---------------------------------|:-------:|:---------------------:|
| `c['x'] = 1`                    | 35.5 ns | 26.6 ns: 1.33x faster |
| `int.__add__(1, 2)`             | 26.9 ns | 21.5 ns: 1.25x faster |
| `object.__setattr__(o, 'x', 1)` | 37.2 ns | 30.8 ns: 1.21x faster |
| `frozen_triple(1, 2, 3)`        | 156 ns  | 132 ns: 1.18x faster  |
| `dict.__init__({}, **kw)`       | 68.9 ns | 70.9 ns: 1.03x slower |

`dict_subclass()`, `list_subclass()` and `pair(1, 2)` were not significantly different.

`dict_subclass()`, `list_subclass()` and `dict.__init__({}, **kw)` call `__init__`, which takes the same path as before. Across builds of the same code they moved by up to 7 ns either way. Under callgrind, `dict.__init__({}, **kw)` costs about 20 more instructions per call in the call path (the `self` reference and type check); `perf stat` pinned to one core shows no cycle difference for the subclass rows beyond run-to-run variance.

Linux x86-64, clang PGO+LTO builds, pyperf:

```python
import pyperf

runner = pyperf.Runner()
setup = """
from collections import Counter
from dataclasses import dataclass

@DataClass(frozen=True)
class frozen_triple:
    a: int
    b: int
    c: int

@DataClass
class pair:
    a: int
    b: int

class C:
    pass

class list_subclass(list):
    def __init__(self):
        super().__init__()

class dict_subclass(dict):
    def __init__(self):
        super().__init__(a=1)

o = C()
c = Counter()
kw = {'x': 1}
"""
for stmt in [
    "c['x'] = 1",
    "object.__setattr__(o, 'x', 1)",
    "int.__add__(1, 2)",
    "frozen_triple(1, 2, 3)",
    "dict_subclass()",
    "list_subclass()",
    "dict.__init__({}, **kw)",
    "pair(1, 2)",
]:
    runner.timeit(stmt, stmt, setup)
```

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@bedevere-app bedevere-app Bot added the type-feature A feature request or enhancement label Nov 21, 2026
@cmaloney cmaloney added the performance Performance or resource usage label Nov 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting core review performance Performance or resource usage skip news type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant