Enum regex_automata::util::prefilter::Choice
source · pub(crate) enum Choice {
Memchr(Memchr),
Memchr2(Memchr2),
Memchr3(Memchr3),
Memmem(Memmem),
Teddy(Teddy),
ByteSet(ByteSet),
AhoCorasick(AhoCorasick),
}
Expand description
A type that encapsulates the selection of a prefilter algorithm from a sequence of needles.
The existence of this type is a little tricky, because we don’t (currently)
use it for performing a search. Instead, we really only consume it by
converting the underlying prefilter into a trait object, whether that be
dyn PrefilterI
or dyn Strategy
(for the meta regex engine). In order
to avoid re-copying the prefilter selection logic, we isolate it here, and
then force anything downstream that wants to convert it to a trait object
to do trivial case analysis on it.
One wonders whether we should use an enum instead of a trait object. At time of writing, I chose trait objects based on instinct because 1) I knew I wasn’t going to inline anything and 2) there would potentially be many different choices. However, as of time of writing, I haven’t actually compared the trait object approach to the enum approach. That probably should be litigated, but I ran out of steam.
Note that if the alloc
feature is disabled, then values of this type
are (and should) never be constructed. Also, in practice, for any of the
prefilters to be selected, you’ll need at least one of the perf-literal-*
features enabled.
Variants§
Memchr(Memchr)
Memchr2(Memchr2)
Memchr3(Memchr3)
Memmem(Memmem)
Teddy(Teddy)
ByteSet(ByteSet)
AhoCorasick(AhoCorasick)
Implementations§
source§impl Choice
impl Choice
sourcepub(crate) fn new<B: AsRef<[u8]>>(
kind: MatchKind,
needles: &[B],
) -> Option<Choice>
pub(crate) fn new<B: AsRef<[u8]>>( kind: MatchKind, needles: &[B], ) -> Option<Choice>
Select what is believed to be the best prefilter algorithm for the match semantics and sequence of needles given.
This selection algorithm uses the needles as given without any
modification. For example, if [bar]
is given, then this doesn’t
try to select memchr
for b
. Instead, it would select memmem
for bar
. If callers would want memchr
selected for [bar]
, then
callers should massages the literals themselves. That is, callers are
responsible for heuristics surrounding which sequence of literals is
best.
What this selection algorithm does is attempt to use the fastest
prefilter that works for the literals given. So if [a, b]
, is given,
then memchr2
is selected.
Of course, which prefilter is selected is also subject to what
is available. For example, if alloc
isn’t enabled, then
that limits which prefilters can be selected. Similarly, if
perf-literal-substring
isn’t enabled, then nothing from the memchr
crate can be returned.