Skip to content

Commit

Permalink
Adds flag to measure performance.
Browse files Browse the repository at this point in the history
  • Loading branch information
gabomatute committed Sep 14, 2023
1 parent ce38632 commit e284af4
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ struct Args {
language: st::lang::Select,
query: String,
file: std::path::PathBuf,
#[arg(long)]
metrics: bool,
}

fn main() {
let timer = std::time::Instant::now();

let args = Args::parse();

let language = args.language.load();
Expand All @@ -24,6 +28,8 @@ fn main() {
let document =
st::document::new::<&str, Tree>(&text, Tree::new(&text, &language, Default::default()));

let parsing = timer.elapsed();

for m in pattern.find_iter(document.walk()) {
let start = m.start.node().start_position();
let end = m.end.node().end_position();
Expand All @@ -38,4 +44,20 @@ fn main() {
end.column + 1
);
}

let searching = timer.elapsed();

if args.metrics {
let (n, d) = document.dim();
let (k, h) = (pattern.len(), pattern.holes());
eprintln!(
"{},{},{},{},{:?},{:?}",
n, // tree size
d, // tree depth
k, // query length
h, // query hole count
parsing.as_millis(),
(searching - parsing).as_millis(),
);
}
}
11 changes: 11 additions & 0 deletions src/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,17 @@ impl<T> Pattern<T> {
None => Err(start),
}
}

pub fn len(&self) -> usize {
self.sequence.len()
}

pub fn holes(&self) -> usize {
self.sequence
.iter()
.filter(|t| matches!(t, Token::Subtree | Token::Siblings))
.count()
}
}

impl<T> FromIterator<Token<T>> for Pattern<T> {
Expand Down
32 changes: 32 additions & 0 deletions src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,38 @@ pub trait Subtree {

#[must_use]
fn walk(self) -> Self::Cursor;

#[must_use]
fn dim(self) -> (usize, usize)
where
Self: Sized,
{
let (mut size, mut depth) = (1, 1);

let mut cursor = self.walk();
let mut level = 1;

loop {
while cursor.goto_first_child() {
size += 1;
level += 1;
}

depth = std::cmp::max(depth, level);

loop {
if cursor.goto_next_sibling() {
size += 1;
break;
} else if cursor.goto_parent() {
level -= 1;
continue;
}
assert_eq!(level, 1);
return (size, depth)
}
}
}
}

pub trait Traverse {
Expand Down

0 comments on commit e284af4

Please sign in to comment.