You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Speed up import rlcompleter by deferring inspect and re #156363
rlcompleter imports inspect and re at module scope, but neither is needed to import the module or to construct a Completer. Each is used in exactly one completion method:
inspect: only in Completer._callable_postfix
re: only in Completer.attr_matches
inspect in particular is a heavy import: it transitively pulls in dis, tokenize, and more. Because rlcompleter is imported during interactive startup (to set up tab completion via readline), this import cost is paid on every interactive session and every import rlcompleter, even though the completion methods may never run.
You're right @aisk, on the default PyREPL path _pyrepl.readline pulls in both re (directly) and inspect (via dataclasses(slots=True) in _pyrepl/utils.py), so the deferral is neutral there.
It does still help in two cases: PYTHON_BASIC_REPL=1, where site.py skips the _pyrepl.readline import entirely, and standalone use of rlcompleter (e.g. readline.set_completer(rlcompleter.Completer(ns).complete)), where importing the module drops from ~15 ms to ~1.6 ms and avoids ~35 transitive modules.
It also helps people who use this module on its own indeed.
Since it's neutral on the common path and a win on the others and in line with the ongoing startup-time / PEP 810 work, I think it's still worth doing, let me know!
Activity
maxday commentedon Aug 25, 2026
I'm working on this :)
pythongh-156363: Speed up import of rlcompleter by deferring inspect …
pythongh-156363: Speed up import of rlcompleter by deferring inspect …
pythongh-156363: Use PEP 810 lazy imports for inspect and re in rlcom…
pythongh-156363: Use PEP 810 lazy imports for inspect and re in rlcom…
aisk commentedon Aug 26, 2026
One thing to notice, on the interactive startup path these two modules get imported a moment later anyway, since
_pyrepl.readlineis imported right afterrlcompleterand pulls them both in:$ ./python -c "import sys, _pyrepl.readline; print('re' in sys.modules, 'inspect' in sys.modules)" True TrueSo the gain doesn't really materialize for the interactive interpreter. It may still help people who use this module on its own, though.
maxday commentedon Aug 28, 2026
You're right @aisk, on the default PyREPL path
_pyrepl.readlinepulls in bothre(directly) andinspect(viadataclasses(slots=True)in_pyrepl/utils.py), so the deferral is neutral there.It does still help in two cases:
PYTHON_BASIC_REPL=1, wheresite.pyskips the_pyrepl.readlineimport entirely, and standalone use ofrlcompleter(e.g.readline.set_completer(rlcompleter.Completer(ns).complete)), where importing the module drops from ~15 ms to ~1.6 ms and avoids ~35 transitive modules.It also helps people who use this module on its own indeed.
Since it's neutral on the common path and a win on the others and in line with the ongoing startup-time / PEP 810 work, I think it's still worth doing, let me know!
pythongh-156363, pythongh-156491: Speed up rlcompleter import and opt…