Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

@vstinnervstinner commentedMar 3, 2026
edited by bedevere-appbot
Loading

@vstinner
Copy link
MemberAuthor

vstinner commentedMar 3, 2026
edited
Loading

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

Benchmark results:

Benchmarknogilnogil_optim
str keys334 ms10.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):

Benchmarkrefnogilnogil_optim
int keys8.36 ms7.40 ms: 1.13x faster7.27 ms: 1.15x faster
str keys231 ms334 ms: 1.45x slower10.0 ms: 22.94x faster
Geometric mean(ref)1.13x slower5.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 freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@methanemethaneAwaiting requested review from methanemethane is a code owner

@markshannonmarkshannonAwaiting requested review from markshannonmarkshannon is a code owner

Assignees

No one assigned

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

2 participants

@vstinner@colesbury

[8]ページ先頭

©2009-2026 Movatter.jp