feat: speedup import time using sys.platform #3399
Merged
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.
Type of changes
Checklist
Description
We're investigating slow CLIs over at Typer: fastapi/typer#744. More generally, I am interested in improving Python import performance.
This PR cuts rich import time from ~230ms -> ~130ms on my machine. Here's the test process.
Create a simple test script that mimics the dependencies used by Typer:
Refer to:
https://github.com/tiangolo/typer/blob/04eba6b70203287176d2823753513226bf778872/typer/rich_utils.py#L11-L22
Run 1x to compile, then test times.
My shell records execution times ranging from 282ms -> 380ms (let's call it 300ms).
Check import times
rich.color
stands out a bit!Results after this PR - run at least 1x to compile.
Centering around 200ms. Considering that the shell records 75ms with NOTHING in test.py,
that's around a 2x import time speedup.
Check import times
rich.color goes from ~100ms -> ~1.8ms
I suspect that that reason is that
sys.platform
can be evaluated at compile time,allowing for further optimizations down the line, where as the runtime
platform.system()
call in module scope was preventing this.Cheers,
JP