Skip to content

Defer pygments and pdb imports (9% speedup) - #14874

Open
giampaolo wants to merge 1 commit into
pytest-dev:mainfrom
giampaolo:speedup
Open

Defer pygments and pdb imports (9% speedup)#14874
giampaolo wants to merge 1 commit into
pytest-dev:mainfrom
giampaolo:speedup

Conversation

@giampaolo

Copy link
Copy Markdown

Hello,
pytest currently imports pygments and pdb on every run, even though they are only needed in specific cases:

  • pygments is only used when there is a failure, for the summary shown at the end, and only when terminal colors are enabled.
  • pdb is only used with the --pdb CLI option, and only if a test fails.

Note: pdb became noticeably more expensive to import in Python 3.14, as it now pulls in socket, asyncio, and selectors modules, which together bring in roughly 40 additional modules.

Using this patched pytest version against the psutil test suite (839 tests) with --plugins-disabled and --collect-only, I see about a 9% speedup:

~/svn/pytest {speedup}$ python3 bench_real.py
upstream/main   378.8ms
working tree    343.8ms
-> +35.0ms (+9.2%)

bench_real.py

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.

1 participant