https://github.com/python/cpython/commit/1fcf409acef8a7a039353cf11a0bbb24ca6722f8
commit: 1fcf409acef8a7a039353cf11a0bbb24ca6722f8
branch: 3.13
author: Sam Gross <colesb...@gmail.com>
committer: Yhg1s <tho...@python.org>
date: 2025-04-07T20:13:02+02:00
summary:

[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.

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2025-04-01-19-25-05.gh-issue-131988.sbYLEs.rst
M Python/pystate.c

diff --git 
a/Misc/NEWS.d/next/Core_and_Builtins/2025-04-01-19-25-05.gh-issue-131988.sbYLEs.rst
 
b/Misc/NEWS.d/next/Core_and_Builtins/2025-04-01-19-25-05.gh-issue-131988.sbYLEs.rst
new file mode 100644
index 00000000000000..44f71238739ae0
--- /dev/null
+++ 
b/Misc/NEWS.d/next/Core_and_Builtins/2025-04-01-19-25-05.gh-issue-131988.sbYLEs.rst
@@ -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.
diff --git a/Python/pystate.c b/Python/pystate.c
index 900cba0de15a4e..5222a7ec140528 100644
--- a/Python/pystate.c
+++ b/Python/pystate.c
@@ -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);
         }
     }

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: arch...@mail-archive.com

Reply via email to