parser: import subcommands lazily#119
Open
Qbushey wants to merge 1 commit into
Open
Conversation
Subcommands are now referenced as 'module:class' strings and imported only when actually run, and get_wallpaper() is no longer invoked during argument parsing. This avoids paying the PIL/materialyoucolor import cost on every invocation: 'caelestia -h' mean time drops from ~111ms to ~64ms, which directly improves the feel of keybound commands. Closes caelestia-dots#75
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.
Inspired by #75?
Currently every invocation of the CLI imports all nine subcommand modules at argument-parser construction time, which pulls in PIL and materialyoucolor (via the wallpaper subcommand) even for commands that never touch them. The parser also calls
get_wallpaper()while building the wallpaper subparser, so that runs on every invocation too.This PR:
"module:class"strings in the parser and resolves them withimportlibinmain()only for the subcommand actually being runget_wallpaper()call forwallpaper -pfrom parse time to run time (-pwithout a path still means the current wallpaper, andwallpaper -pwith no wallpaper set still printsNo wallpaper set)Measured with
python -m caelestia -h(10 runs after warm-up, Python 3.14, Arch):Commands that do use the heavy imports (e.g.
wallpaper,scheme) pay the same cost as before, just at run time. Verified that all subcommand references resolve,scheme list/scheme get/wallpaper/wallpaper -pbehave identically, and there are no behaviour changes to argument parsing (-houtput is unchanged).