Optimization
don't deallocate cell char memory on model clear
This commit is contained in:
parent
049d653b6e
commit
058f9a70bf
@ -1182,8 +1182,7 @@ impl State {
|
|||||||
pub fn on_put(&mut self, text: String) -> RepaintMode {
|
pub fn on_put(&mut self, text: String) -> RepaintMode {
|
||||||
let double_width = text.is_empty();
|
let double_width = text.is_empty();
|
||||||
RepaintMode::Area(
|
RepaintMode::Area(
|
||||||
self.model
|
self.model.put(&text, double_width, self.cur_attrs.as_ref()),
|
||||||
.put(Some(text), double_width, self.cur_attrs.as_ref()),
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,7 +79,7 @@ impl Attrs {
|
|||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Cell {
|
pub struct Cell {
|
||||||
pub attrs: Attrs,
|
pub attrs: Attrs,
|
||||||
pub ch: Option<String>,
|
pub ch: String,
|
||||||
pub dirty: bool,
|
pub dirty: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -87,13 +87,13 @@ impl Cell {
|
|||||||
pub fn new_empty() -> Cell {
|
pub fn new_empty() -> Cell {
|
||||||
Cell {
|
Cell {
|
||||||
attrs: Attrs::new(),
|
attrs: Attrs::new(),
|
||||||
ch: None,
|
ch: String::new(),
|
||||||
dirty: true,
|
dirty: true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn clear(&mut self) {
|
pub fn clear(&mut self) {
|
||||||
self.ch = None;
|
self.ch.clear();
|
||||||
self.attrs.clear();
|
self.attrs.clear();
|
||||||
self.dirty = true;
|
self.dirty = true;
|
||||||
}
|
}
|
||||||
|
@ -254,7 +254,11 @@ impl StyledLine {
|
|||||||
continue;
|
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;
|
let len = line_str.len() - byte_offset;
|
||||||
|
|
||||||
for _ in 0..len {
|
for _ in 0..len {
|
||||||
@ -399,9 +403,9 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_styled_line() {
|
fn test_styled_line() {
|
||||||
let mut line = Line::new(3);
|
let mut line = Line::new(3);
|
||||||
line[0].ch = Some("a".to_owned());
|
line[0].ch = "a".to_owned();
|
||||||
line[1].ch = Some("b".to_owned());
|
line[1].ch = "b".to_owned();
|
||||||
line[2].ch = Some("c".to_owned());
|
line[2].ch = "c".to_owned();
|
||||||
|
|
||||||
let styled_line = StyledLine::from(&line, &color::ColorModel::new());
|
let styled_line = StyledLine::from(&line, &color::ColorModel::new());
|
||||||
assert_eq!("abc", styled_line.line_str);
|
assert_eq!("abc", styled_line.line_str);
|
||||||
|
@ -93,14 +93,16 @@ impl UiModel {
|
|||||||
(self.cur_row, self.cur_col)
|
(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 mut changed_region = self.cur_point();
|
||||||
let line = &mut self.model[self.cur_row];
|
let line = &mut self.model[self.cur_row];
|
||||||
line.dirty_line = true;
|
line.dirty_line = true;
|
||||||
|
|
||||||
let cell = &mut line[self.cur_col];
|
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 = attrs.map(Attrs::clone).unwrap_or_else(Attrs::new);
|
||||||
cell.attrs.double_width = double_width;
|
cell.attrs.double_width = double_width;
|
||||||
cell.dirty = true;
|
cell.dirty = true;
|
||||||
@ -305,7 +307,7 @@ mod tests {
|
|||||||
|
|
||||||
model.set_cursor(1, 1);
|
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.top);
|
||||||
assert_eq!(1, rect.left);
|
assert_eq!(1, rect.left);
|
||||||
|
@ -78,7 +78,7 @@ impl ModelLayout {
|
|||||||
self.insert_into_lines(ch);
|
self.insert_into_lines(ch);
|
||||||
self.layout_replace(0, 0);
|
self.layout_replace(0, 0);
|
||||||
} else {
|
} else {
|
||||||
self.model.put(Some(ch), false, None);
|
self.model.put(&ch, false, None);
|
||||||
}
|
}
|
||||||
|
|
||||||
self.model.set_cursor(row, col);
|
self.model.set_cursor(row, col);
|
||||||
@ -126,9 +126,9 @@ impl ModelLayout {
|
|||||||
}
|
}
|
||||||
|
|
||||||
self.model.set_cursor(row_idx, col_idx as usize);
|
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 {
|
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 {
|
if max_col_idx < col_idx {
|
||||||
@ -220,7 +220,7 @@ mod tests {
|
|||||||
|
|
||||||
let (cols, _) = model.size();
|
let (cols, _) = model.size();
|
||||||
assert_eq!(4, cols);
|
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]
|
#[test]
|
||||||
@ -234,7 +234,7 @@ mod tests {
|
|||||||
|
|
||||||
let (cols, _) = model.size();
|
let (cols, _) = model.size();
|
||||||
assert_eq!(3, cols);
|
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]
|
#[test]
|
||||||
|
Loading…
Reference in New Issue
Block a user