perf(search): pass each long-line window only the ranges that start in it (#5754)

On a line cut into windows, every window was handed all the matches left on the line and in_window reserved room for each, so the reservation added up to windows x matches: searching a 5 MB minified line asked the allocator for about 5 GiB. Each window now gets only the ranges that start in it (partition_point on the sorted ranges); in_window and the matches it returns are unchanged.

Co-authored-by: audichuang <[email protected]>
Цей коміт міститься в:
audichuang authored and GitHub committed 2026-11-25 14:25:10 +08:00
1 джерело 1cea43694e
коміт cdcce048fe
1 file changed
+15 -1
+15 -1
Переглянути файл
@@ -46,7 +46,10 @@ fn from_str(line_no: u32, s: &str, ranges: &[(usize, usize)], out: &mut Vec<Line
utf16_len(&s.as_bytes()[..bounds.0])
};
(prev_start, prev_offset) = (bounds.0, text_offset);
let (m, used) = in_window(line_no, s, rest, bounds, text_offset);
// Only the ranges starting in the window: `in_window` reserves room for all it is given,
// and the rest of a long line can hold every match on it.
let starting = rest.partition_point(|&(a, _)| a <= bounds.1);
let (m, used) = in_window(line_no, s, &rest[..starting], bounds, text_offset);
out.push(m);
rest = &rest[used.max(1)..];
if rest.is_empty() {
@@ -265,6 +268,17 @@ mod tests {
assert!(started.elapsed() < std::time::Duration::from_secs(2), "{:?}", started.elapsed());
}
#[test]
fn each_window_holds_room_for_its_own_matches_only() {
// 7,000 windows of one match each on a 4 MB line.
let s = format!("{}function", "x".repeat(592)).repeat(7_000);
let ranges: Vec<_> = s.match_indices("function").map(|(a, m)| (a, a + m.len())).collect();
let mut out = Vec::new();
line_matches(0, s.as_bytes(), &ranges, &mut out);
let reserved: usize = out.iter().map(|m| m.ranges.capacity()).sum();
assert!(reserved <= 4 * out.len(), "{reserved} ranges reserved for {} windows", out.len());
}
/// `main`'s windowing, which counts each window's offset from the line start.
fn reference(line_no: u32, line: &[u8], ranges: &[(usize, usize)]) -> Vec<LineMatch> {
fn from_str(line_no: u32, s: &str, ranges: &[(usize, usize)], out: &mut Vec<LineMatch>) {