internal: speedup LineEndings calculation using 'memchr'

This commit is contained in:
roife 2024-01-18 17:03:29 +08:00
parent 9d9b34354d
commit 04ce4ce440
3 changed files with 7 additions and 4 deletions

5
Cargo.lock generated
View file

@ -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",

View file

@ -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"

View file

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