Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
gh-156774: Scope pdb change down to the asyncio import only
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%
  • Loading branch information
brittanyrey committed Sep 1, 2026
commit 89fccc3c5ec5047a0a9f7d1aa16883cb7958c5ff
25 changes: 14 additions & 11 deletions Lib/pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,32 +73,28 @@
import bdb
import dis
import code
lazy import glob
lazy import json
import glob
import json
import stat
lazy import runpy
lazy import shlex
lazy import pydoc
import token
import types
import atexit
import codeop
lazy import pprint
lazy import signal
lazy import socket
import pprint
import signal
import socket
import typing
lazy import asyncio
Comment thread
brittanyrey marked this conversation as resolved.
Outdated
import inspect
import weakref
lazy import argparse
import builtins
lazy import tempfile
import tempfile
import textwrap
import tokenize
import itertools
import traceback
import linecache
lazy import selectors
import selectors
import threading
import _colorize

Expand Down Expand Up @@ -250,6 +246,7 @@ class _ModuleTarget(_ExecutableTarget):
def __init__(self, target):
self._target = target

import runpy
try:
_, self._spec, self._code = runpy._get_module_details(self._target)
except ImportError as e:
Expand Down Expand Up @@ -284,6 +281,8 @@ def namespace(self):

class _ZipTarget(_ExecutableTarget):
def __init__(self, target):
import runpy

self._target = os.path.realpath(target)
sys.path.insert(0, self._target)
try:
Expand Down Expand Up @@ -2089,6 +2088,7 @@ def do_run(self, arg):
'e.g. "python -m pdb myscript.py"')
return
if arg:
import shlex
argv0 = sys.argv[0:1]
try:
sys.argv = shlex.split(arg)
Expand Down Expand Up @@ -3743,6 +3743,7 @@ def test():

# print help
def help():
import pydoc
pydoc.pager(__doc__)

_usage = """\
Expand Down Expand Up @@ -3783,6 +3784,8 @@ def parse_args():
# "python -m pdb -m foo -m bar" should pass "-m bar" to "foo".
# This require some customized parsing logic to find the actual debug target.

import argparse

parser = argparse.ArgumentParser(
usage="%(prog)s [-h] [-c command] (-m module | -p pid | pyfile) [args ...]",
description=_usage,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Speed up :mod:`pdb` startup by about 40% using :keyword:`lazy import`, and by
skipping the :mod:`asyncio` import when no event loop has been used.
Speed up :mod:`pdb` startup by deferring the :mod:`asyncio` import, and by
skipping it entirely in ``Pdb._get_asyncio_task()`` when no event loop has
been used.
Comment thread
brittanyrey marked this conversation as resolved.
Outdated
Loading