-
Notifications
You must be signed in to change notification settings - Fork 284
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
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
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.
TimonPost
approved these changes
May 3, 2024
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Noice!
We can fix CI later. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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).