Repository navigation
Speed up trampolines by reading os.environ once per call - #656
Merged
Merged
Conversation
In the stats run, every call of a mutated function went through three os.environ lookups: get_mutant_under_test() checked "MUTANT_UNDER_TEST" in os.environ and then read it, and the stats branch read and parsed MUTMUT_DEPENDENCY_DEPTH. It also rebuilt the qualified name of the function with an f-string and mangled_name_from_mutant_name(). An os.environ lookup encodes the key and decodes the value, so for a function in an inner loop this costs more than the function itself. On main (1cce413), now that boxed#651 gets pypdf past the import, the stats run on pypdf fails because of it: test_lzw_decoder_large_stream_performance makes 2.0 M trampolined calls and has a 15 s pytest-timeout. Profiled, 6.0 M os.environ lookups were the largest single cost of the stats run. The test passes in 1.5 s without mutmut and took 18.9 s in the stats run, so the timeout ended the run. Changes: - get_mutant_under_test() reads MUTANT_UNDER_TEST once. The environment still wins over the process-local value, as before. - run_stats_collection() sets the dependency tracking depth with the new set_dependency_tracking_depth(), which keeps it in process-local state and still mirrors it into MUTMUT_DEPENDENCY_DEPTH. The trampoline reads the environment only when the depth was not set in the process. - Each trampoline builds the qualified name of its function on its first call in the stats run and keeps it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Owner
|
So this halves the amount of calls into os.environ right? But it seems like we could do much better than this by using a global variable that we just make sure to reset if os.environ is this slow... |
Contributor
Author
|
Just a short heads-up: I like the idea and started pretty much immediately to work on it - but even with AI it takes some time. Mostly benchmarking / verifying the answer to make sure that it works as expected. |
boxed
pushed a commit
that referenced
this pull request
Dec 2, 2026
Since #656 the trampoline still calls get_mutant_under_test() on every call of a mutated function, and that reads os.environ["MUTANT_UNDER_TEST"] first so that a value set only in the environment wins. The lookup costs 450 to 500 ns, a global read 40 ns. Changes: - The active mutant and the dependency tracking depth of the stats run live in module globals. They are filled once at import from MUTANT_UNDER_TEST and MUTMUT_DEPENDENCY_DEPTH, so pytest-xdist workers and subprocesses that tests start still get them. - set_mutant_under_test() and set_dependency_tracking_depth() set the global and still write the env var for processes started afterwards. - The trampoline reads the globals directly. - An autouse fixture in tests/conftest.py restores the globals and env vars after each test; tests that only set the env var now go through set_mutant_under_test(). Behaviour change: setting MUTANT_UNDER_TEST inside a running process no longer switches the active mutant. A test that clears os.environ (#511) cannot affect the active mutant at all. Per call: 1,332 to 778 ns with no mutant active, 2,746 to 1,855 ns in the stats run. pypdf's 64 timed tests in the stats run: 56.0 / 58.6 s on main, 51.5 / 50.2 s with this change (25.9 s without mutmut). The whole pypdf mutmut run (29,852 mutants) completed with this change. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
It feels like pypdf has a ton of special cases 😅 We have timed tests - those also cause internally some issues, e.g. when test coverage is enabled they run slower. We try to be robust against that, but it's hard.
The Problem
In the stats run, every call of a mutated function went through three
os.environlookups:get_mutant_under_test()checked "MUTANT_UNDER_TEST" in os.environ and then read it, and the stats branch read and parsed MUTMUT_DEPENDENCY_DEPTH. It also rebuilt the qualified name of the function with an f-string and mangled_name_from_mutant_name(). An os.environ lookup encodes the key and decodes the value, so for a function in an inner loop this costs more than the function itself.On main (1cce413), now that #651 gets pypdf past the import, the stats run on pypdf fails because of it:
pypdfs
test_lzw_decoder_large_stream_performancemakes 2.0 M trampolined calls and has a 15 s pytest-timeout. Profiled, 6.0 M os.environ lookups were the largest single cost of the stats run. The test passes in 1.5 s without mutmut and took 18.9 s in the stats run, so the timeout ended the run.The Solution
Although os.environ feels like a normal dict, it is not. This can be seen with a benchmark:
Other improvements: