Skip to content

gh-145481: Optimize frozendict lookup in free threading#145482

Open
vstinner wants to merge 2 commits intopython:mainfrom
vstinner:frozendict_lookup
Open

gh-145481: Optimize frozendict lookup in free threading#145482
vstinner wants to merge 2 commits intopython:mainfrom
vstinner:frozendict_lookup

Conversation

@vstinner
Copy link
Member

@vstinner vstinner commented Mar 3, 2026

@vstinner
Copy link
Member Author

vstinner commented Mar 3, 2026

According the benchmark below, frozendict[key] lookup scales way better with this change: up to 33x faster.

Benchmark results:

Benchmark nogil nogil_optim
str keys 334 ms 10.0 ms: 33.21x faster
Geometric mean (ref) 5.81x faster

Benchmark hidden because not significant (1): int keys

Benchmark results compared to the regular build (ref):

Benchmark ref nogil nogil_optim
int keys 8.36 ms 7.40 ms: 1.13x faster 7.27 ms: 1.15x faster
str keys 231 ms 334 ms: 1.45x slower 10.0 ms: 22.94x faster
Geometric mean (ref) 1.13x slower 5.14x faster

Python built in release mode, gcc -O3 (without PGO, LTO, or CPU isolation).

Benchmark run on my Fedora 43 laptop which has 12 threads (6 CPU cores).

@colesbury
Copy link
Contributor

The string benchmark has really bad scaling because the strings aren't interned so we have reference count contention, plus some other things means that we also fall back to the locking code path.

Most dicts (and presumably frozendicts) use string literals as keys, which are interned and immortalized and avoid this problem.

You can see this if you update the benchmark:

frozendict_str_keys = frozendict({'0': 0, '1': 1, '2': 2, '3': 3, '4': 4,
                                  '5': 5, '6': 6, '7': 7, '8': 8, '9': 9})

On my machine, I'm not seeing an improvement with this PR for string literals keys or ints. In fact, it's slightly worse, which is surprising. (Maybe something weird with branch predictor behavior?)

main (671a953)

...........
int keys: Mean +- std dev: 6.24 ms +- 0.20 ms
...........
str keys: Mean +- std dev: 6.52 ms +- 0.24 ms

PR + frozendict_str_keys literals

...........
int keys: Mean +- std dev: 6.59 ms +- 0.33 ms
...........
str keys: Mean +- std dev: 6.74 ms +- 0.26 ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants