on master help sign in
Optimize rendering by using Ropey::byte_slice

This avoids costly conversions via byte_to_char (which are then reversed back into bytes internally in Ropey). Reduces time spent in slice/byte_to_char from ~24% to ~5%.

commit59f05088b9628086b35631338e49ae8f061dcba2 authorBlaž Hrastnik <blaz@mxxn.io> dateMon 17 Jan 2022, 16:28 (5 years ago) parentc6bd105
5 files changed+16−17
Cargo.lock+8−2 helix-core/Cargo.toml+2−1 helix-core/src/graphemes.rs+1−4 helix-core/src/syntax.rs+2−6 helix-term/src/ui/editor.rs+3−4
Cargo.lock+8−2
@@ -862,10 +862,10 @@ checksum = "8c31b5c4033f8fdde8700e4657be2c497e7288f01515be52168c631e2e4d4086"
862862[[package]]
863863name = "ropey"
864864version = "1.3.2"
865−source = "registry+https://github.com/rust-lang/crates.io-index"
866−checksum = "e6b9aa65bcd9f308d37c7158b4a1afaaa32b8450213e20c9b98e7d5b3cc2fec3"
865+source = "git+https://github.com/cessen/ropey#38531e083f8cf5c5b1aeb328361895fb5a9b867c"
867866dependencies = [
868867 "smallvec",
868+ "str_indices",
869869]
870870
871871[[package]]
@@ -1031,6 +1031,12 @@ version = "1.0.5"
10311031source = "registry+https://github.com/rust-lang/crates.io-index"
10321032checksum = "d44a3643b4ff9caf57abcee9c2c621d6c03d9135e0d8b589bd9afb5992cb176a"
10331033
1034+[[package]]
1035+name = "str_indices"
1036+version = "0.3.1"
1037+source = "registry+https://github.com/rust-lang/crates.io-index"
1038+checksum = "283baa48c486e4c5e27b4d92c435db9eaceac236a74dab5e3293570e2c3fa4aa"
1039+
10341040[[package]]
10351041name = "syn"
10361042version = "1.0.88"
helix-core/Cargo.toml+2−1
@@ -15,7 +15,8 @@ include = ["src/**/*", "README.md"]
1515[dependencies]
1616helix-loader = { version = "0.6", path = "../helix-loader" }
1717
18−ropey = "1.3"
18+# ropey = "1.3"
19+ropey = { git = "https://github.com/cessen/ropey" }
1920smallvec = "1.8"
2021smartstring = "1.0.0"
2122unicode-segmentation = "1.9"
helix-core/src/graphemes.rs+1−4
@@ -333,10 +333,7 @@ impl<'a> Iterator for RopeGraphemes<'a> {
333333 }
334334
335335 if a < self.cur_chunk_start {
336− let a_char = self.text.byte_to_char(a);
337− let b_char = self.text.byte_to_char(b);
338−
339− Some(self.text.slice(a_char..b_char))
336+ Some(self.text.byte_slice(a..b))
340337 } else {
341338 let a2 = a - self.cur_chunk_start;
342339 let b2 = b - self.cur_chunk_start;
helix-core/src/syntax.rs+2−6
@@ -576,9 +576,7 @@ pub struct Syntax {
576576}
577577
578578fn byte_range_to_str(range: std::ops::Range<usize>, source: RopeSlice) -> Cow<str> {
579− let start_char = source.byte_to_char(range.start);
580− let end_char = source.byte_to_char(range.end);
581− Cow::from(source.slice(start_char..end_char))
579+ Cow::from(source.byte_slice(range))
582580}
583581
584582impl Syntax {
@@ -1197,9 +1195,7 @@ impl<'a> TextProvider<'a> for RopeProvider<'a> {
11971195 type I = ChunksBytes<'a>;
11981196
11991197 fn text(&mut self, node: Node) -> Self::I {
1200− let start_char = self.0.byte_to_char(node.start_byte());
1201− let end_char = self.0.byte_to_char(node.end_byte());
1202− let fragment = self.0.slice(start_char..end_char);
1198+ let fragment = self.0.byte_slice(node.start_byte()..node.end_byte());
12031199 ChunksBytes {
12041200 chunks: fragment.chunks(),
12051201 }
helix-term/src/ui/editor.rs+3−4
@@ -183,6 +183,7 @@ impl EditorView {
183183 .highlight_iter(text.slice(..), Some(range), None)
184184 .map(|event| event.unwrap())
185185 .map(move |event| match event {
186+ // TODO: use byte slices directly
186187 // convert byte offsets to char offset
187188 HighlightEvent::Source { start, end } => {
188189 let start =
@@ -317,6 +318,8 @@ impl EditorView {
317318 theme: &Theme,
318319 highlights: H,
319320 ) {
321+ // It's slightly more efficient to produce a full RopeSlice from the Rope, then slice that a bunch
322+ // of times than it is to always call Rope::slice/get_slice (it will internally always hit RSEnum::Light).
320323 let text = doc.text().slice(..);
321324
322325 let mut spans = Vec::new();
@@ -327,10 +330,6 @@ impl EditorView {
327330
328331 let text_style = theme.get("ui.text");
329332
330− // It's slightly more efficient to produce a full RopeSlice from the Rope, then slice that a bunch
331− // of times than it is to always call Rope::slice/get_slice (it will internally always hit RSEnum::Light).
332− let text = text.slice(..);
333−
334333 'outer: for event in highlights {
335334 match event {
336335 HighlightEvent::HighlightStart(span) => {