Skip to content

Commit 62ef422

Browse files
gh-162711: Only record branch history when tier 2 is enabled
In the free-threaded build the main thread's bytecode is stored inline in the code object, so the store done by RECORD_BRANCH_TAKEN on every conditional jump caused false sharing with other threads reading co_tlbc and _co_instrumentation_version when calling the same function. The branch history is only used by the tier 2 optimizer, so only record it when _Py_TIER2 is defined.
1 parent 49a344e commit 62ef422

2 files changed

Lines changed: 10 additions & 2 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix a performance regression in the :term:`free-threaded build` where a
2+
thread ran slower while the main thread was executing the same function
3+
concurrently.

‎Python/ceval_macros.h‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,13 @@ static void dtrace_function_return(_PyInterpreterFrame *);
384384
} while (0);
385385
#endif
386386

387-
#ifdef ENABLE_SPECIALIZATION
388-
/* Multiple threads may execute these concurrently if thread-local bytecode is
387+
#if defined(ENABLE_SPECIALIZATION) && defined(_Py_TIER2)
388+
/* The branch history is only used by the tier 2 optimizer. Don't record it
389+
* otherwise: in the free-threaded build the main thread's bytecode is stored
390+
* inline in the code object, so the store would cause false sharing with
391+
* threads reading the code object's other fields.
392+
*
393+
* Multiple threads may execute these concurrently if thread-local bytecode is
389394
* disabled and they all execute the main copy of the bytecode. Specialization
390395
* is disabled in that case so the value is unused, but the RMW cycle should be
391396
* free of data races.

0 commit comments

Comments
 (0)