FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-141504: Factor out tracing and optimization heuristics into a single object by corona10 · Pull Request #143381 · python/cpython · GitHub

Repository navigation

gh-141504: Factor out tracing and optimization heuristics into a single object - #143381

Merged
corona10 merged 15 commits into
python:mainfrom
corona10:gh-141504
Jan 3, 2026
Merged

corona10 merged 15 commits into
python:mainfrom
corona10:gh-141504

Conversation

corona10 commented Jan 3, 2026 •
edited by bedevere-app Bot
Loading

Copy link
Copy Markdown
Member

corona10 changed the title gh-141504: Factor out tracing and optimization heuristics into a single object [WIP] gh-141504: Factor out tracing and optimization heuristics into a single object Jan 3, 2026
corona10 marked this pull request as draft January 3, 2026 07:23
Comment thread Python/pystate.c Outdated
_tstate->jit_metrics.side_exit_initial_value = SIDE_EXIT_INITIAL_VALUE;
_tstate->jit_metrics.side_exit_initial_backoff = SIDE_EXIT_INITIAL_BACKOFF;

char *env = Py_GETENV("PYTHON_JIT_JUMP_BACKWARD_INITIAL_VALUE");

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

We may need to extract utility function to handle those things.

corona10 changed the title [WIP] gh-141504: Factor out tracing and optimization heuristics into a single object gh-141504: Factor out tracing and optimization heuristics into a single object Jan 3, 2026
corona10 marked this pull request as ready for review January 3, 2026 07:59
Comment thread Include/internal/pycore_tstate.h Outdated
Comment on lines +56 to +62
typedef struct _PyJitMetrics {
uint16_t jump_backward_initial_value;
uint16_t jump_backward_initial_backoff;
uint16_t side_exit_initial_value;
uint16_t side_exit_initial_backoff;
} _PyJitMetrics;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Can you rename this to _PyJitPolicy pleases?

Also please then wrap this into an overall struct called _PyPolicies. The reason is that we might want to add more structs later, like for the interpreter.

So it should be

Suggested change
typedef struct _PyJitMetrics {
uint16_t jump_backward_initial_value;
uint16_t jump_backward_initial_backoff;
uint16_t side_exit_initial_value;
uint16_t side_exit_initial_backoff;
} _PyJitMetrics;
typedef struct _PyJitPolicy {
uint16_t jump_backward_initial_value;
uint16_t jump_backward_initial_backoff;
uint16_t side_exit_initial_value;
uint16_t side_exit_initial_backoff;
} _PyJitPolicy;
typedef struct _PyPolicy {
_PyJitPolicy jit;
} _PyPolicy;

Comment thread Python/specialize.c Outdated
PyThreadState *tstate = _PyThreadState_GET();
_PyThreadStateImpl *tstate_impl = (_PyThreadStateImpl *)tstate;
jump_counter = initial_jump_backoff_counter(
tstate_impl->policy.jit.jump_backward_initial_value,

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

@Fidget-Spinner
The crash is directly related to this line.
But ENABLE_SPECIALIZATION_FT does not mean TIER2 is enabled.

Do we need to move jump_backward_initial_value and jump_backward_initial_backoff to policy.interpreter?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

JUMP_BACKWARD_INITIAL_VALUE is only used by the JIT. On normal build/FT build, the CACHE of JUMP_BACKWARD is unused. So we can just keep it for the JIT.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Hmm I realised the CI is now failing because FT doesn't build with JIT.

In that case, yes please move this to an interpreter policy.

Comment thread Include/internal/pycore_tstate.h Outdated
#endif
#if _Py_TIER2
_PyJitTracerState jit_tracer_state;
_PyPolicy policy;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This needs to be moved out of the ifdef. CI is failing.

Fidget-Spinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Some comments to cleanup the code. After this I will approve. Thanks!

Comment thread Include/internal/pycore_backoff.h Outdated
#define JUMP_BACKWARD_INITIAL_BACKOFF 6
static inline _Py_BackoffCounter
initial_jump_backoff_counter(void)
initial_jump_backoff_counter(uint16_t initial_value, uint16_t initial_backoff)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This should take a single value _tstate/tstate and grab the jump backward values from it.

Comment thread Include/internal/pycore_backoff.h Outdated

static inline _Py_BackoffCounter
initial_temperature_backoff_counter(void)
initial_temperature_backoff_counter(uint16_t initial_value, uint16_t initial_backoff)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Same here.

Comment thread Python/pystate.c Outdated
}

static inline void
init_jit_metric(uint16_t *target, const char *env_name, uint16_t default_value,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Rename to init_policy

Comment thread Python/pystate.c Outdated
_tstate->asyncio_running_task = NULL;

// Initialize interpreter policy from environment variables
init_jit_metric(&_tstate->policy.interp.jump_backward_initial_value,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

rename here as well

Fidget-Spinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Great. Thanks!

corona10 enabled auto-merge (squash) January 3, 2026 14:56
corona10 disabled auto-merge January 3, 2026 14:58
corona10 enabled auto-merge (squash) January 3, 2026 14:58
corona10 merged commit 12283f6 into python:main Jan 3, 2026
69 of 70 checks passed

Copy link
Copy Markdown
Member

Note: the names are a little lengthy. But considering this is an internal CPython only env var used for testing, we can determine proper names later when/if we decide to expose them.

markshannon commented Jan 5, 2026 •
edited
Loading

Copy link
Copy Markdown
Member

I have some questions.

Why set thresholds per thread, rather than per interpreter. Why would we want per-thread values?
Why set and store the threshold and backoff separately rather than as a single backoff counter?

I've added a new comment on the issue, that will hopefully clarify what I was after.

thunder-coding pushed a commit to thunder-coding/cpython that referenced this pull request Feb 15, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants


Back | FazBrowse Home | New Git URL