Skip to content

Commit

Permalink
Use smallvec for edges buffer in tree dfs
Browse files Browse the repository at this point in the history
Signed-off-by: ITesserakt <portyas85@mail.ru>
  • Loading branch information
ITesserakt committed Sep 7, 2024
1 parent 5698d0f commit 70a783b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
5 changes: 3 additions & 2 deletions crates/kodept-ast/src/graph/syntax_tree/dfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::visit_side::VisitSide;
use slotgraph::dag::NodeKey;
use std::collections::VecDeque;
use std::iter::FusedIterator;
use smallvec::SmallVec;

pub enum TraverseState {
DescendDeeper,
Expand All @@ -12,7 +13,7 @@ pub enum TraverseState {

pub struct DfsIter<'a> {
stack: VecDeque<(NodeKey, TraverseState)>,
edges_buffer: Vec<NodeKey>,
edges_buffer: SmallVec<[NodeKey; 2]>,
graph: &'a Graph,
}

Expand All @@ -23,7 +24,7 @@ impl<'a> DfsIter<'a> {

Self {
stack,
edges_buffer: vec![],
edges_buffer: SmallVec::new(),
graph,
}
}
Expand Down
8 changes: 4 additions & 4 deletions crates/kodept-ast/src/graph/syntax_tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl SyntaxTree<NoAccess> {
id: NodeId<T>,
token: &'b PermTkn,
tag: ChildTag,
) -> OptVec<&U>
) -> OptVec<&'b U>
where
AnyNode: ConvertibleToRef<U>,
{
Expand All @@ -82,23 +82,23 @@ impl SyntaxTree<NoAccess> {
.collect()
}

pub fn get<'b, T>(&'b self, id: NodeId<T>, token: &'b PermTkn) -> Option<&T>
pub fn get<'b, T>(&'b self, id: NodeId<T>, token: &'b PermTkn) -> Option<&'b T>
where
AnyNode: ConvertibleToRef<T>,
{
let node_ref = self.graph.node_weight(id.into())?;
node_ref.ro(token).try_as_ref()
}

pub fn get_mut<'b, T>(&'b self, id: NodeId<T>, token: &'b mut PermTkn) -> Option<&mut T>
pub fn get_mut<'b, T>(&'b self, id: NodeId<T>, token: &'b mut PermTkn) -> Option<&'b mut T>
where
AnyNode: ConvertibleToMut<T>,
{
let node_ref = self.graph.node_weight(id.into())?;
node_ref.rw(token).try_as_mut()
}

pub fn parent_of<'b, T>(&'b self, id: NodeId<T>, token: &'b PermTkn) -> Option<&AnyNode> {
pub fn parent_of<'b, T>(&'b self, id: NodeId<T>, token: &'b PermTkn) -> Option<&'b AnyNode> {
let parent_id = self.graph.parent_id(id.into())?;
Some(self.graph[parent_id].ro(token))
}
Expand Down
38 changes: 19 additions & 19 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 70a783b

Please sign in to comment.