perf: defer command imports during startup#3805
Merged
Merged
Conversation
Contributor
|
I think we can wait for 3.15 use lazy import later? |
Collaborator
that's a long way to go. and i guess we don't have a path to backport, do we? |
Contributor
make sense |
frostming
approved these changes
Jun 12, 2026
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #3805 +/- ##
==========================================
- Coverage 86.29% 86.27% -0.02%
==========================================
Files 118 118
Lines 12536 12541 +5
Branches 2093 2093
==========================================
+ Hits 10818 10820 +2
- Misses 1147 1150 +3
Partials 571 571
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Defer imports used only by the
self,python, andusecommand execution paths so they are not loaded while PDM is only starting up and registering CLI commands.Why
Refs #3673. PDM currently imports every command module during parser initialization, so top-level imports in rarely used commands contribute to startup cost even for simple invocations such as
pdm --version.How
Move execution-only imports for package working sets, Python discovery/install helpers, environment helpers, and update checks into the functions or handlers that actually need them. The command registration and argument definitions are unchanged.
A local comparison using 7 runs of
python -m pdm --versionwith the same virtualenv showed:main: min 573.50 ms, avg 828.56 ms, max 2294.72 msTesting
.\.venv\Scripts\python.exe -m pytest tests\cli\test_self_command.py -q-> 8 passed.\.venv\Scripts\ruff.exe check src\pdm\cli\commands\self_cmd.py src\pdm\cli\commands\python.py src\pdm\cli\commands\use.py tests\cli\test_self_command.py-> passed.\.venv\Scripts\python.exe -m compileall src\pdm\cli\commands\self_cmd.py src\pdm\cli\commands\python.py src\pdm\cli\commands\use.py tests\cli\test_self_command.py-> passed.\.venv\Scripts\pdm.exe --version,self --help,python --help, anduse --help-> passedI also tried
tests\cli\test_python.py tests\cli\test_use.py; on my Windows machine they fail before exercising this change because subprocess output from the local Python path underC:\Users\觉\...is decoded as UTF-8 while the child process emits the username in the system code page.Breaking changes
None.