gh-156363: Speed up import of rlcompleter by deferring inspect and re - #156364
gh-156363: Speed up import of rlcompleter by deferring inspect and re#156364maxday wants to merge 4 commits into
Conversation
…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.
|
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." |
|
If we decide to do this, we can use the lazy import syntax introduced in https://peps.python.org/pep-0810/ to resolve it. |
…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.
| import __main__ | ||
| import warnings | ||
| import types | ||
|
|
There was a problem hiding this comment.
lazy imports shouldn't be separate.
| 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. |
There was a problem hiding this comment.
| 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.
…pleter Address review feedback
|
Thanks for the review @StanFromIreland I've addressed both of your comments :) |
…lzImp.rst Co-authored-by: Stan Ulbrych <stan@python.org>
95578c1 to
fd0a08f
Compare
|
Hi @maxday, please avoid force push in the future, see https://devguide.python.org/getting-started/pull-request-lifecycle/#don-t-force-push And for the reviewers, I made a comment on the original issue: #156363 (comment):
|
|
Sorry @aisk I didn't know, I won't do it again for sure :) thanks for the link |
Speed up import of rlcompleter by deferring
inspectandreDefer the imports of
inspectandreinrlcompleterinto the methods that actually use them (_callable_postfixandattr_matches).Impact
On a local build, importing
rlcompleterdropped from ~16.4 ms to ~1.8 ms of cumulative import time.Measured with:
Test
A regression test using
test.support.import_helper.ensure_lazy_importsguards thatinspectandreare not imported as a side effect of importingrlcompleter.Fixes: #156363