Intel's Hyperscan is renowned for its high-performance regular expression matching capabilities, particularly for simultaneous matching of large numbers of patterns. When looking for comparable libraries in the Rust ecosystem, there are several options with varying degrees of similarity and performance characteristics.
Rust Bindings for Hyperscan
The most direct equivalent to Hyperscan in Rust is to use Rust bindings for Hyperscan itself:
rust-hyperscan
This is a Rust binding for Intel's Hyperscan library that provides access to Hyperscan's capabilities from Rust code[11]. It allows you to use Hyperscan's high-performance pattern matching with Rust's safety guarantees.
use hyperscan::prelude::*;
fn main() {
let pattern = pattern! {"test"; CASELESS | SOM_LEFTMOST};
let db: BlockDatabase = pattern.build().unwrap();
let scratch = db.alloc_scratch().unwrap();
let mut matches = vec![];
db.scan("some test data", &scratch, |id, from, to, flags| {
println!("found pattern #{} @ [{}, {})", id, from, to);
matches.push(from..to);
Matching::Continue
}).unwrap();
assert_eq!(matches, vec![5..9]);
}
This library supports both static and dynamic linking modes, and also includes a "chimera" feature that provides a hybrid of Hyperscan and PCRE capabilities[12].
hyperscan-async-sys
There's also a newer binding called hyperscan-async-sys which provides Rust bindings to the Hyperscan C API specifically for use in the hyperscan-async crate[18].
Native Rust Regular Expression Libraries
regex
The standard regex crate in Rust is highly optimized and provides linear-time matching guarantees:
use regex::Regex;
fn main() {
let re = Regex::new(r"test").unwrap();
let hay = "some test data";
if let Some(mat) = re.find(hay) {
println!("Match found: {}", mat.as_str());
}
}
While not as fast as Hyperscan in all scenarios, the regex crate has several important characteristics:
-
It guarantees linear time matching with worst case O(m * n) time complexity, where m is proportional to the regex size and n to the string size[3][4].
-
It uses finite automata techniques similar to RE2, prioritizing predictable performance over supporting complex features like backreferences and look-around assertions[3].
-
It includes aggressive literal optimizations that can make it faster than RE2 in many common use cases[16].
-
It provides excellent Unicode support by default[4].
According to benchmarks, the Rust regex engine performs quite well compared to other engines, though Hyperscan is still faster overall. In one comparison, Hyperscan was the fastest with a total execution time of ~300ms, while the Rust regex crate placed 5th with ~3700ms[2][13]. However, when using a scoring system that limits the impact of outlier test cases, the Rust regex crate tied for 2nd place with PCRE2-JIT[2][13].
flpc
A newer contender in the Rust regex ecosystem is flpc, which claims to be "probably the fastest regex library for Python" but is actually made with Rust and PyO3[1]. According to its documentation, it can be up to 5932.69x faster than Python's re-module on average in certain scenarios. While primarily targeting Python users, the underlying Rust implementation could potentially be used directly.
Performance Considerations
When comparing these libraries, it's important to note:
-
Hyperscan is specifically designed for high-throughput scenarios where you need to match many patterns simultaneously. It uses hardware acceleration (like AVX instructions) and is particularly optimized for x86 platforms[2][13].
-
The standard Rust regex library prioritizes predictable performance and safety over raw speed in all cases, but is still very fast for most use cases.
-
Compilation time matters: regex compilation can be expensive (microseconds to milliseconds depending on pattern complexity), so it's recommended to compile patterns once and reuse them, especially in loops[3][4].
use once_cell::sync::Lazy;
use regex::Regex;
fn some_helper_function(haystack: &str) -> bool {
static RE: Lazy<regex> = Lazy::new(|| Regex::new(r"...").unwrap());
RE.is_match(haystack)
}
- For specific use cases like massive text replacement, specialized tools might be more appropriate. For example, one developer reported that when using regex for a task that would take 5 days, they built a tool called FlashText that completed it in 15 minutes[5].
Conclusion
If you absolutely need Hyperscan's level of performance and multi-pattern matching capabilities, using the Rust bindings for Hyperscan (rust-hyperscan or hyperscan-async-sys) is your best option. These provide the full power of Hyperscan with Rust's safety guarantees.
If you prefer a pure Rust solution and can accept somewhat lower performance in exchange for better cross-platform support and integration with the Rust ecosystem, the standard regex crate is a solid choice that still offers excellent performance for most use cases.
For specific use cases where you're dealing with a large number of fixed strings rather than complex patterns, specialized solutions might be worth exploring.