[3.13] gh-131988: Fix a multithreaded scaling regression (#131989)

gh-131988: Fix a multithreaded scaling regression

The 3.13 free threaded build immortalizes certain objects to avoid
reference count contention. In gh-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.
This commit is contained in:
Sam Gross 2025-04-07 14:13:02 -04:00 committed by GitHub
parent bd2f518555
commit 1fcf409ace
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 4 additions and 2 deletions

View file

@ -0,0 +1,2 @@
Fix a performance regression that caused scaling bottlenecks in the free
threaded build in 3.13.1 and 3.13.2.

View file

@ -1587,10 +1587,10 @@ new_threadstate(PyInterpreterState *interp, int whence)
HEAD_UNLOCK(interp->runtime);
#ifdef Py_GIL_DISABLED
if (id == 1) {
if (id > 1) {
if (_Py_atomic_load_int(&interp->gc.immortalize) == 0) {
// Immortalize objects marked as using deferred reference counting
// the first time a non-main thread is created.
// once a non-main thread is created, if we haven't already done so.
_PyGC_ImmortalizeDeferredObjects(interp);
}
}