mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
refactor(semantic): AstNodeParentIter fetch nodes lazily (#4533)
Refactor `AstNodeParentIter`, which is used to iterate down node ancestry chain. Fetch `AstNode` objects lazily, only when they're required by a `next()` call. If caller doesn't iterate all the way down the chain, likely this will result in 1 less array lookup.
This commit is contained in:
parent
0870ee1f9f
commit
d6974d4ff7
1 changed files with 9 additions and 7 deletions
|
|
@ -94,9 +94,9 @@ impl<'a> AstNodes<'a> {
|
||||||
///
|
///
|
||||||
/// The first node produced by this iterator is the first parent of the node
|
/// The first node produced by this iterator is the first parent of the node
|
||||||
/// pointed to by `node_id`. The last node will usually be a `Program`.
|
/// pointed to by `node_id`. The last node will usually be a `Program`.
|
||||||
|
#[inline]
|
||||||
pub fn iter_parents(&self, node_id: AstNodeId) -> impl Iterator<Item = &AstNode<'a>> + '_ {
|
pub fn iter_parents(&self, node_id: AstNodeId) -> impl Iterator<Item = &AstNode<'a>> + '_ {
|
||||||
let curr = Some(self.get_node(node_id));
|
AstNodeParentIter { current_node_id: Some(node_id), nodes: self }
|
||||||
AstNodeParentIter { curr, nodes: self }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
|
@ -197,7 +197,7 @@ impl<'a> AstNodes<'a> {
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct AstNodeParentIter<'s, 'a> {
|
pub struct AstNodeParentIter<'s, 'a> {
|
||||||
curr: Option<&'s AstNode<'a>>,
|
current_node_id: Option<AstNodeId>,
|
||||||
nodes: &'s AstNodes<'a>,
|
nodes: &'s AstNodes<'a>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -205,9 +205,11 @@ impl<'s, 'a> Iterator for AstNodeParentIter<'s, 'a> {
|
||||||
type Item = &'s AstNode<'a>;
|
type Item = &'s AstNode<'a>;
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
let next = self.curr;
|
if let Some(node_id) = self.current_node_id {
|
||||||
self.curr = self.curr.and_then(|curr| self.nodes.parent_node(curr.id()));
|
self.current_node_id = self.nodes.parent_ids[node_id];
|
||||||
|
Some(self.nodes.get_node(node_id))
|
||||||
next
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue