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
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 17 additions & 6 deletions src/style.rs
Expand Up @@ -279,13 +279,24 @@ pub struct SetColors(pub Colors);

impl Command for SetColors {
fn write_ansi(&self, f: &mut impl fmt::Write) -> fmt::Result {
if let Some(color) = self.0.foreground {
SetForegroundColor(color).write_ansi(f)?;
}
if let Some(color) = self.0.background {
SetBackgroundColor(color).write_ansi(f)?;
// Writing both foreground and background colors in one command resulted in about 20% more
// FPS (20 to 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). `Esc[38;5;<fg>mEsc[48;5;<bg>m` (16
// chars) vs `Esc[38;5;<fg>;48;5;<bg>m` (14 chars)
match (self.0.foreground, self.0.background) {
(Some(fg), Some(bg)) => {
write!(
f,
csi!("{};{}m"),
Colored::ForegroundColor(fg),
Colored::BackgroundColor(bg)
)
}
(Some(fg), None) => write!(f, csi!("{}m"), Colored::ForegroundColor(fg)),
(None, Some(bg)) => write!(f, csi!("{}m"), Colored::BackgroundColor(bg)),
(None, None) => Ok(()),
}
Ok(())
}

#[cfg(windows)]
Expand Down