Skip to content

Commit

Permalink
perf(crossterm): Speed up combined fg and bg color changes by up to 20%
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
joshka committed Apr 27, 2024
1 parent 4392759 commit 2747205
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions src/backend/crossterm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use crossterm::{
cursor::{Hide, MoveTo, Show},
execute, queue,
style::{
Attribute as CAttribute, Attributes as CAttributes, Color as CColor, ContentStyle, Print,
SetAttribute, SetBackgroundColor, SetForegroundColor,
Attribute as CAttribute, Attributes as CAttributes, Color as CColor, Colors, ContentStyle,
Print, SetAttribute, SetBackgroundColor, SetColors, SetForegroundColor,
},
terminal::{self, Clear},
};
Expand Down Expand Up @@ -145,14 +145,12 @@ where
diff.queue(&mut self.writer)?;
modifier = cell.modifier;
}
if cell.fg != fg {
let color = CColor::from(cell.fg);
queue!(self.writer, SetForegroundColor(color))?;
if cell.fg != fg || cell.bg != bg {
queue!(
self.writer,
SetColors(Colors::new(cell.fg.into(), cell.bg.into()))
)?;

Check warning on line 152 in src/backend/crossterm.rs

View check run for this annotation

Codecov / codecov/patch

src/backend/crossterm.rs#L148-L152

Added lines #L148 - L152 were not covered by tests
fg = cell.fg;
}
if cell.bg != bg {
let color = CColor::from(cell.bg);
queue!(self.writer, SetBackgroundColor(color))?;
bg = cell.bg;
}
#[cfg(feature = "underline-color")]
Expand Down

0 comments on commit 2747205

Please sign in to comment.