Skip to content

Commit 1eed08b

Browse files
committed
Convert SourceMap scan to binary search
1 parent f4708a8 commit 1eed08b

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/fallback.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use alloc::collections::BTreeMap;
1111
use core::cell::RefCell;
1212
#[cfg(span_locations)]
1313
use core::cmp;
14+
#[cfg(all(span_locations, not(fuzzing)))]
15+
use core::cmp::Ordering;
1416
use core::fmt::{self, Debug, Display, Write};
1517
use core::mem::ManuallyDrop;
1618
#[cfg(span_locations)]
@@ -456,12 +458,19 @@ impl SourceMap {
456458
}
457459

458460
fn find(&self, span: Span) -> usize {
459-
for (i, file) in self.files.iter().enumerate() {
460-
if file.span_within(span) {
461-
return i;
461+
match self.files.binary_search_by(|file| {
462+
if file.span.hi < span.lo {
463+
Ordering::Less
464+
} else if file.span.lo > span.hi {
465+
Ordering::Greater
466+
} else {
467+
assert!(file.span_within(span));
468+
Ordering::Equal
462469
}
470+
}) {
471+
Ok(i) => return i,
472+
Err(_) => unreachable!("Invalid span with no related FileInfo!"),
463473
}
464-
unreachable!("Invalid span with no related FileInfo!");
465474
}
466475

467476
fn filepath(&self, span: Span) -> String {

0 commit comments

Comments
 (0)