Repository navigation
Speed up import rlcompleter by deferring inspect and re #156363
Description
Activity
I'm working on this :)
- addedperformancePerformance or resource usagePerformance or resource usagestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
on Aug 25, 2026 - added a parent issue
on Aug 25, 2026 - addedtype-featureA feature request or enhancementA feature request or enhancement
on 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.
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!
rlcompleterimportsinspectandreat module scope, but neither is needed to import the module or to construct aCompleter. Each is used in exactly one completion method:inspect: only inCompleter._callable_postfixre: only inCompleter.attr_matchesinspectin particular is a heavy import: it transitively pulls indis,tokenize, and more. Becauserlcompleteris imported during interactive startup (to set up tab completion viareadline), this import cost is paid on every interactive session and everyimport rlcompleter, even though the completion methods may never run.Linked PRs