From 74d7417564012ff9a98df179470cedd5677b1018 Mon Sep 17 00:00:00 2001 From: daa Date: Sun, 8 Apr 2018 13:46:18 +0300 Subject: [PATCH] Small memory optimization --- src/shell.rs | 2 +- src/ui_model/cell.rs | 10 +++------- src/ui_model/line.rs | 37 +++++++++++++++++------------------- src/ui_model/mod.rs | 4 ++-- src/ui_model/model_layout.rs | 10 +++++----- 5 files changed, 28 insertions(+), 35 deletions(-) diff --git a/src/shell.rs b/src/shell.rs index 900f398..0c16053 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -1157,7 +1157,7 @@ impl State { pub fn on_put(&mut self, text: String) -> RepaintMode { let double_width = text.is_empty(); - RepaintMode::Area(self.model.put(text, double_width, self.cur_attrs.as_ref())) + RepaintMode::Area(self.model.put(Some(text), double_width, self.cur_attrs.as_ref())) } pub fn on_clear(&mut self) -> RepaintMode { diff --git a/src/ui_model/cell.rs b/src/ui_model/cell.rs index 5e409b6..7bd07d1 100644 --- a/src/ui_model/cell.rs +++ b/src/ui_model/cell.rs @@ -79,25 +79,21 @@ impl Attrs { #[derive(Clone)] pub struct Cell { pub attrs: Attrs, - pub ch: String, + pub ch: Option, pub dirty: bool, } impl Cell { pub fn new_empty() -> Cell { - Cell::new(" ".to_owned()) - } - - pub fn new(ch: String) -> Cell { Cell { attrs: Attrs::new(), - ch, + ch: None, dirty: true, } } pub fn clear(&mut self) { - self.ch = " ".to_owned(); + self.ch = None; self.attrs.clear(); self.dirty = true; } diff --git a/src/ui_model/line.rs b/src/ui_model/line.rs index 259ab9d..12092f4 100644 --- a/src/ui_model/line.rs +++ b/src/ui_model/line.rs @@ -70,12 +70,9 @@ impl Line { fn set_cell_to_item(&mut self, new_item: &PangoItemPosition) -> bool { let start_item_idx = self.cell_to_item(new_item.start_cell); let start_item_cells_count = if start_item_idx >= 0 { - self.item_line[start_item_idx as usize].as_ref().map_or( - -1, - |item| { - item.cells_count as i32 - }, - ) + self.item_line[start_item_idx as usize] + .as_ref() + .map_or(-1, |item| item.cells_count as i32) } else { -1 }; @@ -85,9 +82,9 @@ impl Line { // start_item == idx of item start cell // in case different item length was in previous iteration // mark all item as dirty - if start_item_idx != new_item.start_cell as i32 || - new_item.cells_count() != start_item_cells_count || - start_item_idx == -1 || end_item_idx == -1 + if start_item_idx != new_item.start_cell as i32 + || new_item.cells_count() != start_item_cells_count || start_item_idx == -1 + || end_item_idx == -1 { self.initialize_cell_item(new_item.start_cell, new_item.end_cell, new_item.item); true @@ -110,9 +107,9 @@ impl Line { } pub fn merge(&mut self, old_items: &StyledLine, pango_items: &[sys_pango::Item]) { - let mut pango_item_iter = pango_items.iter().map(|item| { - PangoItemPosition::new(old_items, item) - }); + let mut pango_item_iter = pango_items + .iter() + .map(|item| PangoItemPosition::new(old_items, item)); let mut next_item = pango_item_iter.next(); let mut move_to_next_item = false; @@ -120,7 +117,7 @@ impl Line { let mut cell_idx = 0; while cell_idx < self.line.len() { let dirty = match next_item { - None => self.set_cell_to_empty(cell_idx), + None => self.set_cell_to_empty(cell_idx), Some(ref new_item) => { if cell_idx < new_item.start_cell { self.set_cell_to_empty(cell_idx) @@ -257,7 +254,7 @@ impl StyledLine { continue; } - line_str.push_str(&cell.ch); + line_str.push_str(cell.ch.as_ref().map(|ch| ch.as_str()).unwrap_or(" ")); let len = line_str.len() - byte_offset; for _ in 0..len { @@ -389,9 +386,9 @@ impl<'c> StyleAttr<'c> { impl<'c> PartialEq for StyleAttr<'c> { fn eq(&self, other: &Self) -> bool { - self.italic == other.italic && self.bold == other.bold && - self.foreground == other.foreground && self.empty == other.empty && - self.background == other.background + self.italic == other.italic && self.bold == other.bold + && self.foreground == other.foreground && self.empty == other.empty + && self.background == other.background } } @@ -402,9 +399,9 @@ mod tests { #[test] fn test_styled_line() { let mut line = Line::new(3); - line[0].ch = "a".to_owned(); - line[1].ch = "b".to_owned(); - line[2].ch = "c".to_owned(); + line[0].ch = Some("a".to_owned()); + line[1].ch = Some("b".to_owned()); + line[2].ch = Some("c".to_owned()); let styled_line = StyledLine::from(&line, &color::ColorModel::new()); assert_eq!("abc", styled_line.line_str); diff --git a/src/ui_model/mod.rs b/src/ui_model/mod.rs index 2e09658..823751f 100644 --- a/src/ui_model/mod.rs +++ b/src/ui_model/mod.rs @@ -93,7 +93,7 @@ impl UiModel { (self.cur_row, self.cur_col) } - pub fn put(&mut self, ch: String, double_width: bool, attrs: Option<&Attrs>) -> ModelRect { + pub fn put(&mut self, ch: Option, double_width: bool, attrs: Option<&Attrs>) -> ModelRect { let mut changed_region = self.cur_point(); let line = &mut self.model[self.cur_row]; line.dirty_line = true; @@ -305,7 +305,7 @@ mod tests { model.set_cursor(1, 1); - let rect = model.put(" ".to_owned(), false, None); + let rect = model.put(Some(" ".to_owned()), false, None); assert_eq!(1, rect.top); assert_eq!(1, rect.left); diff --git a/src/ui_model/model_layout.rs b/src/ui_model/model_layout.rs index b680ccf..6307021 100644 --- a/src/ui_model/model_layout.rs +++ b/src/ui_model/model_layout.rs @@ -78,7 +78,7 @@ impl ModelLayout { self.insert_into_lines(ch); self.layout_replace(0, 0); } else { - self.model.put(ch, false, None); + self.model.put(Some(ch), false, None); } self.model.set_cursor(row, col); @@ -126,9 +126,9 @@ impl ModelLayout { } self.model.set_cursor(row_idx, col_idx as usize); - self.model.put(ch.clone(), false, attr.as_ref()); + self.model.put(Some(ch.clone()), false, attr.as_ref()); if ch_width > 1 { - self.model.put(" ".to_owned(), true, attr.as_ref()); + self.model.put(None, true, attr.as_ref()); } if max_col_idx < col_idx { @@ -220,7 +220,7 @@ mod tests { let (cols, _) = model.size(); assert_eq!(4, cols); - assert_eq!("b", model.model.model()[0].line[1].ch); + assert_eq!(Some("b".to_owned()), model.model.model()[0].line[1].ch); } #[test] @@ -234,7 +234,7 @@ mod tests { let (cols, _) = model.size(); assert_eq!(3, cols); - assert_eq!("b", model.model.model()[0].line[1].ch); + assert_eq!(Some("b".to_owned()), model.model.model()[0].line[1].ch); } #[test]