Skip to content

Commit fd8a662

Browse files
Implement matchall speedup for meta-engine
1 parent 2419b12 commit fd8a662

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

regex-automata/src/meta/regex.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,8 @@ impl Regex {
611611
&'r self,
612612
input: I,
613613
) -> FindMatches<'r, 'h> {
614-
let cache = self.pool.get();
614+
let mut cache = self.pool.get();
615+
cache.keep_lookaround_state(true);
615616
let it = iter::Searcher::new(input.into());
616617
FindMatches { re: self, cache, it }
617618
}
@@ -652,7 +653,8 @@ impl Regex {
652653
&'r self,
653654
input: I,
654655
) -> CapturesMatches<'r, 'h> {
655-
let cache = self.pool.get();
656+
let mut cache = self.pool.get();
657+
cache.keep_lookaround_state(true);
656658
let caps = self.create_captures();
657659
let it = iter::Searcher::new(input.into());
658660
CapturesMatches { re: self, cache, caps, it }
@@ -2076,7 +2078,11 @@ impl<'r, 'h> Iterator for FindMatches<'r, 'h> {
20762078
#[inline]
20772079
fn next(&mut self) -> Option<Match> {
20782080
let FindMatches { re, ref mut cache, ref mut it } = *self;
2079-
it.advance(|input| Ok(re.search_with(cache, input)))
2081+
let result = it.advance(|input| Ok(re.search_with(cache, input)));
2082+
if result.is_none() {
2083+
cache.keep_lookaround_state(false);
2084+
}
2085+
result
20802086
}
20812087

20822088
#[inline]
@@ -2149,6 +2155,7 @@ impl<'r, 'h> Iterator for CapturesMatches<'r, 'h> {
21492155
if caps.is_match() {
21502156
Some(caps.clone())
21512157
} else {
2158+
cache.keep_lookaround_state(false);
21522159
None
21532160
}
21542161
}
@@ -2385,6 +2392,19 @@ impl Cache {
23852392
re.imp.strat.reset_cache(self)
23862393
}
23872394

2395+
/// Set this cache to keep the state of look-behind assertions upon a
2396+
/// match being found.
2397+
///
2398+
/// This must only be called with a value of `true` when a new search is
2399+
/// started at the end of a previously found match, otherwise the result
2400+
/// of any search after this call will most likely be wrong.
2401+
///
2402+
/// Calling this function with a value of `false` will clear any previously
2403+
/// stored look-behind state.
2404+
pub fn keep_lookaround_state(&mut self, keep: bool) {
2405+
self.pikevm.keep_lookaround_state(keep);
2406+
}
2407+
23882408
/// Returns the heap memory usage, in bytes, of this cache.
23892409
///
23902410
/// This does **not** include the stack size used up by this cache. To

regex-automata/src/meta/wrappers.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,12 @@ impl PikeVMCache {
133133
PikeVMCache(Some(builder.get().0.create_cache()))
134134
}
135135

136+
pub(crate) fn keep_lookaround_state(&mut self, keep: bool) {
137+
if let Some(cache) = self.0.as_mut() {
138+
cache.keep_lookaround_state(keep);
139+
}
140+
}
141+
136142
pub(crate) fn reset(&mut self, builder: &PikeVM) {
137143
self.0.as_mut().unwrap().reset(&builder.get().0);
138144
}

0 commit comments

Comments
 (0)