diff options
author | Wilfred Hughes <me@wilfred.me.uk> | 2022-01-06 00:54:52 -0800 |
---|---|---|
committer | Wilfred Hughes <me@wilfred.me.uk> | 2022-01-06 00:54:52 -0800 |
commit | c511b634f731b875a3c592ab9408b3f86c561536 (patch) | |
tree | fd288217dda0c6b51d85f58342072a0b27046219 | |
parent | 2f8a82de158bc04a6f66ca0a34fcd5a6cb1a2925 (diff) |
Only split source code on newlines once0.15.0
Widths::new() is called once per hunk, so repeatedly splitting on
newlines can become a major performance bottleneck.
-rw-r--r-- | CHANGELOG.md | 2 | ||||
-rw-r--r-- | src/side_by_side.rs | 13 |
2 files changed, 7 insertions, 8 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 676edfd14..02c354087 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,8 @@ poor alignment of unchanged lines. Fixed minor formatting issues when reporting that a file is binary. +Improved display performance on large files. + ## 0.14 (released 27 December 2021) ### Parsing diff --git a/src/side_by_side.rs b/src/side_by_side.rs index 22f547dc0..f9cb7c527 100644 --- a/src/side_by_side.rs +++ b/src/side_by_side.rs @@ -134,12 +134,9 @@ impl Widths { fn new( terminal_width: usize, line_nums: &[(Option<LineNumber>, Option<LineNumber>)], - lhs_src: &str, - rhs_src: &str, + lhs_lines: &[String], + rhs_lines: &[String], ) -> Self { - let lhs_lines = split_on_newlines(lhs_src); - let rhs_lines = split_on_newlines(rhs_src); - let mut lhs_max_line: LineNumber = 1.into(); let mut rhs_max_line: LineNumber = 1.into(); let mut lhs_max_content = 1; @@ -291,7 +288,7 @@ pub fn display_hunks( let no_rhs_changes = hunk.lines.iter().all(|(_, r)| r.is_none()); let same_lines = aligned_lines.iter().all(|(l, r)| l == r); - let widths = Widths::new(display_width(), &aligned_lines, lhs_src, rhs_src); + let widths = Widths::new(display_width(), &aligned_lines, &lhs_lines, &rhs_lines); for (lhs_line_num, rhs_line_num) in aligned_lines { let lhs_line_novel = highlight_as_novel( lhs_line_num, @@ -437,8 +434,8 @@ mod tests { let widths = Widths::new( 80, &line_nums, - "foo\nbar\n", - "x\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\n", + &split_on_newlines("foo\nbar\n"), + &split_on_newlines("x\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\n"), ); assert_eq!(widths.lhs_line_nums, 2); |