Skip to content

gh-160913: Use the dict freelist in dict() - #160922

Merged
eendebakpt merged 1 commit into
python:mainfrom
eendebakpt:dict-vectorcall-freelist
Oct 30, 2026
Merged

eendebakpt merged 1 commit into
python:mainfrom
eendebakpt:dict-vectorcall-freelist

Conversation

@eendebakpt

@eendebakpt eendebakpt commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

dict_vectorcall allocated every instance with _PyType_AllocNoTrack(), so skipping the freelist. tp_vectorcall is not inherited by subclasses, so dict_vectorcall() only ever runs for the exact dict type.

case main PR speedup (p10..p90)
dict() 73.5 ns 61.0 ns 1.21x (1.17..1.26)
dict(d3) 103.5 ns 91.9 ns 1.13x (1.07..1.18)
dict(pairs_3) 199.6 ns 182.6 ns 1.09x (1.03..1.20)
dict(a=1, b=2) 130.1 ns 109.5 ns 1.17x (1.13..1.22)
dict(d_100) 430.9 ns 421.6 ns 1.05x (0.95..1.14)
ctl: x + y 47.0 ns 48.2 ns 1.00x (0.93..1.07)
ctl: {} 54.4 ns 54.6 ns 1.00x (0.98..1.04)
ctl: d3.copy() 96.2 ns 97.3 ns 1.00x (0.92..1.06)

dict_vectorcall() allocated every instance with _PyType_AllocNoTrack(),
so dict() never reused the objects that {} and PyDict_New() recycle
through the freelist. Allocate with PyDict_New() instead. tp_vectorcall
is not inherited by subclasses, so dict_vectorcall() only ever runs for
the exact dict type; assert that rather than checking it.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@cmaloney

Copy link
Copy Markdown
Contributor

There's an additional direct allocation which misses the freelist in copy_lock_held_untracked that could also be updated to use the freelist. It's not that common but could move to the existing new_dict_untracked + new_frozendict_untracked which use the freelist.

@eendebakpt
eendebakpt merged commit 1e406e6 into python:main Oct 30, 2026
59 of 63 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

skip news type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants