Unnamed repository; edit this file 'description' to name the repository.
internal: speedup LineEndings calculation using 'memchr'
roife 2024-01-18
parent 9d9b343 · commit 04ce4ce
-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 e1e85d0963..a743d1c870 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 7641416071..db5cabaf76 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 15450303ff..9517620740 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),