-
Notifications
You must be signed in to change notification settings - Fork 484
Closed as not planned
Description
What version of regex are you using?
latest (1.8.4)
Describe the bug at a high level.
When repeatedly replacing a string in a loop on multiple threads (CPU count -1) the threads seem to block (run in sequence), instead of running in parallel. It seems like something is blocking the threads (maybe shared memory?).
Tested on 2 different multi-core machines, both with Windows 10 64bit.
What are the steps to reproduce the behavior?
cargo run --release -- 1 (runtime 1s) vs cargo run --release -- 6 (runtime 6s)
use std::time::Instant;
use regex::Regex;
fn main() {
let mut args = std::env::args();
let _ = args.next();
let arg1 = args.next();
if arg1.is_none() {
panic!("arg1 missing!");
}
let n = arg1.unwrap().parse().unwrap();
let ts = Instant::now();
let mut handles = Vec::with_capacity(n);
for _ in 0..n {
handles.push(std::thread::spawn(|| {
let mut subject = "#".to_string();
let search = Regex::new(®ex::escape("#")).unwrap();
let replace = "benchmark#";
let ts = Instant::now();
for _ in 0..38000 {
subject = search.replace_all(&subject, replace).to_string();
}
println!("thread {}", ts.elapsed().as_secs_f32());
}));
}
for handle in handles {
handle.join().unwrap();
}
println!("total {}", ts.elapsed().as_secs_f32());
}What is the actual behavior?
The total runtime is the sequential runtime.
What is the expected behavior?
The total runtime should be at most the runtime of the slowest thread on a multi-core machine.