]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-128691: Use deferred reference counting on `_thread._local` (#128693)
authorSam Gross <colesbury@gmail.com>
Fri, 10 Jan 2025 00:59:10 +0000 (19:59 -0500)
committerGitHub <noreply@github.com>
Fri, 10 Jan 2025 00:59:10 +0000 (00:59 +0000)
This change, along with the LOAD_ATTR specializations, makes the
"thread_local_read" micro benchmark in Tools/ftscalingbench/ftscalingbench.py
scale well to multiple threads.

Modules/_threadmodule.c

index 2cbdfeb09b95ae1c3543b49d673308f06117bfd1..d19ae326bd6b4822a7b3332dc88f6bf6e02969cd 100644 (file)
@@ -1414,6 +1414,10 @@ local_new(PyTypeObject *type, PyObject *args, PyObject *kw)
         return NULL;
     }
 
+    // gh-128691: Use deferred reference counting for thread-locals to avoid
+    // contention on the shared object.
+    _PyObject_SetDeferredRefcount((PyObject *)self);
+
     self->args = Py_XNewRef(args);
     self->kw = Py_XNewRef(kw);