Replies: 6 comments 11 replies
-
Most of typer's startup time (>85%) comes from loading rich modules in
This means that these time-consuming import are not needed "normal" execution flows (no help, no errors). If you're not using rich in your CLI, you can avoid this delay by using My app is using rich, but I would still want to avoid that delay, mainly because of autocompletion. The 400 ms delay after pressing the tab key is quite noticeable. In my own code, I can defer loading rich (and other slow dependencies) until it's actually needed, so that it doesn't slow down autocompletion. However, I cannot avoid the import of Would you be interested in a PR that changes |
Beta Was this translation helpful? Give feedback.
-
I'd second this - I also ran into the same thing, where I'd a simple cli app that had annoying latency in the startup time: using Deferring the import unless its actually needed seems like it'd be a good idea (or failing that, some way to flag that I don't want it to be used even if installed) |
Beta Was this translation helpful? Give feedback.
-
Different library, but this is something we noticed in More info: https://ewels.github.io/rich-click/blog/2024/04/30/version-1.8/ |
Beta Was this translation helpful? Give feedback.
-
Chiming in with some data. Latency is embarrassing for a CLI tool. Yes, this is mostly a Python problem, not a Typer problem, but it would be nice to improve. There is a long history of lazy-loading PEPs, take a look if you are patient and brave 🤣. Here's my app: import typer
app = typer.Typer()
@app.command()
def main(name: str):
print(f"Hello {name}") My shell reports ~400ms to get any output from this application 🤨 Of course it's the imports. Run:
See that typer uses the vast majority of the import time - the cumulative 1,245,672uS:
I am working on portable python packaging and distribution 💀 and hacked lazy imports would break the dependency analysis. Which is why there are a bunch of PEPs on the topic. After surveying themm and considering that the WILL to improve this has existed for like 15 years, I am not confident that import performance of python will ever improve. |
Beta Was this translation helpful? Give feedback.
-
Please review the upstream changes to Rich here: Textualize/rich#3399 I am seeing a ~100ms improvement of the Rich import, making it about twice as fast. |
Beta Was this translation helpful? Give feedback.
-
So, just wanted to follow up on this discussion since this is something we're now noticing. Out of curiosity, I benchmarked import times for the same typer app @JPHutchins made but with their fixes to rich applied (which are merged but not released) and kept going to see if I could cut things down further in the "fast path" (by that I mean you're just invoking the CLI with no special rich console printing, etc.). Our test app is:
We'll start with We see indeed that the So, I tried to make the Turns out it's not quite as simple to get rid of Finally, I noticed in So after applying all these changes: That's a ~35% import speedup on top of @JPHutchins's awesome changes to rich. So compared to currently released Of course, if you actually use rich functionality, etc. you'll pay those import costs. That's fine. The goal here is to cut the import time in the general case wherever possible. Will be submitting a PR if @tiangolo is OK with it. |
Beta Was this translation helpful? Give feedback.
-
First Check
Commit to Help
Example Code
Description
Is there any way to increase performance of using Typer? For example, I could imagine there are some flags one can disable?
I'm running Typer on a Raspberry Pi 3, which is - of course - not as fast as my MacBook.
Stats
$ time python test.py hello real 0m2.071s user 0m1.936s sys 0m0.112s
For my own, larger but not excessive, CLI
$ time python cli.py --help real 0m5.411s user 0m4.656s sys 0m0.301s
I saw in other issues I could perhaps make my imports dynamic to shave off some time, but perhaps there are some Typer-related features I can leverage.
Operating System
Linux
Operating System Details
Raspberry Pi 3
Typer Version
0.9.0
Python Version
3.9.2
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions