python / python/cpython

JIT x Free-Threading

Open
#141,594 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

interpreter-core topic-free-threading topic-JIT type-feature
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Feature or enhancement

Proposal:

This provides an alternative design to https://github.com/python/cpython/issues/133171. I have a branch where all tests (including FT) pass.

Part 1:
For the first and simplest part, we could add a watcher/callback to thread creation. When multiple threads are created, we simply invalidate the executor as _CHECK_VALIDITY fails.

This means single threaded code gets all the benefits of JIT in the future, while multi-threaded code will lose all the benefits. However, both can coexist at the same time in the same build. We will finally get JIT + FT in a limited form. Pending part 2.

Part 2:
If we detect multiple threads are running, we turn off the non-thread safe optimizations, and redo the trace with those optimizations off. If we detect only a single thread, we follow part 1 and run till we get invalidated. This allows single threaded code to run faster, while multi-threaded code will run a little slower. Note that all optimizations I've proposed for the JIT in the past 6 months and in the future are FT safe (in theory. whether they are implemented in practice is different).

Together, part 1 and part 2 get the best of both worlds: for single-threaded code, we get max performance, even recovering some of the perf single-threaded perf lost from FT. In part 2, we get max compatibility with multiple threads.

We need both parts to get the max perf out of the JIT on FT.

Follow-up after this:

  • Lock/unlock removal in FT code.
Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

https://github.com/python/cpython/issues/133171

Linked PRs
  • gh-141595

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the linked discussion in issue 133171 and the related work in PR 141595. The proposal has no files or tests named; completion would require agreeing on and implementing both JIT/free-threading parts, followed by the full test suite, including free-threading tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers, performance
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.