Skip to content

Commit 2225547

Browse files
authored
perf(interface): estimate capacity for SourceFile::lines (#515)
Estimate capacity at `len / 32`. In most cases this reallocates 0 or 1 times.
1 parent 85a8be1 commit 2225547

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

crates/interface/src/source_map/analyze.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ use crate::pos::RelativeBytePos;
77
/// This function will use an SSE2 enhanced implementation if hardware support
88
/// is detected at runtime.
99
pub(crate) fn analyze_source_file(src: &str) -> (Vec<RelativeBytePos>, Vec<MultiByteChar>) {
10-
let mut lines = vec![RelativeBytePos::from_u32(0)];
10+
// In most cases this will re-allocate 0 or 1 times.
11+
let lines_upper_bound = 1 + src.len() / 32;
12+
let mut lines = Vec::with_capacity(lines_upper_bound);
13+
lines.push(RelativeBytePos::from_u32(0));
14+
1115
let mut multi_byte_chars = vec![];
1216

1317
// Calls the right implementation, depending on hardware support available.

0 commit comments

Comments
 (0)