Skip to content

Keep the active mutant in a module global - #661

Merged
boxed merged 1 commit into
boxed:mainfrom
MartinThoma:keep-mutant-in-global
Dec 2, 2026
Merged

boxed merged 1 commit into
boxed:mainfrom
MartinThoma:keep-mutant-in-global

Conversation

@MartinThoma

Copy link
Copy Markdown
Contributor

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.environ as they are roughly 15x as expensive as a x in dict call:

$ python --version
Python 3.15.2

$ python -m timeit -s "import os; d = dict(os.environ); k = 'PATH'" "k in d"
10000000 loops, best of 5: 38.9 nsec per loop

$ python -m timeit -s "import os; k = 'PATH'" "k in os.environ"
500000 loops, best of 5: 616 nsec per loop

Even after #656 the trampoline still calls get_mutant_under_test() on every call of a mutated function. That reads os.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

  • 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().

Changed Behavior

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.

Benchmark

Per call of a mutated function:

main 15e84f4 this PR
no mutant active 1,332 ns 778 ns
stats run 2,746 ns 1,855 ns

On pypdf, the 64 tests with a pytest.mark.timeout take 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.

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>
@MartinThoma

MartinThoma commented Dec 1, 2026 •

Copy link
Copy Markdown
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 :-)
Also, if projects meddle with os.environ this PR might also make things better.

@boxed
boxed merged commit 3060f31 into boxed:main Dec 2, 2026
13 checks passed
@boxed

boxed commented Dec 2, 2026

Copy link
Copy Markdown
Owner

We'll just have to try. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants