Small memory optimization

This commit is contained in:
daa 2018-04-08 13:46:18 +03:00
parent 3e15e5fa2a
commit 74d7417564
5 changed files with 28 additions and 35 deletions

View File

@ -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 {

View File

@ -79,25 +79,21 @@ impl Attrs {
#[derive(Clone)]
pub struct Cell {
pub attrs: Attrs,
pub ch: String,
pub ch: Option<String>,
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;
}

View File

@ -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);

View File

@ -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<String>, 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);

View File

@ -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]