Auto merge of #16395 - roife:internal/speed-up-LineEndings-with-memchr, r=Veykril

internal: speedup LineEndings calculation using 'memchr'

See https://github.com/rust-lang/rust-analyzer/issues/13538
This commit is contained in:
bors 2024-01-18 09:18:41 +00:00
commit 8bb500aaac
3 changed files with 7 additions and 4 deletions

5
Cargo.lock generated
View file

@ -1001,9 +1001,9 @@ dependencies = [
[[package]] [[package]]
name = "memchr" name = "memchr"
version = "2.6.4" version = "2.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167" checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149"
[[package]] [[package]]
name = "memmap2" name = "memmap2"
@ -1532,6 +1532,7 @@ dependencies = [
"lsp-server 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)", "lsp-server 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)",
"lsp-types", "lsp-types",
"mbe", "mbe",
"memchr",
"mimalloc", "mimalloc",
"nohash-hasher", "nohash-hasher",
"num_cpus", "num_cpus",

View file

@ -63,6 +63,7 @@ parser.workspace = true
toolchain.workspace = true toolchain.workspace = true
vfs-notify.workspace = true vfs-notify.workspace = true
vfs.workspace = true vfs.workspace = true
memchr = "2.7.1"
[target.'cfg(windows)'.dependencies] [target.'cfg(windows)'.dependencies]
winapi = "0.3.9" winapi = "0.3.9"

View file

@ -6,6 +6,7 @@
//! convert back to `\r\n` on the way out). //! convert back to `\r\n` on the way out).
use ide_db::line_index::WideEncoding; use ide_db::line_index::WideEncoding;
use memchr::memmem;
use triomphe::Arc; use triomphe::Arc;
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
@ -39,10 +40,10 @@ impl LineEndings {
let mut tail = buf.as_mut_slice(); let mut tail = buf.as_mut_slice();
let mut crlf_seen = false; 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 { loop {
let idx = match find_crlf(&tail[gap_len..]) { let idx = match finder.find(&tail[gap_len..]) {
None if crlf_seen => tail.len(), None if crlf_seen => tail.len(),
// SAFETY: buf is unchanged and therefore still contains utf8 data // SAFETY: buf is unchanged and therefore still contains utf8 data
None => return (unsafe { String::from_utf8_unchecked(buf) }, LineEndings::Unix), None => return (unsafe { String::from_utf8_unchecked(buf) }, LineEndings::Unix),