Skip to content

Speed up trampolines by reading os.environ once per call - #656

Merged
boxed merged 1 commit into
boxed:mainfrom
MartinThoma:speed-up-stats-trampoline
Nov 30, 2026
Merged

boxed merged 1 commit into
boxed:mainfrom
MartinThoma:speed-up-stats-trampoline

Conversation

@MartinThoma

@MartinThoma MartinThoma commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

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.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 #651 gets pypdf past the import, the stats run on pypdf fails because of it:
pypdfs 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.

The Solution

Although os.environ feels like a normal dict, it is not. This can be seen with a benchmark:

image

Other improvements:

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

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>
@boxed
boxed merged commit 15e84f4 into boxed:main Nov 30, 2026
13 checks passed
@boxed

boxed commented Nov 30, 2026

Copy link
Copy Markdown
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...

@MartinThoma

Copy link
Copy Markdown
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>
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