mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-29 13:25:09 +00:00
Remove allocations from LCA
I haven't actually profiled this, but not allocating a hash map (or anything, really) seems like a good idea
This commit is contained in:
parent
1647d5ac60
commit
738fc79c92
1 changed files with 13 additions and 3 deletions
|
@ -7,7 +7,7 @@ use std::{
|
||||||
|
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use ra_text_edit::TextEditBuilder;
|
use ra_text_edit::TextEditBuilder;
|
||||||
use rustc_hash::{FxHashMap, FxHashSet};
|
use rustc_hash::FxHashMap;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
AstNode, Direction, NodeOrToken, SyntaxElement, SyntaxNode, SyntaxNodePtr, SyntaxToken,
|
AstNode, Direction, NodeOrToken, SyntaxElement, SyntaxNode, SyntaxNodePtr, SyntaxToken,
|
||||||
|
@ -72,8 +72,18 @@ pub fn find_covering_element(root: &SyntaxNode, range: TextRange) -> SyntaxEleme
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn least_common_ancestor(u: &SyntaxNode, v: &SyntaxNode) -> Option<SyntaxNode> {
|
pub fn least_common_ancestor(u: &SyntaxNode, v: &SyntaxNode) -> Option<SyntaxNode> {
|
||||||
let u_ancestors = u.ancestors().collect::<FxHashSet<SyntaxNode>>();
|
if u == v {
|
||||||
v.ancestors().find(|it| u_ancestors.contains(it))
|
return Some(u.clone());
|
||||||
|
}
|
||||||
|
|
||||||
|
let u_depth = u.ancestors().count();
|
||||||
|
let v_depth = v.ancestors().count();
|
||||||
|
let keep = u_depth.min(v_depth);
|
||||||
|
|
||||||
|
let u_candidates = u.ancestors().skip(u_depth - keep);
|
||||||
|
let v_canidates = v.ancestors().skip(v_depth - keep);
|
||||||
|
let (res, _) = u_candidates.zip(v_canidates).find(|(x, y)| x == y)?;
|
||||||
|
Some(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn neighbor<T: AstNode>(me: &T, direction: Direction) -> Option<T> {
|
pub fn neighbor<T: AstNode>(me: &T, direction: Direction) -> Option<T> {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue