@@ -37,6 +37,7 @@ pub struct Editor {
3737 first_line: u16,
3838 size: (u16, u16),
3939 surface: Surface,
40+ cache: Surface,
4041 theme: Theme,
4142}
4243
@@ -55,6 +56,7 @@ impl Editor {
5556 first_line: 0,
5657 size,
5758 surface: Surface::empty(area),
59+ cache: Surface::empty(area),
5860 theme,
5961 // TODO; move to state
6062 };
@@ -82,8 +84,8 @@ impl Editor {
8284 match &mut self.state {
8385 Some(state) => {
8486 let area = Rect::new(0, 0, self.size.0, self.size.1);
85− let mut surface = Surface::empty(area);
8687 let mut stdout = stdout();
88+ self.surface.reset(); // reset is faster than allocating new empty surface
8789
8890 // TODO: inefficient, should feed chunks.iter() to tree_sitter.parse_with(|offset, pos|)
8991 let source_code = state.doc().to_string();
@@ -146,7 +148,12 @@ impl Editor {
146148 // which should really be the majority case
147149 let grapheme = std::borrow::Cow::from(grapheme);
148150 let width = grapheme_width(&grapheme) as u16;
149− surface.set_string(offset + visual_x, line, grapheme, style);
151+ self.surface.set_string(
152+ offset + visual_x,
153+ line,
154+ grapheme,
155+ style,
156+ );
150157
151158 visual_x += width;
152159 }
@@ -178,7 +185,7 @@ impl Editor {
178185 // (x2 - x1 + 1) as u16,
179186 // (y2 - y1 + 1) as u16,
180187 // );
181− // surface.set_style(area, select);
188+ // self.surface.set_style(area, select);
182189
183190 // // TODO: don't highlight next char in append mode
184191 // }
@@ -199,9 +206,9 @@ impl Editor {
199206
200207 self.terminal
201208 .backend_mut()
202− .draw(self.surface.diff(&surface).into_iter());
209+ .draw(self.cache.diff(&self.surface).into_iter());
203210 // swap the buffer
204− self.surface = surface;
211+ std::mem::swap(&mut self.surface, &mut self.cache);
205212
206213 // set cursor shape
207214 match state.mode() {