Repository navigation
Description
Bug report
Issue #158195 / PR #159972 "Do not hold the type lock during stop-the-world" (backported to 3.14) has caused a performance regression in concurrent type slot updates on the free-threaded build, biggest regression on Windows, medium on macOS and smaller on Ubuntu.
On Windows CI, test_opcache is now often taking more than 10 minutes and needing a slow re-run, contributing to one CI job going from ~28 mins to ~38 mins and becoming the bottleneck. In the last 30 main runs, test_opcache was re-run in 19 arm64 and 15 x64 Windows free-threading debug jobs.
- Last build before, 2ef3728: https://github.com/python/cpython/actions/runs/41472050477/job/125368047226
- First build after, b89f1c9: https://github.com/python/cpython/actions/runs/41480423258/job/125397096430
Median test_opcache time on main GitHub Actions over 12 runs before and 12 after the PR was merged:
| Job | Before | After |
|---|---|---|
| Windows free-threading, arm64, switch-case | under 30 s | 10 min (timeout) |
| Windows free-threading, x64, switch-case | under 30 s | 7.9 min |
| Windows free-threading, x64, tail-call | under 30 s | 3.6 min |
| macOS free-threading | under 30 s | 5.0 min |
| Ubuntu free-threading, x64 | under 30 s | 66 s |
| Ubuntu free-threading, arm | under 30 s | 42 s |
| TSan free-threading | 51 s | 78 s |
| All non-free-threading Windows jobs | under 30 s | under 30 s |
- Same pattern on
3.15and3.14. - Free-threading only: the default (GIL) builds are unaffected.
- Tail-call too: the x64 tail-call job went from under 30 seconds to 3.6 minutes. It doesn't time out because those are release builds with more headroom.
- Windows arm64 tail-call: not shown here, those jobs were only added recently.
The extra time is in TestRacesDoNotCrash.test_load_attr_getattribute_overridden and test_binary_subscr_getitem. Locally on macOS the whole file went from 1.1 s to 26.9 s (debug build).
Benchmark to repro
Here's a minimal benchmark derived from test_opcache:
"""Time threads assigning a special method on a class (a type slot update)."""
import sys
import threading
import time
THREADS = int(sys.argv[1]) if len(sys.argv) > 1 else 4
N = int(sys.argv[2]) if len(sys.argv) > 2 else 2000
class C:
__getitem__ = lambda self, item: None
def write():
for _ in range(N):
C.__getitem__ = lambda self, item: None
threads = [threading.Thread(target=write) for _ in range(THREADS)]
start = time.perf_counter()
for t in threads:
t.start()
for t in threads:
t.join()
elapsed = time.perf_counter() - start
print(f"{THREADS} threads x {N} assignments: {elapsed:.2f}s "
f"({elapsed / (THREADS * N) * 1e6:.0f} us each)")Running a free-threaded (debug) build locally on macOS:
- Before, 2ef3728:
4 threads x 2000 assignments: 0.01s (1 us each) - After, b89f1c9:
4 threads x 2000 assignments: 2.31s (289 us each)
CPython versions tested on:
CPython main branch, 3.16, 3.15, 3.14
Operating systems tested on:
Windows, macOS, Linux
Activity