]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
gh-115999: Implement thread-local bytecode and enable specialization for `BINARY_OP...
authormpage <mpage@cs.stanford.edu>
Mon, 4 Nov 2024 19:13:32 +0000 (11:13 -0800)
committerGitHub <noreply@github.com>
Mon, 4 Nov 2024 19:13:32 +0000 (11:13 -0800)
commit2e95c5ba3bf7e5004c7e2304afda4a8f8e2443a7
treede32ac52ed5ffcb9460dfc062effc6b4b662ee5d
parente5a4b402ae55f5eeeb44d3e7bc3f3ec39b249846
gh-115999: Implement thread-local bytecode and enable specialization for `BINARY_OP` (#123926)

Each thread specializes a thread-local copy of the bytecode, created on the first RESUME, in free-threaded builds. All copies of the bytecode for a code object are stored in the co_tlbc array on the code object. Threads reserve a globally unique index identifying its copy of the bytecode in all co_tlbc arrays at thread creation and release the index at thread destruction. The first entry in every co_tlbc array always points to the "main" copy of the bytecode that is stored at the end of the code object. This ensures that no bytecode is copied for programs that do not use threads.

Thread-local bytecode can be disabled at runtime by providing either -X tlbc=0 or PYTHON_TLBC=0. Disabling thread-local bytecode also disables specialization.

Concurrent modifications to the bytecode made by the specializing interpreter and instrumentation use atomics, with specialization taking care not to overwrite an instruction that was instrumented concurrently.
44 files changed:
Include/cpython/code.h
Include/cpython/initconfig.h
Include/internal/pycore_ceval.h
Include/internal/pycore_code.h
Include/internal/pycore_frame.h
Include/internal/pycore_gc.h
Include/internal/pycore_index_pool.h [new file with mode: 0644]
Include/internal/pycore_interp.h
Include/internal/pycore_tstate.h
Include/internal/pycore_uop_ids.h
Include/internal/pycore_uop_metadata.h
Lib/test/support/__init__.py
Lib/test/test_capi/test_config.py
Lib/test/test_capi/test_opt.py
Lib/test/test_cmd_line.py
Lib/test/test_dis.py
Lib/test/test_embed.py
Lib/test/test_sys.py
Lib/test/test_thread_local_bytecode.py [new file with mode: 0644]
Makefile.pre.in
Modules/_opcode.c
Modules/_testinternalcapi.c
Objects/codeobject.c
Objects/frameobject.c
Objects/typeobject.c
PCbuild/_freeze_module.vcxproj
PCbuild/_freeze_module.vcxproj.filters
PCbuild/pythoncore.vcxproj
PCbuild/pythoncore.vcxproj.filters
Python/bytecodes.c
Python/ceval.c
Python/ceval_macros.h
Python/executor_cases.c.h
Python/frame.c
Python/gc_free_threading.c
Python/generated_cases.c.h
Python/index_pool.c [new file with mode: 0644]
Python/initconfig.c
Python/instrumentation.c
Python/optimizer_cases.c.h
Python/pystate.c
Python/specialize.c
Python/sysmodule.c
Tools/gdb/libpython.py