Skip to content

Commit

Permalink
Auto merge of #16395 - roife:internal/speed-up-LineEndings-with-memch…
Browse files Browse the repository at this point in the history
…r, r=Veykril

internal: speedup LineEndings calculation using 'memchr'

See #13538
  • Loading branch information
bors committed Jan 18, 2024
2 parents 9d9b343 + 04ce4ce commit 8bb500a
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/rust-analyzer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 3 additions & 2 deletions crates/rust-analyzer/src/line_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit 8bb500a

Please sign in to comment.