From 8a8d599f9d2f2d78e9ad84e4084788c2d563afa5 Mon Sep 17 00:00:00 2001 From: Andrew Gallant Date: Fri, 13 Oct 2023 14:52:09 -0400 Subject: [PATCH] automata/meta: tweak reverse suffix prefilter strategy 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. --- regex-automata/src/meta/strategy.rs | 37 ++++++++++------------------- 1 file changed, 12 insertions(+), 25 deletions(-) diff --git a/regex-automata/src/meta/strategy.rs b/regex-automata/src/meta/strategy.rs index 5b96d888a..4cb3b29b9 100644 --- a/regex-automata/src/meta/strategy.rs +++ b/regex-automata/src/meta/strategy.rs @@ -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!(