You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 0df9822
Browse filesBrowse the repository at this point in the historyBrowse files
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>
0 commit comments