misc(perf): nit perf improvements (#244)

This commit is contained in:
Yoni Feng 2023-03-30 06:21:40 +03:00 committed by GitHub
parent a215720576
commit 566d6be6e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 5 deletions

View File

@ -316,7 +316,7 @@ impl GraphicalReportHandler {
})
.collect::<Result<Vec<Box<dyn SpanContents<'_>>>, MietteError>>()
.map_err(|_| fmt::Error)?;
let mut contexts = Vec::new();
let mut contexts = Vec::with_capacity(contents.len());
for (right, right_conts) in labels.iter().cloned().zip(contents.iter()) {
if contexts.is_empty() {
contexts.push((right, right_conts));

View File

@ -3,6 +3,7 @@ Default trait implementations for [`SourceCode`].
*/
use std::{
borrow::{Cow, ToOwned},
collections::VecDeque,
fmt::Debug,
sync::Arc,
};
@ -19,7 +20,7 @@ fn context_info<'a>(
let mut line_count = 0usize;
let mut start_line = 0usize;
let mut start_column = 0usize;
let mut before_lines_starts = Vec::new();
let mut before_lines_starts = VecDeque::new();
let mut current_line_start = 0usize;
let mut end_lines = 0usize;
let mut post_span = false;
@ -34,10 +35,10 @@ fn context_info<'a>(
if offset < span.offset() {
// We're before the start of the span.
start_column = 0;
before_lines_starts.push(current_line_start);
before_lines_starts.push_back(current_line_start);
if before_lines_starts.len() > context_lines_before {
start_line += 1;
before_lines_starts.remove(0);
before_lines_starts.pop_front();
}
} else if offset >= span.offset() + span.len().saturating_sub(1) {
// We're after the end of the span, but haven't necessarily
@ -73,7 +74,7 @@ fn context_info<'a>(
}
if offset >= (span.offset() + span.len()).saturating_sub(1) {
let starting_offset = before_lines_starts.first().copied().unwrap_or_else(|| {
let starting_offset = before_lines_starts.front().copied().unwrap_or_else(|| {
if context_lines_before == 0 {
span.offset()
} else {