Optimization

don't deallocate cell char memory on model clear
This commit is contained in:
daa 2018-04-29 15:44:35 +03:00
parent 049d653b6e
commit 058f9a70bf
5 changed files with 22 additions and 17 deletions

View File

@ -1182,8 +1182,7 @@ impl State {
pub fn on_put(&mut self, text: String) -> RepaintMode {
let double_width = text.is_empty();
RepaintMode::Area(
self.model
.put(Some(text), double_width, self.cur_attrs.as_ref()),
self.model.put(&text, double_width, self.cur_attrs.as_ref()),
)
}

View File

@ -79,7 +79,7 @@ impl Attrs {
#[derive(Clone)]
pub struct Cell {
pub attrs: Attrs,
pub ch: Option<String>,
pub ch: String,
pub dirty: bool,
}
@ -87,13 +87,13 @@ impl Cell {
pub fn new_empty() -> Cell {
Cell {
attrs: Attrs::new(),
ch: None,
ch: String::new(),
dirty: true,
}
}
pub fn clear(&mut self) {
self.ch = None;
self.ch.clear();
self.attrs.clear();
self.dirty = true;
}

View File

@ -254,7 +254,11 @@ impl StyledLine {
continue;
}
line_str.push_str(cell.ch.as_ref().map(|ch| ch.as_str()).unwrap_or(" "));
if !cell.ch.is_empty() {
line_str.push_str(&cell.ch);
} else {
line_str.push(' ');
}
let len = line_str.len() - byte_offset;
for _ in 0..len {
@ -399,9 +403,9 @@ mod tests {
#[test]
fn test_styled_line() {
let mut line = Line::new(3);
line[0].ch = Some("a".to_owned());
line[1].ch = Some("b".to_owned());
line[2].ch = Some("c".to_owned());
line[0].ch = "a".to_owned();
line[1].ch = "b".to_owned();
line[2].ch = "c".to_owned();
let styled_line = StyledLine::from(&line, &color::ColorModel::new());
assert_eq!("abc", styled_line.line_str);

View File

@ -93,14 +93,16 @@ impl UiModel {
(self.cur_row, self.cur_col)
}
pub fn put(&mut self, ch: Option<String>, double_width: bool, attrs: Option<&Attrs>) -> ModelRect {
pub fn put(&mut self, ch: &str, 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;
let cell = &mut line[self.cur_col];
cell.ch = ch;
cell.ch.clear();
cell.ch.push_str(ch);
cell.attrs = attrs.map(Attrs::clone).unwrap_or_else(Attrs::new);
cell.attrs.double_width = double_width;
cell.dirty = true;
@ -305,7 +307,7 @@ mod tests {
model.set_cursor(1, 1);
let rect = model.put(Some(" ".to_owned()), false, None);
let rect = model.put(" ", 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(Some(ch), false, None);
self.model.put(&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(Some(ch.clone()), false, attr.as_ref());
self.model.put(ch, false, attr.as_ref());
if ch_width > 1 {
self.model.put(None, true, attr.as_ref());
self.model.put("", 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!(Some("b".to_owned()), model.model.model()[0].line[1].ch);
assert_eq!("b", model.model.model()[0].line[1].ch);
}
#[test]
@ -234,7 +234,7 @@ mod tests {
let (cols, _) = model.size();
assert_eq!(3, cols);
assert_eq!(Some("b".to_owned()), model.model.model()[0].line[1].ch);
assert_eq!("b", model.model.model()[0].line[1].ch);
}
#[test]