Skip to content

⚡ perf(plugin): defer dotenv loading - #341

Merged
gaborbernat merged 1 commit into
mainfrom
perf/lazy-imports
Nov 19, 2026
Merged

gaborbernat merged 1 commit into
mainfrom
perf/lazy-imports

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

Loading pytest-env should not load the .env parser for sessions that use only inline environment settings. The paired benchmark measured about 0.3 MiB less peak memory when importing the plugin on Python 3.14 and 3.15. Wall-time results varied under host load, so they do not establish a startup speedup.

The plugin now loads python-dotenv when it reads an environment file. On Python 3.15, a lazy module declaration also delays tomllib until TOML configuration needs it. The package root remains eager: deferring its version import showed no measurable gain. Lifeguard and flake8-lazy informed the import review.

Benchmarks

Each block is one paired 60-run hyperfine comparison with --warmup 5 --metrics time_wall_clock:ms,memory_peak_resident:MiB. Both commands used the same tox interpreter and dependencies, with PYTHONPATH selecting the merge-base or PR source tree. The Python 3.14 plugin run used the reverse command order to check the apparent slowdown in an earlier run.

Python 3.15

Direct plugin import (import pytest_env.plugin)

Benchmark 1: Before (60 runs)
                mean     ±       σ          min     …     max
  Wall Time     86.6 ms  ±    12.0 ms      72.8 ms  …   102.3 ms
  Memory        31.6 MiB ±     0.1 MiB     31.6 MiB …    31.9 MiB
 
Benchmark 2: After (60 runs)
                mean     ±       σ          min     …     max
  Wall Time     85.5 ms  ±    12.5 ms      70.0 ms  …   102.8 ms            -1.2%
  Memory        31.3 MiB ±     0.0 MiB     31.2 MiB …    31.5 MiB           -1.2%

pytest --help

Benchmark 1: Before (60 runs)
                mean     ±       σ          min     …     max
  Wall Time    141.2 ms  ±    21.9 ms     114.7 ms  …   168.0 ms
  Memory        39.2 MiB ±     0.1 MiB     39.0 MiB …    39.5 MiB
 
Benchmark 2: After (60 runs)
                mean     ±       σ          min     …     max
  Wall Time    130.5 ms  ±    18.0 ms     113.3 ms  …   167.3 ms            -7.6%
  Memory        39.2 MiB ±     0.1 MiB     39.0 MiB …    39.4 MiB            0.0%

Python 3.14

Direct plugin import (import pytest_env.plugin)

Benchmark 1: After (60 runs)
                mean     ±       σ          min     …     max
  Wall Time     69.4 ms  ±     1.2 ms      67.4 ms  …    73.1 ms
  Memory        30.6 MiB ±     0.1 MiB     30.4 MiB …    30.7 MiB
 
Benchmark 2: Before (60 runs)
                mean     ±       σ          min     …     max
  Wall Time     77.5 ms  ±    10.1 ms      69.2 ms  …   100.2 ms           +11.6%
  Memory        30.9 MiB ±     0.1 MiB     30.6 MiB …    31.0 MiB           +0.8%

pytest --help

Benchmark 1: Before (60 runs)
                mean     ±       σ          min     …     max
  Wall Time    131.9 ms  ±    29.6 ms     103.1 ms  …   255.2 ms
  Memory        37.0 MiB ±     0.2 MiB     36.8 MiB …    37.3 MiB
 
Benchmark 2: After (60 runs)
                mean     ±       σ          min     …     max
  Wall Time    105.4 ms  ±     7.8 ms     101.7 ms  …   147.4 ms           -20.1%
  Memory        36.8 MiB ±     0.1 MiB     36.6 MiB …    37.1 MiB           -0.6%

Import profile

The Python 3.15 diagrams come from python -X importtime -c 'import pytest_env.plugin' and tuna. The before profile includes dotenv; the after profile does not. Most of the tree is pytest, which the plugin still needs at import time.

Before

Before plugin import tree

After

After plugin import tree

@gaborbernat gaborbernat added the enhancement New feature or request label Nov 18, 2026
@gaborbernat
gaborbernat marked this pull request as ready for review November 18, 2026 22:28
@gaborbernat
gaborbernat merged commit 7b545e6 into main Nov 19, 2026
19 checks passed
@gaborbernat
gaborbernat deleted the perf/lazy-imports branch November 19, 2026 06:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant