Skip to content

Commit

Permalink
reduce number of allocations in deriv()
Browse files Browse the repository at this point in the history
  • Loading branch information
mmoskal committed Dec 18, 2024
1 parent bded1a1 commit a629ecc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
4 changes: 3 additions & 1 deletion src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,8 @@ impl ExprSet {
}

let mut todo = vec![r];
let mut mapped = Vec::with_capacity(128);

while let Some(r) = todo.last() {
let r = *r;
let idx = mk_key(r);
Expand All @@ -487,7 +489,7 @@ impl ExprSet {
let is_concat = concat_nullable_check && matches!(e, Expr::Concat(_, _));
let todo_len = todo.len();
let eargs = e.args();
let mut mapped = Vec::with_capacity(eargs.len());
mapped.clear();
for a in eargs {
let a = *a;
let brk = is_concat && !self.is_nullable(a);
Expand Down
21 changes: 14 additions & 7 deletions src/deriv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ impl DerivCache {
}
}

let mut tmp = vec![];
let mut or_branches = vec![];

// regular path
exprs.map(
r,
Expand Down Expand Up @@ -79,15 +82,19 @@ impl DerivCache {
max.saturating_sub(1)
};
let tail = exprs.mk_repeat(e, min.saturating_sub(1), max);
exprs.mk_concat(&mut vec![deriv[0], tail])
tmp.clear();
tmp.push(deriv[0]);
tmp.push(tail);
exprs.mk_concat(&mut tmp)
}
Expr::Concat(_, args) => {
let mut or_branches = vec![];
let mut args = args.to_vec();
for i in 0..args.len() {
let nullable = exprs.is_nullable(args[i]);
args[i] = deriv[i];
or_branches.push(exprs.mk_concat(&mut args[i..].to_vec()));
or_branches.clear();
tmp.clear();
tmp.extend_from_slice(args);
for i in 0..tmp.len() {
let nullable = exprs.is_nullable(tmp[i]);
tmp[i] = deriv[i];
or_branches.push(exprs.mk_concat(&mut tmp[i..].to_vec()));
if !nullable {
break;
}
Expand Down

0 comments on commit a629ecc

Please sign in to comment.