Skip to content

perf: defer command imports during startup#3805

Merged
frostming merged 1 commit into
pdm-project:mainfrom
Yijian6:defer-command-imports
Jun 12, 2026
Merged

perf: defer command imports during startup#3805
frostming merged 1 commit into
pdm-project:mainfrom
Yijian6:defer-command-imports

Conversation

@Yijian6

@Yijian6 Yijian6 commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

What

Defer imports used only by the self, python, and use command 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 --version with the same virtualenv showed:

  • main: min 573.50 ms, avg 828.56 ms, max 2294.72 ms
  • this branch: min 550.61 ms, avg 572.28 ms, max 613.83 ms

Testing

  • .\.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, and use --help -> passed

I 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 under C:\Users\觉\... is decoded as UTF-8 while the child process emits the username in the system code page.

Breaking changes

None.

@yihong0618

Copy link
Copy Markdown
Contributor

I think we can wait for 3.15 use lazy import later?

@frostming

frostming commented Jun 11, 2026

Copy link
Copy Markdown
Collaborator

I think we can wait for 3.15 use lazy import later?

that's a long way to go. and i guess we don't have a path to backport, do we?

@yihong0618

Copy link
Copy Markdown
Contributor

I think we can wait for 3.15 use lazy import later?

that's a long way to go. and i guess we don't have a path to backport, do we?

make sense

@codecov

codecov Bot commented Jun 12, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 73.68421% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.27%. Comparing base (935b256) to head (78b5f52).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
src/pdm/cli/commands/self_cmd.py 50.00% 5 Missing ⚠️
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              
Flag Coverage Δ
unittests 86.15% <73.68%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@frostming frostming merged commit e02de21 into pdm-project:main Jun 12, 2026
22 of 23 checks passed
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.

3 participants