Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 15 additions & 2 deletions Python/optimizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1225,6 +1225,9 @@ int effective_trace_length(_PyUOpInstruction *buffer, int length)
}
#endif

static int uop_optimize_initialized = 0;
static int uop_optimize_flag = 0;
Copy link
Member

Choose a reason for hiding this comment

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

This isn't thread safe or subinterpreter safe. You might want to store the value in a PyThreadState / PyInterpreterState in pylifecycle.c instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I gave it a try, but it didn’t work out in the end. I guess I don’t have enough experience yet. I'm really sorry, I might not be able to handle this on my own. Maybe someone else could take it, or maybe you could help improve what I have so far :(


static int
uop_optimize(
_PyOptimizerObject *self,
Expand All @@ -1245,8 +1248,18 @@ 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 (!uop_optimize_initialized) {
char *env_var = Py_GETENV("PYTHON_UOPS_OPTIMIZE");
if (env_var == NULL || *env_var == '\0' || *env_var > '0') {
uop_optimize_flag = 1;
} else {
uop_optimize_flag = 0;
}
uop_optimize_initialized = 1;
}

if (uop_optimize_flag) {
length = _Py_uop_analyze_and_optimize(frame, buffer,
length,
curr_stackentries, &dependencies);
Expand Down
Loading