Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Cache ``getenv`` result in optimizer.c to reduce redundant calls during
optimization
27 changes: 25 additions & 2 deletions Python/optimizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1200,6 +1200,28 @@ int effective_trace_length(_PyUOpInstruction *buffer, int length)
}
#endif

static bool uops_optimize_initialized = false;
static bool uops_optimize_flag = false;

static void
initialize_uops_optimize_flag(void) {
if (!uops_optimize_initialized) {
PyInterpreterState *interp = _PyInterpreterState_GET();
char *env_var = Py_GETENV("PYTHON_UOPS_OPTIMIZE");
Copy link
Member

@Fidget-Spinner Fidget-Spinner Apr 29, 2025

Choose a reason for hiding this comment

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

Can you please do this instead at where we check for PYTHON_JIT ? So in pylifecycle.c.

Set the thread state/interp state there as well.

bool uops_optimize_flag = (env_var == NULL || *env_var == '\0' || *env_var > '0');
if (interp != NULL) {
interp->uops_optimize_flag = uops_optimize_flag;
}
uops_optimize_initialized = true;
}
else {
PyInterpreterState *interp = _PyInterpreterState_GET();
if (interp != NULL) {
uops_optimize_flag = interp->uops_optimize_flag;
}
}
}

static int
uop_optimize(
_PyInterpreterFrame *frame,
Expand All @@ -1208,6 +1230,8 @@ uop_optimize(
int curr_stackentries,
bool progress_needed)
{
initialize_uops_optimize_flag();

_PyBloomFilter dependencies;
_Py_BloomFilter_Init(&dependencies);
_PyUOpInstruction buffer[UOP_MAX_TRACE_LENGTH];
Expand All @@ -1219,8 +1243,7 @@ uop_optimize(
}
assert(length < UOP_MAX_TRACE_LENGTH);
OPT_STAT_INC(traces_created);
char *env_var = Py_GETENV("PYTHON_UOPS_OPTIMIZE");
if (env_var == NULL || *env_var == '\0' || *env_var > '0') {
if (uops_optimize_flag) {
length = _Py_uop_analyze_and_optimize(frame, buffer,
length,
curr_stackentries, &dependencies);
Expand Down
Loading