1
0
mirror of https://github.com/BurntSushi/ripgrep.git synced 2025-04-19 09:02:15 +02:00

Switch to Chase-Lev work stealing queue.

It seems to be a touch faster.
This commit is contained in:
Andrew Gallant 2016-09-02 23:38:27 -04:00
parent 7f0b1ccbd3
commit 062aa5ef76

View File

@ -26,7 +26,7 @@ use std::result;
use std::sync::Arc; use std::sync::Arc;
use std::thread; use std::thread;
use crossbeam::sync::SegQueue; use crossbeam::sync::chase_lev::{self, Steal, Stealer};
use docopt::Docopt; use docopt::Docopt;
use grep::{Grep, GrepBuilder}; use grep::{Grep, GrepBuilder};
use parking_lot::Mutex; use parking_lot::Mutex;
@ -138,8 +138,8 @@ fn run(mut args: Args) -> Result<()> {
let mut workers = vec![]; let mut workers = vec![];
let stdout = Arc::new(Mutex::new(io::BufWriter::new(io::stdout()))); let stdout = Arc::new(Mutex::new(io::BufWriter::new(io::stdout())));
let chan_work_send = { let mut chan_work_send = {
let chan_work = Arc::new(SegQueue::new()); let (worker, stealer) = chase_lev::deque();
for _ in 0..args.num_workers() { for _ in 0..args.num_workers() {
let grepb = let grepb =
GrepBuilder::new(&args.arg_pattern) GrepBuilder::new(&args.arg_pattern)
@ -147,14 +147,14 @@ fn run(mut args: Args) -> Result<()> {
let worker = Worker { let worker = Worker {
args: args.clone(), args: args.clone(),
stdout: stdout.clone(), stdout: stdout.clone(),
chan_work: chan_work.clone(), chan_work: stealer.clone(),
inpbuf: InputBuffer::new(), inpbuf: InputBuffer::new(),
outbuf: Some(vec![]), outbuf: Some(vec![]),
grep: try!(grepb.build()), grep: try!(grepb.build()),
}; };
workers.push(thread::spawn(move || worker.run())); workers.push(thread::spawn(move || worker.run()));
} }
chan_work worker
}; };
for p in &args.arg_path { for p in &args.arg_path {
@ -210,7 +210,7 @@ enum Message<T> {
struct Worker { struct Worker {
args: Arc<Args>, args: Arc<Args>,
stdout: Arc<Mutex<io::BufWriter<io::Stdout>>>, stdout: Arc<Mutex<io::BufWriter<io::Stdout>>>,
chan_work: Arc<SegQueue<Message<PathBuf>>>, chan_work: Stealer<Message<PathBuf>>,
inpbuf: InputBuffer, inpbuf: InputBuffer,
outbuf: Option<Vec<u8>>, outbuf: Option<Vec<u8>>,
grep: Grep, grep: Grep,
@ -219,10 +219,10 @@ struct Worker {
impl Worker { impl Worker {
fn run(mut self) { fn run(mut self) {
loop { loop {
let path = match self.chan_work.try_pop() { let path = match self.chan_work.steal() {
None => continue, Steal::Empty | Steal::Abort => continue,
Some(Message::Quit) => break, Steal::Data(Message::Quit) => break,
Some(Message::Some(path)) => path, Steal::Data(Message::Some(path)) => path,
}; };
let file = match File::open(&path) { let file = match File::open(&path) {
Ok(file) => file, Ok(file) => file,