Defer pygments and pdb imports (9% speedup) - #14874
Conversation
|
|
||
|
|
||
| def pytest_configure(config: Config) -> None: | ||
| import pdb |
There was a problem hiding this comment.
Can't we just import pdb in a conditional branch ? The new class feel unnecessary.
There was a problem hiding this comment.
Unfortunately no. With a pure import the following tests fail:
FAILED test_pdb_set_trace_interception
FAILED TestDebuggingBreakpoints::test_sys_breakpoint_interception
FAILED test_pdbcls_via_local_module
FAILED test_pdb_wrapper_class_is_reused
FAILED test_quit_with_swallowed_SystemExit
test_pdb_set_trace_interception runs pytest with no options at all and expects import pdb; pdb.set_trace() inside a test to be intercepted. Checking "pdb" in sys.modules at configure time doesn't work either, because test modules are imported during collection, which happens after pytest_configure.
So the only thing left to hook is the import itself, and a sys.meta_path entry has to be an object exposing find_spec.
|
The pygments change LGTM, but maybe @nicoddemus should ACK. The pdb seems to add some complexity. It seems to me like it would perhaps be better to improve this in cpython itself, maybe the imports can be changed to lazy there? I see cpython has an issue Improve import time of various stdlib modules so at least they seem open to it. |
nicoddemus
left a comment
There was a problem hiding this comment.
I also think the pygments changes are OK, but I agree with @bluetech that the pdb changes are a bit too complex to our liking. After all, we will need to maintain that code moving forward, and who knows what bugs might happen because of it?
I'm -0 on accepting this, while 9% speed up is not negligible, the necessary code to achieve that is not ideal (and this is not saying the code per-se is bad, seems like it is the only solution possible here).
| from .wcwidth import wcswidth | ||
|
|
||
|
|
||
| if TYPE_CHECKING: |
There was a problem hiding this comment.
We could leave a note at the global level mentioning that the local imports for pygments are optional, and related to a speed up.
|
@nicoddemus sure, I see where you're coming from. I would probably reject the pdb complexity as well. Happy to remove it and just ship the pygments change if you think it's worth it. That alone should be around a 5% speedup. |
|
Thanks. Lets give it a day for others to weigh in but if nobody manifests otherwise, go ahead with just keeping the pygments changes. Btw, how much speedup do the pygments changes (in isolation) provide? |
Keep this PR to pygments only. The pdb part needs a sys.meta_path finder, which deserves its own discussion. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
|
Done. The speedup is very modest, so not sure if it's worth it. |
Hello,
pytest currently imports
pygmentsandpdbon every run, even though they are only needed in specific cases:pygmentsis only used when there is a failure, for the summary shown at the end, and only when terminal colors are enabled.pdbis 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, andselectorsmodules, which together bring in roughly 40 additional modules.Using this patched pytest version against the psutil test suite (839 tests) with
--plugins-disabledand--collect-only, I see about a 9% speedup:bench_real.py