Skip to content

Commit 2aa1727

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 2aa1727

Some content is hidden

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

68 files changed

+19475
-1957
lines changed

.travis.yml

+4-10
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,12 @@ script:
1313
- cargo test --verbose --manifest-path=regex-syntax/Cargo.toml
1414
- cargo doc --verbose --manifest-path=regex-syntax/Cargo.toml
1515
- if [ "$TRAVIS_RUST_VERSION" = "nightly" ]; then
16-
cargo test --verbose --features pattern;
17-
cargo bench --verbose;
16+
travis_wait cargo test --verbose --features pattern;
17+
travis_wait cargo bench --verbose --bench dynamic;
18+
travis_wait cargo bench --manifest-path=regex-pcre-benchmark/Cargo.toml --verbose
1819
travis_wait cargo test --verbose --manifest-path=regex_macros/Cargo.toml;
19-
travis_wait cargo bench --verbose --manifest-path=regex_macros/Cargo.toml;
20+
travis_wait cargo bench --manifest-path=regex_macros/Cargo.toml --verbose --bench native bench::;
2021
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-
# )
2822
after_success: |
2923
[ $TRAVIS_BRANCH = master ] &&
3024
[ $TRAVIS_PULL_REQUEST = false ] &&

Cargo.toml

+64-19
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,86 @@ 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+
# To prevent the benchmarking harness from running setup code more than once.
27+
# Why? Because it takes too long.
28+
lazy_static = "0.1"
29+
# For generating random text to test/benchmark with.
30+
rand = "0.3"
31+
32+
[features]
33+
# Enable to use the unstable pattern traits defined in std.
34+
pattern = []
35+
36+
# Runs unit tests defined inside the regex package.
37+
# Generally these tests specific pieces of the regex implementation.
1438
[[test]]
1539
path = "src/lib.rs"
1640
name = "regex"
1741

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

51+
# Run the test suite on the NFA algorithm over Unicode codepoints.
52+
[[test]]
53+
path = "tests/test_dynamic_nfa.rs"
54+
name = "dynamic-nfa"
55+
56+
# Run the test suite on the NFA algorithm over bytes.
57+
[[test]]
58+
path = "tests/test_dynamic_nfa_bytes.rs"
59+
name = "dynamic-nfa-bytes"
60+
61+
# Run the test suite on the backtracking engine over Unicode codepoints.
2262
[[test]]
23-
path = "regex_macros/tests/test_dynamic_nfa.rs"
24-
name = "dynamic_nfa"
63+
path = "tests/test_dynamic_backtrack.rs"
64+
name = "dynamic-backtrack"
2565

66+
# Run the test suite on the backtracking engine over bytes.
2667
[[test]]
27-
path = "regex_macros/tests/test_dynamic_backtrack.rs"
28-
name = "dynamic_backtrack"
68+
path = "tests/test_dynamic_backtrack_bytes.rs"
69+
name = "dynamic-backtrack-bytes"
2970

71+
# Run the benchmarks on the default behavior of Regex::new.
72+
#
73+
# N.B. These benchmarks were originally taken from Russ Cox.
3074
[[bench]]
31-
name = "all"
32-
path = "regex_macros/benches/bench_dynamic.rs"
75+
name = "dynamic"
76+
path = "benches/bench_dynamic.rs"
3377
test = false
3478
bench = true
3579

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"
43-
44-
[features]
45-
pattern = []
80+
# Run the benchmarks on the NFA algorithm. We avoid chasing other permutations.
81+
#
82+
# N.B. These can take a *loong* time to run.
83+
[[bench]]
84+
name = "dynamic-nfa"
85+
path = "benches/bench_dynamic_nfa.rs"
86+
test = false
87+
bench = true
4688

4789
[profile.bench]
48-
lto = true
90+
debug = true
91+
92+
[profile.test]
93+
debug = true

0 commit comments

Comments
 (0)