perf: lazy-import vscode_parser and vscode_report in CLI (#890)#891
Merged
perf: lazy-import vscode_parser and vscode_report in CLI (#890)#891
Conversation
Move the vscode_parser and vscode_report imports from module level into the vscode() command function so they are only loaded when that subcommand is actually invoked. This avoids loading re, stat, types, and executing re.compile() on every CLI invocation. Update two existing tests that patched the imports at cli module level to patch at the source module instead. Add a regression test verifying the lazy-import invariant. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR reduces CLI startup overhead by deferring VS Code-specific imports until the vscode subcommand is invoked, avoiding importing vscode_parser/vscode_report (and their heavier dependencies) for other subcommands.
Changes:
- Move
vscode_parser/vscode_reportimports fromcopilot_usage.climodule scope into thevscode()command function. - Update tests to patch
copilot_usage.vscode_parser.get_vscode_summary(since the CLI import is now local). - Add a packaging regression test asserting
copilot_usage.cliimport does not load VS Code modules.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/copilot_usage/cli.py | Lazily imports VS Code parser/report only inside the vscode subcommand. |
| tests/copilot_usage/test_vscode_parser.py | Updates mocking target to align with the new lazy import location. |
| tests/test_packaging.py | Adds regression test to ensure cli module import doesn’t import VS Code modules. |
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
|
Commit pushed:
|
Contributor
There was a problem hiding this comment.
Quality Gate: APPROVE
Low-impact performance optimization with good test coverage. Auto-approving for merge.
Evaluated:
cli.py: Moves 2 vscode imports from module-level to insidevscode()function — correct lazy-import patterntest_vscode_parser.py: Patch targets updated fromcopilot_usage.cli.get_vscode_summarytocopilot_usage.vscode_parser.get_vscode_summary— correct for local importstest_packaging.py: New regression test verifies vscode modules stay out ofsys.modulesafter importing cli — well-structured with proper cleanup
Impact: LOW — No API/model changes, no new dependencies, behavior unchanged. All 8 CI checks pass.
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.
Summary
Moves the
vscode_parserandvscode_reportimports from module level into thevscode()command function so they are only loaded when that subcommand is actually invoked.Changes
src/copilot_usage/cli.py: Removed module-level imports ofget_vscode_summaryandrender_vscode_summary; added local imports inside thevscode()function.tests/copilot_usage/test_vscode_parser.py: Updated two tests that patchedcopilot_usage.cli.get_vscode_summaryto patchcopilot_usage.vscode_parser.get_vscode_summaryinstead, since the import is now local.tests/test_packaging.py: Addedtest_cli_does_not_import_vscode_modules_at_module_levelregression test that verifies neithercopilot_usage.vscode_parsernorcopilot_usage.vscode_reportappear insys.modulesafter a clean import ofcopilot_usage.cli.Impact
Removes
re,stat,types, and there.compile(...)call from the startup path of all non-vscode subcommands (summary,session,cost,live, interactive mode). Estimated saving: ~3-5 ms per cold invocation.Closes #890