python / cpython

The Python programming language
https://www.python.org
Other
63.37k stars 30.33k forks source link

Optimizer calls `getenv` on every optimization run #116772

Open mdboom opened 7 months ago

mdboom commented 7 months ago

Bug report

Bug description:

The optimizer currently calls getenv during every optimization attempt. This has a small, but statistically significant 0.5% impact on performance. Do we need to do this each time, or could we read it to a flag during initialization?

diff --git a/Python/optimizer.c b/Python/optimizer.c
index 88c45f2e73c..b55556d30ab 100644
--- a/Python/optimizer.c
+++ b/Python/optimizer.c
@@ -1010,8 +1010,7 @@ uop_optimize(
         return err;
     }
     OPT_STAT_INC(traces_created);
-    char *env_var = Py_GETENV("PYTHON_UOPS_OPTIMIZE");
-    if (env_var == NULL || *env_var == '\0' || *env_var > '0') {
+    if (true) {
         err = _Py_uop_analyze_and_optimize(frame, buffer,
                                            UOP_MAX_TRACE_LENGTH,
                                            curr_stackentries, &dependencies);

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

ronaldoussoren commented 7 months ago

IMHO options should be processed using the PyConfig interface, that way all configuration stays in a central place and it is easier for embedders to change this setting. Also: With the current code there's no way to enable this option when using python -E.