Repository navigation
Commit cffc305
authored
Add `@vectorcall` to Argument Clinic to generate `method.__new__`. Rename `method_new` as `method_type_new` to avoid the AC generated verctorcall name conflicting with existing `method_vectorcall`. Didn't add `Py_TPFLAGS_IMMUTABLETYPE` as this flag [automatically](https://docs.python.org/3/c-api/typeobj.html#c.Py_TPFLAGS_IMMUTABLETYPE) applies to static types.
Performance before:
```
Constructor microbenchmark (best of 7, 1000000 iterations each)
MethodType(f, obj) 0.1073 s 107.3 ns/op
types.MethodType(f, obj) 0.1142 s 114.2 ns/op
MethodType(C.m, obj) 0.1224 s 122.4 ns/op
call bound method m() 0.0944 s 94.4 ns/op
```
Performance after:
```
Constructor microbenchmark (best of 7, 1000000 iterations each)
MethodType(f, obj) 0.0805 s 80.5 ns/op (24.97% faster)
types.MethodType(f, obj) 0.0862 s 86.2 ns/op (24.51% faster)
MethodType(C.m, obj) 0.0927 s 92.7 ns/op (24.26% faster)
call bound method m() 0.0946 s 94.6 ns/op (-0.21% faster)
```
```python
import timeit
SETUP = """
import sys
import types
from types import MethodType
def f(self):
pass
class C:
def m(self):
pass
obj = C()
m = MethodType(f, obj)
"""
CASES = [
("MethodType(f, obj)", "MethodType(f, obj)"),
("types.MethodType(f, obj)", "types.MethodType(f, obj)"),
("MethodType(C.m, obj)", "MethodType(C.m, obj)"),
("call bound method m()", "m()"),
]
NUMBER = 1_000_000
REPEAT = 7
print("Constructor microbenchmark (best of %d, %d iterations each)" % (REPEAT, NUMBER))
for name, stmt in CASES:
times = timeit.repeat(stmt=stmt, setup=SETUP, number=NUMBER, repeat=REPEAT)
best = min(times)
print("%-34s %8.4f s %7.1f ns/op" % (name, best, best / NUMBER * 1e9))
```
1 parent 1cc5802 commit cffc305
2 files changed
Lines changed: 41 additions & 10 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
182 | 182 | | |
183 | 183 | | |
184 | 184 | | |
| 185 | + | |
185 | 186 | | |
186 | | - | |
| 187 | + | |
187 | 188 | | |
188 | 189 | | |
189 | 190 | | |
| |||
192 | 193 | | |
193 | 194 | | |
194 | 195 | | |
195 | | - | |
196 | | - | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
197 | 199 | | |
198 | 200 | | |
199 | 201 | | |
| |||
323 | 325 | | |
324 | 326 | | |
325 | 327 | | |
326 | | - | |
| 328 | + | |
327 | 329 | | |
328 | 330 | | |
329 | 331 | | |
330 | 332 | | |
331 | 333 | | |
332 | 334 | | |
333 | 335 | | |
334 | | - | |
| 336 | + | |
| 337 | + | |
335 | 338 | | |
336 | 339 | | |
337 | 340 | | |
| |||
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
0 commit comments