Skip to content

Commit 537431e

Browse files
committed
Cache getenv result in optimizer.c to reduce calls
1 parent 78842e4 commit 537431e

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Cache ``getenv`` result in optimizer.c to reduce redundant calls during
2+
optimization

Python/optimizer.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,6 +1225,9 @@ int effective_trace_length(_PyUOpInstruction *buffer, int length)
12251225
}
12261226
#endif
12271227

1228+
static int uop_optimize_initialized = 0;
1229+
static int uop_optimize_flag = 0;
1230+
12281231
static int
12291232
uop_optimize(
12301233
_PyOptimizerObject *self,
@@ -1245,8 +1248,18 @@ uop_optimize(
12451248
}
12461249
assert(length < UOP_MAX_TRACE_LENGTH);
12471250
OPT_STAT_INC(traces_created);
1248-
char *env_var = Py_GETENV("PYTHON_UOPS_OPTIMIZE");
1249-
if (env_var == NULL || *env_var == '\0' || *env_var > '0') {
1251+
1252+
if (!uop_optimize_initialized) {
1253+
char *env_var = Py_GETENV("PYTHON_UOPS_OPTIMIZE");
1254+
if (env_var == NULL || *env_var == '\0' || *env_var > '0') {
1255+
uop_optimize_flag = 1;
1256+
} else {
1257+
uop_optimize_flag = 0;
1258+
}
1259+
uop_optimize_initialized = 1;
1260+
}
1261+
1262+
if (uop_optimize_flag) {
12501263
length = _Py_uop_analyze_and_optimize(frame, buffer,
12511264
length,
12521265
curr_stackentries, &dependencies);

0 commit comments

Comments
 (0)