Skip to content

Commit 43146f6

Browse files
committed
Add a lazy DFA.
A lazy DFA is much faster than executing an NFA because it doesn't repeat the work of following epsilon transitions over and and over. Instead, it computes states during search and caches them for reuse. We avoid exponential state blow up by bounding the cache in size. When the DFA isn't powerful enough to fulfill the caller's request (e.g., return sub-capture locations), it still runs to find the boundaries of the match and then falls back to NFA execution on the matched region. The lazy DFA can otherwise execute on every regular expression *except* for regular expressions that contain word boundary assertions (`\b` or `\B`). (They are tricky to implement in the lazy DFA because they are Unicode aware and therefore require multi-byte look-behind/ahead.) The implementation in this PR is based on the implementation in Google's RE2 library. Adding a lazy DFA was a substantial change and required several modifications: 1. The compiler can now produce both Unicode based programs (still used by the NFA engines) and byte based programs (required by the lazy DFA, but possible to use in the NFA engines too). In byte based programs, UTF-8 decoding is built into the automaton. 2. A new `Exec` type was introduced to implement the logic for compiling and choosing the right engine to use on each search. 3. Prefix literal detection was rewritten to work on bytes. 4. Benchmarks were overhauled and new ones were added to more carefully track the impact of various optimizations. 5. A new `HACKING.md` guide has been added that gives a high-level design overview of this crate. Other changes in this commit include: 1. Protection against stack overflows. All places that once required recursion have now either acquired a bound or have been converted to using a stack on the heap. 2. Update the Aho-Corasick dependency, which includes `memchr2` and `memchr3` optimizations. 3. Add PCRE benchmarks using the Rust `pcre` bindings. Closes #66, #146.
1 parent 7120b80 commit 43146f6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+19445
-1955
lines changed

.travis.yml

+2-9
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,10 @@ script:
1414
- cargo doc --verbose --manifest-path=regex-syntax/Cargo.toml
1515
- if [ "$TRAVIS_RUST_VERSION" = "nightly" ]; then
1616
cargo test --verbose --features pattern;
17-
cargo bench --verbose;
17+
cargo bench --verbose --bench dynamic;
1818
travis_wait cargo test --verbose --manifest-path=regex_macros/Cargo.toml;
19-
travis_wait cargo bench --verbose --manifest-path=regex_macros/Cargo.toml;
19+
travis_wait cargo bench --manifest-path=regex_macros/Cargo.toml --verbose --bench native bench::;
2020
fi
21-
# - |
22-
# [ $TRAVIS_RUST_VERSION != nightly ] || (
23-
# cargo test --verbose --features pattern &&
24-
# cargo bench --verbose &&
25-
# travis_wait cargo test --verbose --manifest-path=regex_macros/Cargo.toml &&
26-
# travis_wait cargo bench --verbose --manifest-path=regex_macros/Cargo.toml
27-
# )
2821
after_success: |
2922
[ $TRAVIS_BRANCH = master ] &&
3023
[ $TRAVIS_PULL_REQUEST = false ] &&

Cargo.toml

+74-18
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,97 @@ repository = "https://github.com/rust-lang/regex"
88
documentation = "https://doc.rust-lang.org/regex"
99
homepage = "https://github.com/rust-lang/regex"
1010
description = """
11-
An implementation of regular expressions for Rust.
11+
An implementation of regular expressions for Rust. This implementation uses
12+
finite automata and guarantees linear time matching on all inputs.
1213
"""
1314

15+
[dependencies]
16+
# For very fast prefix literal matching.
17+
aho-corasick = "0.5"
18+
# For skipping along search text quickly when a leading byte is known.
19+
memchr = "0.1"
20+
# For parsing regular expressions.
21+
regex-syntax = { path = "regex-syntax", version = "0.2" }
22+
# For compiling UTF-8 decoding into automata.
23+
utf8-ranges = "0.1"
24+
25+
[dev-dependencies]
26+
# Because the pcre crate needs it for studying the regex.
27+
enum-set = "0.0.6"
28+
# To prevent the benchmarking harness from running setup code more than once.
29+
# Why? Because it takes too long.
30+
lazy_static = "0.1"
31+
# For running benchmarks.
32+
pcre = "0.2"
33+
# For generating random text to test/benchmark with.
34+
rand = "0.3"
35+
36+
[features]
37+
# Enable to use the unstable pattern traits defined in std.
38+
pattern = []
39+
40+
# Runs unit tests defined inside the regex package.
41+
# Generally these tests specific pieces of the regex implementation.
1442
[[test]]
1543
path = "src/lib.rs"
1644
name = "regex"
1745

46+
# Run the test suite on the default behavior of Regex::new.
47+
# This includes a mish mash of NFAs and DFAs, which are chosen automatically
48+
# based on the regex. We test both of the NFA implementations by forcing their
49+
# usage with the test definitions below. (We can't test the DFA implementations
50+
# in the same way since they can't be used for every regex tested.)
1851
[[test]]
19-
path = "regex_macros/tests/test_dynamic.rs"
52+
path = "tests/test_dynamic.rs"
2053
name = "dynamic"
2154

55+
# Run the test suite on the NFA algorithm over Unicode codepoints.
56+
[[test]]
57+
path = "tests/test_dynamic_nfa.rs"
58+
name = "dynamic-nfa"
59+
60+
# Run the test suite on the NFA algorithm over bytes.
2261
[[test]]
23-
path = "regex_macros/tests/test_dynamic_nfa.rs"
24-
name = "dynamic_nfa"
62+
path = "tests/test_dynamic_nfa_bytes.rs"
63+
name = "dynamic-nfa-bytes"
2564

65+
# Run the test suite on the backtracking engine over Unicode codepoints.
2666
[[test]]
27-
path = "regex_macros/tests/test_dynamic_backtrack.rs"
28-
name = "dynamic_backtrack"
67+
path = "tests/test_dynamic_backtrack.rs"
68+
name = "dynamic-backtrack"
2969

70+
# Run the test suite on the backtracking engine over bytes.
71+
[[test]]
72+
path = "tests/test_dynamic_backtrack_bytes.rs"
73+
name = "dynamic-backtrack-bytes"
74+
75+
# Run the benchmarks on the default behavior of Regex::new.
76+
#
77+
# N.B. These benchmarks were originally taken from Russ Cox.
3078
[[bench]]
31-
name = "all"
32-
path = "regex_macros/benches/bench_dynamic.rs"
79+
name = "dynamic"
80+
path = "benches/bench_dynamic.rs"
3381
test = false
3482
bench = true
3583

36-
[dependencies]
37-
aho-corasick = "0.4"
38-
memchr = "0.1"
39-
regex-syntax = { path = "regex-syntax", version = "0.2" }
40-
41-
[dev-dependencies]
42-
rand = "0.3"
84+
# Run the benchmarks on the NFA algorithm. We avoid chasing other permutations.
85+
#
86+
# N.B. These can take a *loong* time to run.
87+
[[bench]]
88+
name = "dynamic-nfa"
89+
path = "benches/bench_dynamic_nfa.rs"
90+
test = false
91+
bench = true
4392

44-
[features]
45-
pattern = []
93+
# Run the benchmarks on PCRE.
94+
[[bench]]
95+
name = "pcre"
96+
path = "benches/bench_pcre.rs"
97+
test = false
98+
bench = true
4699

47100
[profile.bench]
48-
lto = true
101+
debug = true
102+
103+
[profile.test]
104+
debug = true

0 commit comments

Comments
 (0)