Speed up surface code by resetting instead of allocating a new one.

pull/1/head
Blaž Hrastnik 4 years ago
parent 088f8a82af
commit eb477ec442

@ -37,6 +37,7 @@ pub struct Editor {
first_line: u16, first_line: u16,
size: (u16, u16), size: (u16, u16),
surface: Surface, surface: Surface,
cache: Surface,
theme: Theme, theme: Theme,
} }
@ -55,6 +56,7 @@ impl Editor {
first_line: 0, first_line: 0,
size, size,
surface: Surface::empty(area), surface: Surface::empty(area),
cache: Surface::empty(area),
theme, theme,
// TODO; move to state // TODO; move to state
}; };
@ -82,8 +84,8 @@ impl Editor {
match &mut self.state { match &mut self.state {
Some(state) => { Some(state) => {
let area = Rect::new(0, 0, self.size.0, self.size.1); let area = Rect::new(0, 0, self.size.0, self.size.1);
let mut surface = Surface::empty(area);
let mut stdout = stdout(); let mut stdout = stdout();
self.surface.reset(); // reset is faster than allocating new empty surface
// TODO: inefficient, should feed chunks.iter() to tree_sitter.parse_with(|offset, pos|) // TODO: inefficient, should feed chunks.iter() to tree_sitter.parse_with(|offset, pos|)
let source_code = state.doc().to_string(); let source_code = state.doc().to_string();
@ -146,7 +148,12 @@ impl Editor {
// which should really be the majority case // which should really be the majority case
let grapheme = std::borrow::Cow::from(grapheme); let grapheme = std::borrow::Cow::from(grapheme);
let width = grapheme_width(&grapheme) as u16; let width = grapheme_width(&grapheme) as u16;
surface.set_string(offset + visual_x, line, grapheme, style); self.surface.set_string(
offset + visual_x,
line,
grapheme,
style,
);
visual_x += width; visual_x += width;
} }
@ -178,7 +185,7 @@ impl Editor {
// (x2 - x1 + 1) as u16, // (x2 - x1 + 1) as u16,
// (y2 - y1 + 1) as u16, // (y2 - y1 + 1) as u16,
// ); // );
// surface.set_style(area, select); // self.surface.set_style(area, select);
// // TODO: don't highlight next char in append mode // // TODO: don't highlight next char in append mode
// } // }
@ -199,9 +206,9 @@ impl Editor {
self.terminal self.terminal
.backend_mut() .backend_mut()
.draw(self.surface.diff(&surface).into_iter()); .draw(self.cache.diff(&self.surface).into_iter());
// swap the buffer // swap the buffer
self.surface = surface; std::mem::swap(&mut self.surface, &mut self.cache);
// set cursor shape // set cursor shape
match state.mode() { match state.mode() {

Loading…
Cancel
Save