on master help sign in
Speed up surface code by resetting instead of allocating a new one.
eb477ecBlaž Hrastnik
helix-term/src/editor.rs+12−5
@@ -37,6 +37,7 @@ pub struct Editor {
37 first_line: u16,
37 first_line: u16,
38 size: (u16, u16),
38 size: (u16, u16),
39 surface: Surface,
39 surface: Surface,
40+ cache: Surface,
40 theme: Theme,
41 theme: Theme,
41}
42}
42
43
@@ -55,6 +56,7 @@ impl Editor {
55 first_line: 0,
56 first_line: 0,
56 size,
57 size,
57 surface: Surface::empty(area),
58 surface: Surface::empty(area),
59+ cache: Surface::empty(area),
58 theme,
60 theme,
59 // TODO; move to state
61 // TODO; move to state
60 };
62 };
@@ -82,8 +84,8 @@ impl Editor {
82 match &mut self.state {
84 match &mut self.state {
83 Some(state) => {
85 Some(state) => {
84 let area = Rect::new(0, 0, self.size.0, self.size.1);
86 let area = Rect::new(0, 0, self.size.0, self.size.1);
85− let mut surface = Surface::empty(area);
86 let mut stdout = stdout();
87 let mut stdout = stdout();
88+ self.surface.reset(); // reset is faster than allocating new empty surface
87
89
88 // TODO: inefficient, should feed chunks.iter() to tree_sitter.parse_with(|offset, pos|)
90 // TODO: inefficient, should feed chunks.iter() to tree_sitter.parse_with(|offset, pos|)
89 let source_code = state.doc().to_string();
91 let source_code = state.doc().to_string();
@@ -146,7 +148,12 @@ impl Editor {
146 // which should really be the majority case
148 // which should really be the majority case
147 let grapheme = std::borrow::Cow::from(grapheme);
149 let grapheme = std::borrow::Cow::from(grapheme);
148 let width = grapheme_width(&grapheme) as u16;
150 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+ );
150
157
151 visual_x += width;
158 visual_x += width;
152 }
159 }
@@ -178,7 +185,7 @@ impl Editor {
178 // (x2 - x1 + 1) as u16,
185 // (x2 - x1 + 1) as u16,
179 // (y2 - y1 + 1) as u16,
186 // (y2 - y1 + 1) as u16,
180 // );
187 // );
181− // surface.set_style(area, select);
188+ // self.surface.set_style(area, select);
182
189
183 // // TODO: don't highlight next char in append mode
190 // // TODO: don't highlight next char in append mode
184 // }
191 // }
@@ -199,9 +206,9 @@ impl Editor {
199
206
200 self.terminal
207 self.terminal
201 .backend_mut()
208 .backend_mut()
202− .draw(self.surface.diff(&surface).into_iter());
209+ .draw(self.cache.diff(&self.surface).into_iter());
203 // swap the buffer
210 // swap the buffer
204− self.surface = surface;
211+ std::mem::swap(&mut self.surface, &mut self.cache);
205
212
206 // set cursor shape
213 // set cursor shape
207 match state.mode() {
214 match state.mode() {