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:
from rich import box
from rich.align import Align
from rich.columns import Columns
from rich.console import Console, RenderableType, group
from rich.emoji import Emoji
from rich.highlighter import RegexHighlighter
from rich.markdown import Markdown
from rich.padding import Padding
from rich.panel import Panel
from rich.table import Table
from rich.text import Text
from rich.theme import Theme
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.
Type of changes
Checklist
Description
We're investigating slow CLIs over at Typer: https://github.com/tiangolo/typer/discussions/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 runtimeplatform.system()
call in module scope was preventing this.Cheers, JP