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

perf: Improve dfa matching #511

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ dist: trusty
sudo: required
language: rust
rust:
- 1.20.0
- 1.21.0
- stable
- beta
- nightly
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ recommended for general use.

### Minimum Rust version policy

This crate's minimum supported `rustc` version is `1.20.0`.
This crate's minimum supported `rustc` version is `1.21.0`.

The current **tentative** policy is that the minimum Rust version required
to use this crate can be increased in minor version updates. For example, if
Expand Down
54 changes: 52 additions & 2 deletions bench/src/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ pub use ffi::re2::Regex;
#[cfg(feature = "re-dphobos")]
pub use ffi::d_phobos::Regex;
#[cfg(feature = "re-rust")]
pub use regex::Regex;
pub use regex::{Regex, RegexSet};
#[cfg(feature = "re-rust-bytes")]
pub use regex::bytes::Regex;
pub use regex::bytes::{Regex, RegexSet};
#[cfg(feature = "re-tcl")]
pub use ffi::tcl::Regex;

Expand Down Expand Up @@ -272,6 +272,56 @@ macro_rules! bench_captures {
}
}

macro_rules! bench_is_match_set {
($name:ident, $is_match:expr, $re:expr, $haystack:expr) => {
#[bench]
fn $name(b: &mut Bencher) {
use std::sync::Mutex;
lazy_static! {
static ref RE: Mutex<RegexSet> = Mutex::new($re);
static ref TEXT: Mutex<Text> = Mutex::new(text!($haystack));
};
let re = RE.lock().unwrap();
let text = TEXT.lock().unwrap();
b.bytes = text.len() as u64;
b.iter(|| {
if re.is_match(&text) != $is_match {
if $is_match {
panic!("expected match, got not match");
} else {
panic!("expected no match, got match");
}
}
});
}
}
}

macro_rules! bench_matches_set {
($name:ident, $is_match:expr, $re:expr, $haystack:expr) => {
#[bench]
fn $name(b: &mut Bencher) {
use std::sync::Mutex;
lazy_static! {
static ref RE: Mutex<RegexSet> = Mutex::new($re);
static ref TEXT: Mutex<Text> = Mutex::new(text!($haystack));
};
let re = RE.lock().unwrap();
let text = TEXT.lock().unwrap();
b.bytes = text.len() as u64;
b.iter(|| {
if re.matches(&text).matched_any() != $is_match {
if $is_match {
panic!("expected match, got not match");
} else {
panic!("expected no match, got match");
}
}
});
}
}
}

mod ffi;
mod misc;
mod regexdna;
Expand Down
20 changes: 20 additions & 0 deletions bench/src/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ use std::iter::repeat;

use test::Bencher;

#[cfg(any(feature = "re-rust", feature = "re-rust-bytes"))]
use RegexSet;
use {Regex, Text};

#[cfg(not(feature = "re-onig"))]
Expand Down Expand Up @@ -278,3 +280,21 @@ bench_captures!(short_haystack_1000000x,
repeat("aaaa").take(1000000).collect::<String>(),
repeat("dddd").take(1000000).collect::<String>(),
));

#[cfg(any(feature = "re-rust", feature = "re-rust-bytes"))]
bench_is_match_set!(is_match_set,
true,
RegexSet::new(vec!["aaaaaaaaaaaaaaaaaaa", "abc579", "def.+", "e24fg", "a.*2c", "23."]).unwrap(),
format!("{}a482c{}",
repeat('a').take(10).collect::<String>(),
repeat('b').take(10).collect::<String>())
);

#[cfg(any(feature = "re-rust", feature = "re-rust-bytes"))]
bench_matches_set!(matches_set,
true,
RegexSet::new(vec!["aaaaaaaaaaaaaaaaaaa", "abc579", "def.+", "e24fg", "a.*2c", "23."]).unwrap(),
format!("{}a482c{}",
repeat('a').take(10).collect::<String>(),
repeat('b').take(10).collect::<String>())
);
2 changes: 1 addition & 1 deletion ci/script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# This is the main CI script for testing the regex crate and its sub-crates.

set -ex
MSRV="1.20.0"
MSRV="1.21.0"

# If we're building on 1.20, then lazy_static 1.2 will fail to build since it
# updated its MSRV to 1.24.1. In this case, we force the use of lazy_static 1.1
Expand Down
Loading