Skip to content

parser: import subcommands lazily#119

Open
Qbushey wants to merge 1 commit into
caelestia-dots:mainfrom
Qbushey:parser-lazy-imports
Open

parser: import subcommands lazily#119
Qbushey wants to merge 1 commit into
caelestia-dots:mainfrom
Qbushey:parser-lazy-imports

Conversation

@Qbushey

@Qbushey Qbushey commented Jun 11, 2026

Copy link
Copy Markdown

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:

  • references subcommands as "module:class" strings in the parser and resolves them with importlib in main() only for the subcommand actually being run
  • defers the get_wallpaper() call for wallpaper -p from parse time to run time (-p without a path still means the current wallpaper, and wallpaper -p with no wallpaper set still prints No wallpaper set)

Measured with python -m caelestia -h (10 runs after warm-up, Python 3.14, Arch):

min mean
main 95.1 ms 110.8 ms
this PR 58.1 ms 64.3 ms

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 -p behave identically, and there are no behaviour changes to argument parsing (-h output is unchanged).

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
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