Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid top-level imports in cli.py #848

Open
AdrianVollmer opened this issue Mar 23, 2025 · 3 comments
Open

Avoid top-level imports in cli.py #848

AdrianVollmer opened this issue Mar 23, 2025 · 3 comments

Comments

@AdrianVollmer
Copy link

AdrianVollmer commented Mar 23, 2025

Outputing the help takes about one second on my system:

$ time llm --help > /dev/null
llm --help > /dev/null  0.82s user 0.10s system 99% cpu 0.920 total

That's because cli.py is importing a lot of libraries at the top-level. I propose moving the imports in that file to the functions where they are needed. It may not look asthetic, but it will speed up the call to --help by many orders of magnitudes, and probably somewhat speed up the warm up time when calling subcommands.

Let me know if I can help out with a PR. (It's probably also something that an LLM would be good at.)

Edit: I'm seeing that __init__.py is also affected, and my suggestions probably doesn't apply there ...

@simonw
Copy link
Owner

simonw commented Mar 28, 2025

I'd be interested in a benchmark that shows how much time we could save here.

AdrianVollmer pushed a commit to AdrianVollmer/llm that referenced this issue Mar 28, 2025
For this, it is necessary to split up the package into two:
one "library" part called `llm` and one "CLI" part called `lmm_cli`.
The `click` interface is defined only in `llm_cli` and loads
dependencies from `llm` or elsewhere only when necessary.

This speeds up running `llm --help` by an order of magnitude:

```
$ time llm --help > /dev/null
llm --help > /dev/null  0.80s user 0.10s system 99% cpu 0.906 total
 # After
$ time .venv/bin/llm --help > /dev/null
.venv/bin/llm --help > /dev/null  0.07s user 0.01s system 99% cpu 0.078 total
```

See simonw#848.
@AdrianVollmer
Copy link
Author

AdrianVollmer commented Mar 28, 2025

What a coincidence, I went ahead and implemented it and just pushed my proposed changes. I had to split up the package into two, though. Still need to run the test suite. See the commit message for a basic benchmark. I can create a benchmark for the other subcommands, but I expect the difference to be mostly negligible.

@AdrianVollmer
Copy link
Author

Yeah, unfortunately this won't work. The plugin mechanism is too intertwined with the CLI.

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

No branches or pull requests

2 participants