use partition_point instead of binary_search when looking up source lines

In local benchmarks this results in 0.4% fewer cycles in a critical sequential
section when compiling libcore.
This commit is contained in:
The 8472 2022-09-19 00:25:44 +02:00
parent 98ad6a5519
commit 40b37268eb

View File

@ -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),
})
}