Skip to content

Commit

Permalink
use partition_point instead of binary_search when looking up source l…
Browse files Browse the repository at this point in the history
…ines

In local benchmarks this results in 0.4% fewer cycles in a critical sequential
section when compiling libcore.
  • Loading branch information
the8472 committed Sep 18, 2022
1 parent 98ad6a5 commit 40b3726
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions compiler/rustc_span/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1631,10 +1631,9 @@ impl SourceFile {
/// number. If the source_file is empty or the position is located before the
/// first line, `None` is returned.
pub fn lookup_line(&self, pos: BytePos) -> Option<usize> {
self.lines(|lines| match lines.binary_search(&pos) {
Ok(idx) => Some(idx),
Err(0) => None,
Err(idx) => Some(idx - 1),
self.lines(|lines| match lines.partition_point(|x| x <= &pos) {
0 => None,
i => Some(i - 1),
})
}

Expand Down

0 comments on commit 40b3726

Please sign in to comment.