about summary refs log tree commit diff
diff options
context:
space:
mode:
authorroife <roifewu@gmail.com>2024-01-18 17:03:29 +0800
committerroife <roifewu@gmail.com>2024-01-18 17:03:29 +0800
commit04ce4ce4403fa3e29817ad670a7aa648996e7326 (patch)
tree47c8d03d1dc96861ea2f45295fd566986cec073a
parent9d9b34354d2f13e33568c9c55b226dd014a146a0 (diff)
downloadrust-04ce4ce4403fa3e29817ad670a7aa648996e7326.tar.gz
rust-04ce4ce4403fa3e29817ad670a7aa648996e7326.zip
internal: speedup LineEndings calculation using 'memchr'
-rw-r--r--Cargo.lock5
-rw-r--r--crates/rust-analyzer/Cargo.toml1
-rw-r--r--crates/rust-analyzer/src/line_index.rs5
3 files changed, 7 insertions, 4 deletions
diff --git a/Cargo.lock b/Cargo.lock
index e1e85d0963d..a743d1c870a 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1001,9 +1001,9 @@ dependencies = [
 
 [[package]]
 name = "memchr"
-version = "2.6.4"
+version = "2.7.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167"
+checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149"
 
 [[package]]
 name = "memmap2"
@@ -1532,6 +1532,7 @@ dependencies = [
  "lsp-server 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)",
  "lsp-types",
  "mbe",
+ "memchr",
  "mimalloc",
  "nohash-hasher",
  "num_cpus",
diff --git a/crates/rust-analyzer/Cargo.toml b/crates/rust-analyzer/Cargo.toml
index 76414160716..db5cabaf769 100644
--- a/crates/rust-analyzer/Cargo.toml
+++ b/crates/rust-analyzer/Cargo.toml
@@ -63,6 +63,7 @@ parser.workspace = true
 toolchain.workspace = true
 vfs-notify.workspace = true
 vfs.workspace = true
+memchr = "2.7.1"
 
 [target.'cfg(windows)'.dependencies]
 winapi = "0.3.9"
diff --git a/crates/rust-analyzer/src/line_index.rs b/crates/rust-analyzer/src/line_index.rs
index 15450303ff2..95176207407 100644
--- a/crates/rust-analyzer/src/line_index.rs
+++ b/crates/rust-analyzer/src/line_index.rs
@@ -6,6 +6,7 @@
 //! convert back to `\r\n` on the way out).
 
 use ide_db::line_index::WideEncoding;
+use memchr::memmem;
 use triomphe::Arc;
 
 #[derive(Clone, Copy)]
@@ -39,10 +40,10 @@ impl LineEndings {
         let mut tail = buf.as_mut_slice();
         let mut crlf_seen = false;
 
-        let find_crlf = |src: &[u8]| src.windows(2).position(|it| it == b"\r\n");
+        let finder = memmem::Finder::new(b"\r\n");
 
         loop {
-            let idx = match find_crlf(&tail[gap_len..]) {
+            let idx = match finder.find(&tail[gap_len..]) {
                 None if crlf_seen => tail.len(),
                 // SAFETY: buf is unchanged and therefore still contains utf8 data
                 None => return (unsafe { String::from_utf8_unchecked(buf) }, LineEndings::Unix),