Repository navigation
Keep the active mutant in a module global - #661
Merged
Merged
Conversation
Since boxed#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 (boxed#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>
Contributor
Author
|
I'm VERY uncertain if those changes might have side-effects on users workflows as I don't know how people typically use mutmut. It works on my machine with pypdf. And all tests pass. And getting rid of mocking / patching always is nice :-) |
Owner
|
We'll just have to try. Thanks! |
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.
This PR speeds up every call of a mutated function by 30 to 40 %: the trampoline now reads the active mutant from a module global instead of
os.environ.The Problem
"foo" in os.environas they are roughly 15x as expensive as ax in dictcall:Even after #656 the trampoline still calls
get_mutant_under_test()on every call of a mutated function. That readsos.environ["MUTANT_UNDER_TEST"]first so that a value set only in the environment wins. The lookup costs 450 to 600 ns, a global read 40 ns (see numbers above)The Solution
Changed Behavior
setting
MUTANT_UNDER_TESTinside a running process no longer switches the active mutant. A test that clearsos.environ(#511) cannot affect the active mutant at all.Benchmark
Per call of a mutated function:
On pypdf, the 64 tests with a
pytest.mark.timeouttake 56.0 / 58.6 s in the stats run on main and 51.5 / 50.2 s with this PR (25.9 s without mutmut).test_add_tm__matrix_multiplications(limit 5 s) goes from 3.6 to 3.0 s.