Skip to content

perf: defer the installer imports in poetry.plugins.plugin_manager - #11096

Open
kuldeeepy wants to merge 1 commit into
python-poetry:mainfrom
kuldeeepy:perf/defer-installer-imports-in-plugin-manager
Open

kuldeeepy wants to merge 1 commit into
python-poetry:mainfrom
kuldeeepy:perf/defer-installer-imports-in-plugin-manager

Conversation

@kuldeeepy

Copy link
Copy Markdown

Pull Request Check List

  • Added tests for changed code.
  • Updated documentation for changed code.

Summary

Application._load_plugins imports poetry.plugins.plugin_manager on every command (unless --no-plugins). That module imports Installer, Locker, InstalledRepository, Env and EnvManager at module level, but they are only needed to install project plugins. This moves them into the functions that use them, next to the existing local Factory imports, and Env goes under TYPE_CHECKING.

Numbers

Importing poetry.plugins.plugin_manager in a fresh interpreter no longer loads poetry.installation: 846 modules in sys.modules before, 245 after. CPU time of the import went from about 640 ms to 140 ms (best of 20), measured on a busy machine, so the absolute numbers are high but the ratio held across runs.

Tests

Added a test that importing the plugin manager does not load poetry.installation, like the one added in #11004. The mocks that patched Installer through plugin_manager now patch poetry.installation.installer.Installer._execute, as other tests do. tests/plugins, mypy and pre-commit pass.

In #11004 you preferred moving code into its own module over function-level imports. If you'd rather have ProjectPluginCache moved into a separate module with global imports, I'm happy to do that instead.

This PR was written with the help of Claude Code. I reviewed and tested all changes before submitting.

The plugin manager is imported for every command, but it imports the
installer stack at module level. Installer, Locker, InstalledRepository
and EnvManager are only needed to install project plugins, so they are
imported where they are used.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've reviewed your changes and they look great!

Sourcery assessment

Approved.


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

@kuldeeepy

Copy link
Copy Markdown
Author

The two red jobs look unrelated to this change: test_call_does_not_block_on_full_pipe on Windows (also seen in #10916 and #10769) and test_git_clone_revision_is_branch on macOS, which clones over the network. Both pass locally for me.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant