Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
199 changes: 198 additions & 1 deletion Cargo.lock

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

7 changes: 7 additions & 0 deletions tools/lsp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,10 @@ tower-lsp = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
tokio = { version = "1", features = ["full"] }

[dev-dependencies]
criterion = "0.8.2"

[[bench]]
name = "bench"
harness = false
64 changes: 64 additions & 0 deletions tools/lsp/benches/bench.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use criterion::{criterion_group, criterion_main, Criterion};

fn find_boundary_slow(line: &str, word: &str) -> usize {
let mut count = 0;
let word_len = word.len();
let mut start_pos = 0;
while let Some(pos_in_line) = line[start_pos..].find(word) {
let actual_pos = start_pos + pos_in_line;
let char_before = if actual_pos > 0 {
line.chars().nth(actual_pos - 1)
} else {
None
};
let char_after = line.chars().nth(actual_pos + word_len);

let is_boundary_before = char_before.is_none_or(|c| !c.is_alphanumeric() && c != '_');
let is_boundary_after = char_after.is_none_or(|c| !c.is_alphanumeric() && c != '_');

if is_boundary_before && is_boundary_after {
count += 1;
}
start_pos = actual_pos + word_len;
}
count
}

fn find_boundary_fast(line: &str, word: &str) -> usize {
let mut count = 0;
let word_len = word.len();
let mut start_pos = 0;
while let Some(pos_in_line) = line[start_pos..].find(word) {
let actual_pos = start_pos + pos_in_line;
let char_before = if actual_pos > 0 {
line[..actual_pos].chars().next_back()
} else {
None
};
let char_after = line[actual_pos + word_len..].chars().next();

let is_boundary_before = char_before.is_none_or(|c| !c.is_alphanumeric() && c != '_');
let is_boundary_after = char_after.is_none_or(|c| !c.is_alphanumeric() && c != '_');

if is_boundary_before && is_boundary_after {
count += 1;
}
start_pos = actual_pos + word_len;
}
count
}

fn criterion_benchmark(c: &mut Criterion) {
let long_line = "let abc = 123; ".repeat(1000);
let word = "abc";

c.bench_function("slow", |b| {
b.iter(|| find_boundary_slow(std::hint::black_box(&long_line), std::hint::black_box(word)))
});
c.bench_function("fast", |b| {
b.iter(|| find_boundary_fast(std::hint::black_box(&long_line), std::hint::black_box(word)))
});
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
Loading
Loading