Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

investigate skipping reverse NFA compilation if regex is anchored #1097

Open
BurntSushi opened this issue Oct 6, 2023 · 1 comment
Open

Comments

@BurntSushi
Copy link
Member

If the regex is anchored and a match is found in the forwards direction, then the reverse NFA is never needed to find the start position because the start position must necessarily be 0 as a result of the anchor.

See this comment for a little more context: #1095 (comment)

@BurntSushi
Copy link
Member Author

If someone wanted to work on this, it would be a pretty cool optimization to have.

I think the right way to do this is to add a new implementation of the internal Strategy trait for the meta regex engine:

pub(super) trait Strategy:
Debug + Send + Sync + RefUnwindSafe + UnwindSafe + 'static

And then create an instance of that implementation here:

It's important to avoid building a Core, since that will compile a reverse NFA. You'll want to short circuit a build what is essentially a near copy of Core, but a simpler version of it that never builds a reverse NFA. I suspect there may be a fair bit of code duplication. I'm not quite sure if it makes sense to try and reduce that duplication since I imagine it could complicate things.

The other unfortunate thing is that you won't be able to reuse, for example, the HybridEngine wrapper:

#[derive(Debug)]
pub(crate) struct HybridEngine(
#[cfg(feature = "hybrid")] hybrid::regex::Regex,
#[cfg(not(feature = "hybrid"))] (),
);

Since that wrapper unconditionally builds a reverse lazy DFA from a reverse NFA. Indeed, its constructor requires a reverse NFA. So we'll need a new wrapper for that (and the same applies to the fully compiled DFA wrapper too).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant