perf(handlers): optimize string-buffer reallocations

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.
This commit is contained in:
Luca BRUNO 2024-06-26 11:59:05 +02:00
parent 813232ba79
commit 0c73830d08
No known key found for this signature in database
GPG Key ID: C9FA3B36E61883AF
2 changed files with 4 additions and 4 deletions

View File

@ -1196,9 +1196,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;

View File

@ -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;