diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-11-28-11-20-00.gh-issue-162711.bR4hTq.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-28-11-20-00.gh-issue-162711.bR4hTq.rst new file mode 100644 index 00000000000000..7ba8c6e4bbcf25 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-28-11-20-00.gh-issue-162711.bR4hTq.rst @@ -0,0 +1,3 @@ +Fix a performance regression in the :term:`free-threaded build` where a +thread ran slower while the main thread was executing the same function +concurrently. diff --git a/Python/ceval_macros.h b/Python/ceval_macros.h index 36921e35533433..27c9a8d8688533 100644 --- a/Python/ceval_macros.h +++ b/Python/ceval_macros.h @@ -384,8 +384,13 @@ static void dtrace_function_return(_PyInterpreterFrame *); } while (0); #endif -#ifdef ENABLE_SPECIALIZATION -/* Multiple threads may execute these concurrently if thread-local bytecode is +#if defined(ENABLE_SPECIALIZATION) && defined(_Py_TIER2) +/* The branch history is only used by the tier 2 optimizer. Don't record it + * otherwise: in the free-threaded build the main thread's bytecode is stored + * inline in the code object, so the store would cause false sharing with + * threads reading the code object's other fields. + * + * Multiple threads may execute these concurrently if thread-local bytecode is * disabled and they all execute the main copy of the bytecode. Specialization * is disabled in that case so the value is unused, but the RMW cycle should be * free of data races.