From 48997f02d1819cc2e10c6445ea76d81de0f06212 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Mon, 19 Feb 2018 09:08:17 -0800 Subject: [PATCH] micro optimize cell creation --- README.md | 2 +- term/src/cell.rs | 35 +++++++++++++++++++++++------------ 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 51abdb311..0c9b6aa3b 100644 --- a/README.md +++ b/README.md @@ -141,7 +141,7 @@ set to 80x24 with 3500 lines of scrollback. `alacritty` has no scrollback. | xterm | 9.863 | | Gnome Terminal | 2.391 | | Terminator 1.91 | 2.319 | -| wezterm | 1.193 | +| wezterm | 1.033 | | kitty | 0.899 | | urxvt | 0.615 | | alacritty | 0.421 | diff --git a/term/src/cell.rs b/term/src/cell.rs index 6d8c49bfb..dd1eba78e 100644 --- a/term/src/cell.rs +++ b/term/src/cell.rs @@ -127,7 +127,8 @@ mod test { #[derive(Debug, Clone, Eq, PartialEq)] pub struct Cell { - bytes: [u8; 8], + len: u8, + bytes: [u8; 7], pub attrs: CellAttributes, } @@ -139,10 +140,11 @@ impl Default for Cell { impl Cell { pub fn new(s: &str, attrs: &CellAttributes) -> Cell { - let mut bytes = [0u8; 8]; - let len = s.len().min(8); + let mut bytes = [0u8; 7]; + let len = s.len().min(7); bytes[0..len].copy_from_slice(s.as_bytes()); Cell { + len: len as u8, bytes, attrs: attrs.clone(), } @@ -150,29 +152,38 @@ impl Cell { #[inline] pub fn bytes(&self) -> &[u8] { - if let Some(len) = self.bytes.iter().position(|&c| c == 0) { - &self.bytes[0..len] - } else { - &self.bytes - } + &self.bytes[0..self.len as usize] } pub fn from_char(c: char, attr: &CellAttributes) -> Cell { - let mut bytes = [0u8; 8]; - c.encode_utf8(&mut bytes); + let mut bytes = [0u8; 7]; + let len = if c == 0 as char { + 0u8 + } else if c < 0x80 as char { + bytes[0] = c as u8; + 1u8 + } else { + c.encode_utf8(&mut bytes).len() as u8 + }; Cell { + len, bytes, attrs: attr.clone(), } } + #[inline] pub fn str(&self) -> &str { str::from_utf8(self.bytes()).unwrap_or("?") } pub fn width(&self) -> usize { - use unicode_width::UnicodeWidthStr; - str::from_utf8(self.bytes()).unwrap_or("").width() + if self.len <= 1 { + self.len as usize + } else { + use unicode_width::UnicodeWidthStr; + self.str().width() + } } }