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

Slow import time #552

Open
LewisGaul opened this issue Feb 13, 2024 · 5 comments
Open

Slow import time #552

LewisGaul opened this issue Feb 13, 2024 · 5 comments

Comments

@LewisGaul
Copy link
Collaborator

The python_on_whales package is a bit slow to import (noticeable for scripts when you just want to show help output):

   > python -V
Python 3.12.1

   > echo 'import python_on_whales' > /tmp/script.py

   > time python3 /tmp/script.py

real    0m0.629s
user    0m0.569s
sys     0m0.057s

   > python -m cProfile /tmp/script.py | head
         1438040 function calls (1220502 primitive calls) in 1.141 seconds

   Ordered by: cumulative time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
       20    0.000    0.000    1.881    0.094 cli_wrapper.py:1(<module>)
       45    0.001    0.000    1.531    0.034 __init__.py:1(<module>)
    468/1    0.017    0.000    1.141    1.141 {built-in method builtins.exec}
        1    0.000    0.000    1.141    1.141 script.py:1(<module>)
   3502/1    0.005    0.000    1.141    1.141 <frozen importlib._bootstrap>:1349(_find_and_load)
@LewisGaul
Copy link
Collaborator Author

Related to #512 since importing these extra dependencies seems to be contributing around 25% of the time.

@gabrieldemarmiesse
Copy link
Owner

Thanks for the bug report, I reproduced it with tuna and -X importtime, and here are the results I found:

There is not much we can do about the pydantic models. We can provide solutions to users:

  1. Users who are making CLI tools or other tools that are very sensitive to import times should use lazy import tools such as https://pypi.org/project/lazy-imports/ . I have used such tools in the past and found them very performant and easy to use.
  2. Mojo is turning out to solve a bunch of issue that are present in python. For example, since it's a compiled language, the import times only impact the compilation speed and not the runtime speed. Once Mojo is mature enough, I would be very happy to make python-on-whales compatible. Users should be able to then make CLI tools that start very fast and are not impacted by import times by using the Mojo compiler.

Since there is not much we can do except fixing #512 and then much later (1+ year) provide a Mojo package, I would advise to close this issue.

@LewisGaul
Copy link
Collaborator Author

LewisGaul commented Sep 5, 2024

I think we should keep this open as import time can be important and I'd say it's currently too high, this is still currently a major pain point for me.

Following #512 (comment) would you be happy with removing the dependencies that account for ~20% of the time or prefer to wait a bit longer?

It might also be worth seeing if we can speed up the pydantic stuff, e.g. see https://docs.pydantic.dev/latest/concepts/performance/.

@gabrieldemarmiesse
Copy link
Owner

I think we can start removing the typer/tqdm dependency. Installing the docker client is documented well enough on the internet that we don't have to do it for our users.

gabrieldemarmiesse pushed a commit that referenced this issue Oct 13, 2024
Removes dependency on typer/tqdm/requests by dropping support for
downloading the docker client.

Initially agreed at
#512 (comment),
this has been deprecated since
#577 (v0.71.0
released in April 2024), and agreed we can now remove at
#552 (comment).
If accepted, this supercedes
#513 as a
resolution to
#512. We
will also be able to close
#575.
@LewisGaul
Copy link
Collaborator Author

The import time has improved slightly (in my testing around 500ms, down from 600ms) since removing the typer/tqdm dependency, and the config change at #643 roughly halves this. However, a 200ms import time is still more than I'd hope for, e.g. for implementing a CLI tool that supports running a command in a running container, where the Podman command itself takes ~100ms. Perhaps we're just dependent on some more Pydantic performance improvements here...

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