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

perf: speed up SetColors by ~15-25% #879

Merged
merged 1 commit into from
May 3, 2024

Conversation

joshka
Copy link
Collaborator

@joshka joshka commented Apr 27, 2024

The SetColors command was executing SetForegroundColor and then
SetBackgroundColor, which writes 2 extra characters per cell compared to
writing both colors in one command. This resulted in about 15-25% more
FPS (19->24 fps) on a fullscreen (171x51) app that writes every cell
with a different foreground and background color, compared to separately
using the SetForegroundColor and SetBackgroundColor commands (iTerm2, M2
Macbook Pro).

The app is the colors_rgb example in Ratatui, which writes every cell
with a different foreground and background color in a loop. The
CrosstermBackend was changed to use SetColors instead of
SetForegroundColor and SetBackgroundColor.


Note: this only effects non-windows performance.

Perf gains will vary depending on the terminal emulator. Testing on alacritty and kitty didn't see the same bump, but they were much faster to start with (~42-45 FPS). I suspect there's probably a badly performing code path for mutliple SGR commands in iTerm2.

There are other performance gains to come in the SetAttributes, SetStyle, etc. methods
Additionally, the Color commands use indexed 8-bit colors (Esc[5;<fg>m) instead of the shorter 3/4 bit colors, whice likely implies even shorter: E.g. Red on White (bright versions) is currently:
Esc[38;5;8mEsc[48;5;15m (19 chars) but should be: Esc[91;97m (8 chars).

The SetColors command was executing SetForegroundColor and then
SetBackgroundColor, which writes 2 extra characters per cell compared to
writing both colors in one command. This resulted in about 15-25% more
FPS (19->24 fps) on a fullscreen (171x51) app that writes every cell
with a different foreground and background color, compared to separately
using the SetForegroundColor and SetBackgroundColor commands (iTerm2, M2
Macbook Pro).

The app is the colors_rgb example in Ratatui, which writes every cell
with a different foreground and background color in a loop. The
CrosstermBackend was changed to use SetColors instead of
SetForegroundColor and SetBackgroundColor.
@joshka joshka requested a review from TimonPost as a code owner April 27, 2024 02:36
@joshka
Copy link
Collaborator Author

joshka commented Apr 27, 2024

Before:
image

After:
image

joshka added a commit to ratatui/ratatui that referenced this pull request Apr 27, 2024
Use Crossterm SetColors instead of SetForegroundColor and
SetBackgroundColor.

In crossterm-rs/crossterm#879 I changed the
SetColors command to write both colors at once with a single write
instead of multiple writes that more bytes. This led to a 15-25% fps
increase when testing the colors_rgb example on iTerm2 on an M2 Macbook
Pro.
joshka added a commit to joshka/ratatui that referenced this pull request Apr 27, 2024
Use Crossterm SetColors instead of SetForegroundColor and
SetBackgroundColor.

In crossterm-rs/crossterm#879 I changed the
SetColors command to write both colors at once with a single write
instead of multiple writes that more bytes. This led to a 15-25% fps
increase when testing the colors_rgb example on iTerm2 on an M2 Macbook
Pro.
joshka added a commit to ratatui/ratatui that referenced this pull request Apr 27, 2024
Use Crossterm SetColors instead of SetForegroundColor and
SetBackgroundColor.

In crossterm-rs/crossterm#879 I changed the
SetColors command to write both colors at once with a single write
instead of multiple writes that more bytes. This led to a 15-25% fps
increase when testing the colors_rgb example on iTerm2 on an M2 Macbook
Pro.
Copy link
Member

@TimonPost TimonPost left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noice!

@TimonPost TimonPost merged commit 7efe19d into crossterm-rs:master May 3, 2024
0 of 6 checks passed
@TimonPost
Copy link
Member

We can fix CI later.

@joshka joshka deleted the jm/perf-set-colors branch May 5, 2024 23:50
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

Successfully merging this pull request may close these issues.

2 participants