FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-156363: Speed up import of rlcompleter by deferring inspect and re by maxday · Pull Request #156364 · python/cpython · GitHub

/ cpython Public

gh-156363: Speed up import of rlcompleter by deferring inspect and re - #156364

Open
maxday wants to merge 2 commits into
python:mainfrom
maxday:maxday/speedup-rlcompleter-lazy-imports
Open

gh-156363: Speed up import of rlcompleter by deferring inspect and re#156364
maxday wants to merge 2 commits into
python:mainfrom
maxday:maxday/speedup-rlcompleter-lazy-imports

Conversation

maxday commented Aug 25, 2026
edited by bedevere-app Bot
Loading

Copy link
Copy Markdown
Contributor

Speed up import of rlcompleter by deferring inspect and re
Defer the imports of inspect and re in rlcompleter into the methods that actually use them (_callable_postfix and attr_matches).

Impact
On a local build, importing rlcompleter dropped from ~16.4 ms to ~1.8 ms of cumulative import time.

Measured with:

python -X importtime -c "import rlcompleter"

Test
A regression test using test.support.import_helper.ensure_lazy_imports guards that inspect and re are not imported as a side effect of importing rlcompleter.

Fixes: #156363

…and re

rlcompleter imported inspect and re at module scope, but each is used in
exactly one completion method (inspect in Completer._callable_postfix, re
in Completer.attr_matches).  Neither is needed to construct a Completer or
set up interactive completion, only to compute completions.

inspect in particular is a heavy import (it pulls in dis, tokenize, ...),
so importing rlcompleter dropped from ~16.4ms to ~1.8ms of cumulative
import time on a local build.  Defer both imports into the methods that use
them and add a lazy-import guard test.

maxday commented Aug 25, 2026

Copy link
Copy Markdown
Contributor Author

I believe the macOS failing test can be rerun, looks like it's a network error issue, from the log: "The hosted runner lost communication with the server. Anything in your workflow that terminates the runner process, starves it for CPU/Memory, or blocks its network access can cause this error."

aisk commented Aug 25, 2026
edited
Loading

Copy link
Copy Markdown
Member

If we decide to do this, we can use the lazy import syntax introduced in https://peps.python.org/pep-0810/ to resolve it.

Comment thread Lib/rlcompleter.py Outdated

def _callable_postfix(self, val, word):
if callable(val):
import inspect

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Rather than deferring both these imports, let's use the new lazy keyword and keep them at the top. List the lazy imports after the regular imports.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

done

…pleter

Address review feedback: replace deferred in-function imports of
`inspect` and `re` with `lazy import` statements at the top of the
module, keeping them alongside the regular imports.
maxday requested a review from hugovk August 25, 2026 17:50
Comment thread Lib/rlcompleter.py
import __main__
import warnings
import types

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
Suggested change

lazy imports shouldn't be separate.

Comment on lines +1 to +5
Speed up ``import rlcompleter`` by deferring the imports of :mod:`inspect`
and :mod:`re` into the completion methods that use them. They are only
needed while computing completions, so importing :mod:`rlcompleter` (for
example when setting up interactive completion) no longer pays their import
cost.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
Suggested change
Speed up ``import rlcompleter`` by deferring the imports of :mod:`inspect`
and :mod:`re` into the completion methods that use them. They are only
needed while computing completions, so importing :mod:`rlcompleter` (for
example when setting up interactive completion) no longer pays their import
cost.
Speed up the :mod:`rlcompleter` module's import time.

The rest is implementation details.

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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.

Speed up import rlcompleter by deferring inspect and re

4 participants


Back | FazBrowse Home | New Git URL