Repository navigation
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add a
vectorcallmember toPyWrapperDescrObjectso 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 throughtp_call, asMETH_VARARGSfunctions do: with vectorcall,PyObject_Call()with**kwargswould unpack the dict into an array only for the wrapper to build a new dict. Removing thePyWrapperFlag_KEYWORDScheck inPyDescr_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.
c['x'] = 1int.__add__(1, 2)object.__setattr__(o, 'x', 1)frozen_triple(1, 2, 3)dict.__init__({}, **kw)The
dict.__init__is ~2ns which is within noise for my machine.dict_subclass(),list_subclass()andpair(1, 2)were not significantly different.dict_subclass(),list_subclass()anddict.__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 (theselfreference and type check);perf statpinned to one core shows no cycle difference for the subclass rows beyond run-to-run variance.Linux x86-64, clang PGO+LTO builds, pyperf: