Skip to content

Commit

Permalink
automata/meta: tweak reverse suffix prefilter strategy
Browse files Browse the repository at this point in the history
Previously, we were only use the reverse suffix optimization if it found
a non-empty longest common suffix *and* if the prefilter thought itself
was fast. This was a heuristic used in the old regex crate before we
grew the "is prefilter fast" heuristic. We change this optimization to
just use the "is prefilter fast" heuristic instead of requiring a
non-empty longest common suffix.

This is, after all, what the inner literal optimization does. And in the
inner literal case, one should probably be even more conservative
because of the extra work that needs to be done. So if things are going
okay with the inner literal optimization, then we should be fine with
the reverse suffix optimization doing essentially the same thing.
  • Loading branch information
BurntSushi committed Oct 14, 2023
1 parent 04f5d7b commit 8a8d599
Showing 1 changed file with 12 additions and 25 deletions.
37 changes: 12 additions & 25 deletions regex-automata/src/meta/strategy.rs
Expand Up @@ -1167,34 +1167,21 @@ impl ReverseSuffix {
return Err(core);
}
let kind = core.info.config().get_match_kind();
let suffixes = crate::util::prefilter::suffixes(kind, hirs);
let lcs = match suffixes.longest_common_suffix() {
None => {
debug!(
"skipping reverse suffix optimization because \
a longest common suffix could not be found",
);
return Err(core);
}
Some(lcs) if lcs.is_empty() => {
debug!(
"skipping reverse suffix optimization because \
the longest common suffix is the empty string",
);
return Err(core);
}
Some(lcs) => lcs,
let suffixseq = crate::util::prefilter::suffixes(kind, hirs);
let Some(suffixes) = suffixseq.literals() else {
debug!(
"skipping reverse suffix optimization because \
the extract suffix sequence is not finite",
);
return Err(core);
};
let pre = match Prefilter::new(kind, &[lcs]) {
Some(pre) => pre,
None => {
debug!(
"skipping reverse suffix optimization because \
let Some(pre) = Prefilter::new(kind, suffixes) else {
debug!(
"skipping reverse suffix optimization because \
a prefilter could not be constructed from the \
longest common suffix",
);
return Err(core);
}
);
return Err(core);
};
if !pre.is_fast() {
debug!(
Expand Down

0 comments on commit 8a8d599

Please sign in to comment.