I'm working on some application where performance is critical and i'm looking for opportunities to improve it. The app uses regular expressions extensively (regex crate), eg.:
use regex::Regex;
...
let Regex = Regex::new(r###"[a-z0-9%]{3,}"###).unwrap();
Is there any opportunity to save a precompiled Regex instance to a buffer (think Vec<u8>) and load later pre-compiled to avoid multiple compilations (can't see anything similar in the docs)?
PS. I already do it lazily with:
lazy_static! {
static ref REGEX2: Regex = Regex::new(r###"[a-z0-9%]{3,}"###).unwrap();
}
trying to save some CPU cycles if having multiple invocations (but that's another story).
PS. Just out of curiosity i've benchmarked the code:
fn bench_regex_candidates(b: &mut Bencher) {
b.iter(|| {
Regex::new(r###"[a-z0-9%]{3,}"###).unwrap();
});
}
fn bench_regex_content(b: &mut Bencher) {
b.iter(|| {
Regex::new(r###"^([^*|@"!]*?)#([@?$])?#(.+)$"###).unwrap()
});
}
and the compilation is fast (relatively, for my needs):
regex/candidates time: [22.021 us 22.684 us 23.689 us]
Found 11 outliers among 100 measurements (11.00%)
2 (2.00%) high mild
9 (9.00%) high severe
regex/content time: [36.542 us 36.687 us 36.845 us]
Found 8 outliers among 100 measurements (8.00%)
3 (3.00%) high mild
5 (5.00%) high severe