gh-141510: Avoid critical section on frozendict copy#145920
Merged
vstinner merged 2 commits intopython:mainfrom Mar 16, 2026
Merged
gh-141510: Avoid critical section on frozendict copy#145920vstinner merged 2 commits intopython:mainfrom
vstinner merged 2 commits intopython:mainfrom
Conversation
Member
Author
|
cc @corona10 |
corona10
approved these changes
Mar 13, 2026
clin1234
reviewed
Mar 13, 2026
Member
Author
|
Microbenchmark on Detailsimport threading
import time
import pyperf
import sys
KEYS = 4_096
LOOPS = 2 ** 9
NTHREAD = 50
code = f"fd = frozendict({', '.join(f'key{key}=1' for key in range(KEYS))})"
exec(code)
assert(len(fd) == KEYS)
assert(all(sys._is_immortal(key) for key in fd.keys()))
fd2 = frozendict(x=1, y=2, z=3)
class Worker(threading.Thread):
def __init__(self, fd, fd2):
super().__init__()
self.copies = 0
self.fd = fd
self.fd2 = fd2
def run(self):
fd = self.fd
fd2 = self.fd2
for _ in range(LOOPS):
join = fd | fd2
def bench(fd, fd2):
threads = [Worker(fd, fd2) for _ in range(NTHREAD)]
for thread in threads:
thread.start()
for thread in threads:
thread.join()
runner = pyperf.Runner()
runner.bench_func('bench', bench, fd, fd2)Benchmark result: I ran the benchmark on Fedora 43 on my laptop with 12 threads (6 CPU cores) on a free-threaded Python build. |
Member
Author
A |
Member
Author
|
This change increases the scalability on the following operations:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.