Skip to content

Commit

Permalink
perf(handlers): optimize string-buffer reallocations (#387)
Browse files Browse the repository at this point in the history
This improves `get_lines()` logic by using a string-buffer with a capacity hint.
That avoids growing the buffer from zero each time, saving a bunch of reallocations.
  • Loading branch information
lucab committed Jun 26, 2024
1 parent e1026f7 commit b8dfcda
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/handlers/graphical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1188,9 +1188,9 @@ impl GraphicalReportHandler {
let mut column = context_data.column();
let mut offset = context_data.span().offset();
let mut line_offset = offset;
let mut line_str = String::with_capacity(context.len());
let mut lines = Vec::with_capacity(1);
let mut iter = context.chars().peekable();
let mut line_str = String::new();
let mut lines = Vec::new();
while let Some(char) = iter.next() {
offset += char.len_utf8();
let mut at_end_of_file = false;
Expand Down
4 changes: 2 additions & 2 deletions src/handlers/narratable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,9 @@ impl NarratableReportHandler {
let mut column = context_data.column();
let mut offset = context_data.span().offset();
let mut line_offset = offset;
let mut line_str = String::with_capacity(context.len());
let mut lines = Vec::with_capacity(1);
let mut iter = context.chars().peekable();
let mut line_str = String::new();
let mut lines = Vec::new();
while let Some(char) = iter.next() {
offset += char.len_utf8();
let mut at_end_of_file = false;
Expand Down

0 comments on commit b8dfcda

Please sign in to comment.