Skip to content

gh-156774: Speed up pdb startup with asyncio guard - #156775

Open
brittanyrey wants to merge 3 commits into
python:mainfrom
brittanyrey:b-perf-pdb-lazy-imports
Open

gh-156774: Speed up pdb startup with asyncio guard#156775
brittanyrey wants to merge 3 commits into
python:mainfrom
brittanyrey:b-perf-pdb-lazy-imports

Conversation

@brittanyrey

@brittanyrey brittanyrey commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

summary

  • Guard expensive asyncio import on if the current program is using asyncio at all.

perf

Broken out impact of lazy imports + asyncio guard
Without it, only the initial import really improves:

Benchmark asyncio guard
import pdb -28.8%
breakpoint() → continue -28.5%
python -m pdb script -26.0%
pdb post-mortem -24.6%

Defer the eight imports that are not already pulled in by pdb's
required-eager imports (cmd, bdb, code, typing and the guarded
_pyrepl.utils), and lift five unguarded function-level imports to
module-scope lazy imports.

Guard Pdb._get_asyncio_task() on 'asyncio' in sys.modules. It runs on every
interaction(), so without the guard the deferred asyncio import is paid back
immediately on the first breakpoint and the lazy import buys nothing for an
actual debugging session. If asyncio was never imported there cannot be a
running task, so returning None is equivalent.

The guarded imports (readline, rlcompleter, _pyrepl) stay function-level:
they sit in try/except ImportError blocks, and hoisting them would move the
failure outside the guard.

import pdb              -42.1%
breakpoint() -> cont    -41.1%
python -m pdb script    -33.4%
pdb post-mortem         -33.3%
@hugovk

hugovk commented Sep 1, 2026

Copy link
Copy Markdown
Member

See #156567 for the lazy imports, your review welcome!

I was planning on doing the asyncio guard as a followup :) Shall we reduce this PR to just that?

@brittanyrey

Copy link
Copy Markdown
Contributor Author

Sorry about that! I did a peek over the lazy imports label & missed your issue!

I'm glad to scope this down to just the asyncio gated change.

@brittanyrey brittanyrey changed the title gh-156774: Speed up pdb startup with lazy imports + asyncio guard gh-156774: Speed up pdb startup with asyncio guard Sep 1, 2026
Drop the other lazy imports and the lifted function-level imports, leaving
just the asyncio deferral and the _get_asyncio_task() guard. Those two carry
most of the win and keep the diff to five lines.

import pdb              -28.8% (was -42.1% with the full set)
breakpoint() -> cont    -28.5%
python -m pdb script    -26.0%
pdb post-mortem         -24.6%
@brittanyrey

Copy link
Copy Markdown
Contributor Author

PR + issue are fully rescoped @hugovk

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants