Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork34.2k
Open
Description
Frozendict lookups use locks and atomic operations for thread safety. We should be able to use a faster implementation forfrozendict, avoid locks and atomic operations, since thefrozendict mapping is immutable.
Stress test on frozendict lookup comparing a regular build (ref) to a free-threading build (nogil):
| Benchmark | ref | nogil |
|---|---|---|
| int keys | 8.36 ms | 7.40 ms: 1.13x faster |
| str keys | 231 ms | 334 ms: 1.45x slower |
| Geometric mean | (ref) | 1.13x slower |
Benchmark code:
Details
importpyperffromthreadingimportThreadimportosfrozendict_int_keys=frozendict((i,i)foriinrange(10))frozendict_str_keys=frozendict((str(i),i)foriinrange(10))LOOPS=2**13NTHREAD=os.process_cpu_count()defcompute_int_keys():data=frozendict_int_keys# use a local variabletotal=0for_inrange(LOOPS):total+=data[0]total+=data[1]total+=data[2]total+=data[3]total+=data[4]total+=data[5]total+=data[6]total+=data[7]total+=data[8]total+=data[9]returntotaldefcompute_str_keys():data=frozendict_str_keys# use a local variabletotal=0for_inrange(LOOPS):total+=data['0']total+=data['1']total+=data['2']total+=data['3']total+=data['4']total+=data['5']total+=data['6']total+=data['7']total+=data['8']total+=data['9']returntotaldefbench(target):threads= [Thread(target=target)for_inrange(NTHREAD)]forthreadinthreads:thread.start()forthreadinthreads:thread.join()runner=pyperf.Runner()runner.bench_func('int keys',bench,compute_int_keys)runner.bench_func('str keys',bench,compute_str_keys)