Skip to content

Commit a807d48

Browse files
Refactor char lookup in LSP to O(1) byte slices
Co-authored-by: Tcode-Motion <188012755+Tcode-Motion@users.noreply.github.com>
1 parent 0a6bbb4 commit a807d48

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

‎tools/lsp/benches/bench.rs‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use criterion::{black_box, criterion_group, criterion_main, Criterion};
1+
use criterion::{criterion_group, criterion_main, Criterion};
22

33
fn find_boundary_slow(line: &str, word: &str) -> usize {
44
let mut count = 0;
@@ -13,8 +13,8 @@ fn find_boundary_slow(line: &str, word: &str) -> usize {
1313
};
1414
let char_after = line.chars().nth(actual_pos + word_len);
1515

16-
let is_boundary_before = char_before.map_or(true, |c| !c.is_alphanumeric() && c != '_');
17-
let is_boundary_after = char_after.map_or(true, |c| !c.is_alphanumeric() && c != '_');
16+
let is_boundary_before = char_before.is_none_or(|c| !c.is_alphanumeric() && c != '_');
17+
let is_boundary_after = char_after.is_none_or(|c| !c.is_alphanumeric() && c != '_');
1818

1919
if is_boundary_before && is_boundary_after {
2020
count += 1;
@@ -37,8 +37,8 @@ fn find_boundary_fast(line: &str, word: &str) -> usize {
3737
};
3838
let char_after = line[actual_pos + word_len..].chars().next();
3939

40-
let is_boundary_before = char_before.map_or(true, |c| !c.is_alphanumeric() && c != '_');
41-
let is_boundary_after = char_after.map_or(true, |c| !c.is_alphanumeric() && c != '_');
40+
let is_boundary_before = char_before.is_none_or(|c| !c.is_alphanumeric() && c != '_');
41+
let is_boundary_after = char_after.is_none_or(|c| !c.is_alphanumeric() && c != '_');
4242

4343
if is_boundary_before && is_boundary_after {
4444
count += 1;
@@ -52,8 +52,12 @@ fn criterion_benchmark(c: &mut Criterion) {
5252
let long_line = "let abc = 123; ".repeat(1000);
5353
let word = "abc";
5454

55-
c.bench_function("slow", |b| b.iter(|| find_boundary_slow(black_box(&long_line), black_box(word))));
56-
c.bench_function("fast", |b| b.iter(|| find_boundary_fast(black_box(&long_line), black_box(word))));
55+
c.bench_function("slow", |b| {
56+
b.iter(|| find_boundary_slow(std::hint::black_box(&long_line), std::hint::black_box(word)))
57+
});
58+
c.bench_function("fast", |b| {
59+
b.iter(|| find_boundary_fast(std::hint::black_box(&long_line), std::hint::black_box(word)))
60+
});
5761
}
5862

5963
criterion_group!(benches, criterion_benchmark);

0 commit comments

Comments
 (0)