Skip to content

Commit 28b6838

Browse files
committed
Auto merge of #17908 - ChayimFriedman2:usages-word-boundaries, r=Veykril
Test for word boundary in `FindUsages` This speeds up short identifiers search significantly, while unlikely to have an effect on long identifiers (the analysis takes much longer than some character comparison). Tested by finding all references to `eq()` (from `PartialEq`) in the rust-analyzer repo. Total time went down from 100s to 10s (a 10x reduction!). Feel free to close this if you consider this a non-issue, as most short identifiers are local.
2 parents fc36e0c + 1a31fe2 commit 28b6838

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

crates/ide-db/src/search.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,20 @@ impl<'a> FindUsages<'a> {
503503
if !search_range.contains_inclusive(offset) {
504504
return None;
505505
}
506+
// If this is not a word boundary, that means this is only part of an identifier,
507+
// so it can't be what we're looking for.
508+
// This speeds up short identifiers significantly.
509+
if text[..idx]
510+
.chars()
511+
.next_back()
512+
.is_some_and(|ch| matches!(ch, 'A'..='Z' | 'a'..='z' | '_'))
513+
|| text[idx + finder.needle().len()..]
514+
.chars()
515+
.next()
516+
.is_some_and(|ch| matches!(ch, 'A'..='Z' | 'a'..='z' | '_' | '0'..='9'))
517+
{
518+
return None;
519+
}
506520
Some(offset)
507521
})
508522
}

0 commit comments

Comments
 (0)