mirror of
https://github.com/neovim/neovim
synced 2025-07-16 01:01:49 +00:00
perf(treesitter): use child_containing_descendant()
in is_ancestor()
**Problem:** `is_ancestor()` uses a slow, bottom-up parent lookup which has performance pitfalls detailed in #28512. **Solution:** Take `is_ancestor()` from $O(n^2)$ to $O(n)$ by incorporating the use of the `child_containing_descendant()` function
This commit is contained in:
committed by
Christian Clason
parent
921dc22fc0
commit
64847fbdc9
@ -159,16 +159,8 @@ function M.is_ancestor(dest, source)
|
||||
return false
|
||||
end
|
||||
|
||||
local current = source ---@type TSNode?
|
||||
while current ~= nil do
|
||||
if current == dest then
|
||||
return true
|
||||
end
|
||||
|
||||
current = current:parent()
|
||||
end
|
||||
|
||||
return false
|
||||
-- child_containing_descendant returns nil if dest is a direct parent
|
||||
return source:parent() == dest or dest:child_containing_descendant(source) ~= nil
|
||||
end
|
||||
|
||||
--- Returns the node's range or an unpacked range table
|
||||
|
Reference in New Issue
Block a user