perf: speed up SetColors by ~15-25% #879
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.
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).