Skip to content

Commit 22349fd

Browse files
committed
pythongh-131988: Fix a multithreaded scaling regression
The 3.13 free threaded build immortalizes certain objects to avoid reference count contention. In pythongh-127114 the condition was unintentionally changed to happen when the first thread was created instead of the first non-main thread. The `interp->gc.immortalize` field is then cleared again during `_PyGC_Init()`. Change the condition so that we check if we should immortalize objects using deferred reference counting whenever a non-main thread is created.
1 parent c318a03 commit 22349fd

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a performance regression that caused scaling bottlenecks in the free
2+
threaded build in 3.13.1 and 3.13.2.

Python/pystate.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1587,10 +1587,10 @@ new_threadstate(PyInterpreterState *interp, int whence)
15871587

15881588
HEAD_UNLOCK(interp->runtime);
15891589
#ifdef Py_GIL_DISABLED
1590-
if (id == 1) {
1590+
if (id > 1) {
15911591
if (_Py_atomic_load_int(&interp->gc.immortalize) == 0) {
15921592
// Immortalize objects marked as using deferred reference counting
1593-
// the first time a non-main thread is created.
1593+
// once a non-main thread is created, if we haven't already done so.
15941594
_PyGC_ImmortalizeDeferredObjects(interp);
15951595
}
15961596
}

0 commit comments

Comments
 (0)