Repository navigation
Commit 6ed9dc2
authored
Replace hand-written `defdict_init` with AC + vectorcall. There is no bad arguments message change.
For argument `default_factory`: `dd->default_factory = Py_NewRef(default_factory)` creates strong reference before `result = PyDict_Type.tp_init(self, newargs, kwargs)` runs arbitrary code. For `args` and `kwargs`: `defdict_vectorcall()` already creates strong references for both before calling `defdict_init_impl()`.
The `default_factory` argument defaults to `None`, so `defaultdict()` is functionally equivalent to `defaultdict(None)`, thus AC declaration uses `default_factory: object = None`. It should not add `(c_default="NULL")` as that causes error at the `default_factory != Py_None` check, thus didn't use `default_factory: object(c_default="NULL") = None`.
Benchmark:
```
+-------------------------+--------+----------------------+
| Benchmark | main | patch |
+=========================+========+======================+
| defaultdict() | 146 ns | 110 ns: 1.33x faster |
+-------------------------+--------+----------------------+
| defaultdict(list) | 169 ns | 118 ns: 1.43x faster |
+-------------------------+--------+----------------------+
| defaultdict(list, kw) | 239 ns | 168 ns: 1.42x faster |
+-------------------------+--------+----------------------+
| defaultdict(list, **kw) | 286 ns | 358 ns: 1.25x slower |
+-------------------------+--------+----------------------+
| Geometric mean | (ref) | 1.21x faster |
+-------------------------+--------+----------------------+
```
```python
import pyperf
SETUP = """
from collections import defaultdict
kw = {'a': 1, 'b': 2}
"""
CASES = [
('defaultdict()', 'defaultdict()'),
('defaultdict(list)', 'defaultdict(list)'),
('defaultdict(list, kw)', 'defaultdict(list, kw)'),
('defaultdict(list, **kw)', 'defaultdict(list, **kw)')
]
runner = pyperf.Runner()
for name, stmt in CASES:
runner.timeit(name, stmt=stmt, setup=SETUP)
```
1 parent 64c20ff commit 6ed9dc2
2 files changed
Lines changed: 113 additions & 22 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
| 51 | + | |
51 | 52 | | |
52 | | - | |
| 53 | + | |
53 | 54 | | |
54 | 55 | | |
55 | 56 | | |
| |||
2455 | 2456 | | |
2456 | 2457 | | |
2457 | 2458 | | |
| 2459 | + | |
| 2460 | + | |
| 2461 | + | |
| 2462 | + | |
| 2463 | + | |
| 2464 | + | |
| 2465 | + | |
| 2466 | + | |
| 2467 | + | |
2458 | 2468 | | |
2459 | | - | |
| 2469 | + | |
| 2470 | + | |
| 2471 | + | |
2460 | 2472 | | |
2461 | 2473 | | |
2462 | 2474 | | |
2463 | | - | |
2464 | 2475 | | |
2465 | 2476 | | |
2466 | | - | |
2467 | | - | |
2468 | | - | |
2469 | | - | |
2470 | | - | |
2471 | | - | |
2472 | | - | |
2473 | | - | |
2474 | | - | |
2475 | | - | |
2476 | | - | |
2477 | | - | |
2478 | | - | |
2479 | | - | |
2480 | | - | |
| 2477 | + | |
| 2478 | + | |
| 2479 | + | |
| 2480 | + | |
2481 | 2481 | | |
2482 | | - | |
2483 | | - | |
| 2482 | + | |
| 2483 | + | |
| 2484 | + | |
| 2485 | + | |
| 2486 | + | |
2484 | 2487 | | |
2485 | 2488 | | |
2486 | 2489 | | |
| |||
2511 | 2514 | | |
2512 | 2515 | | |
2513 | 2516 | | |
| 2517 | + | |
2514 | 2518 | | |
2515 | 2519 | | |
2516 | 2520 | | |
| |||
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
0 commit comments