about summary refs log tree commit diff
diff options
context:
space:
mode:
authorThe 8472 <git@infinite-source.de>2022-09-19 00:25:44 +0200
committerThe 8472 <git@infinite-source.de>2022-09-19 00:48:14 +0200
commit40b37268eb0926b4ed55711d25fcf7aa19295e95 (patch)
tree7cd55d394b446c2ab6d27f19f051af9f8ba5289d
parent98ad6a5519651af36e246c0335c964dd52c554ba (diff)
downloadrust-40b37268eb0926b4ed55711d25fcf7aa19295e95.tar.gz
rust-40b37268eb0926b4ed55711d25fcf7aa19295e95.zip
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.
-rw-r--r--compiler/rustc_span/src/lib.rs7
1 files changed, 3 insertions, 4 deletions
diff --git a/compiler/rustc_span/src/lib.rs b/compiler/rustc_span/src/lib.rs
index ada3bae6150..6ce75492caf 100644
--- a/compiler/rustc_span/src/lib.rs
+++ b/compiler/rustc_span/src/lib.rs
@@ -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),
         })
     }