walk: Limit batch sizes in --exec mode
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tavian Barnes · committed by Tavian Barnes · parent 73260c0 · Browse files · 1 file·+16 added−6 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+16 added−6 removed
- other:+16 added−6 removed
Provenance
- Commit
b8a5f95- Tree
c59a891- Author
Tavian Barnes <tavianator@tavianator.com>- Committer
Tavian Barnes <tavianator@tavianator.com>- Parent
73260c0
1 file changed+16 added−6 removed
modified:src/walk.rs+16 added-6 removed
73 unmodified lines74 /// Wrapper that sends batches of items at once over a channel.75 struct BatchSender {76 batch: Batch,77 tx: Sender<Batch>,78+ limit: usize,79 }80 81 impl BatchSender {81- fn new(tx: Sender<Batch>) -> Self {82+ fn new(tx: Sender<Batch>, limit: usize) -> Self {83 Self {84 batch: Batch::new(),85 tx,86+ limit,87 }88 }89 90 /// Check if we need to flush a batch.89- fn needs_flush(batch: Option<&Vec<WorkerResult>>) -> bool {91+ fn needs_flush(&self, batch: Option<&Vec<WorkerResult>>) -> bool {92 match batch {93 // Limit the batch size to provide some backpressure92- Some(vec) => vec.len() >= 0x400,94+ Some(vec) => vec.len() >= self.limit,95 // Batch was already taken by the receiver, so make a new one96 None => true,97 }98 }1 unmodified line100 /// Add an item to a batch.101 fn send(&mut self, item: WorkerResult) -> Result<(), SendError<()>> {102 let mut batch = self.batch.lock();103 102- if Self::needs_flush(batch.as_ref()) {104+ if self.needs_flush(batch.as_ref()) {105 drop(batch);106 self.batch = Batch::new();107 batch = self.batch.lock();108 }335 unmodified lines444 walker.run(|| {445 let patterns = &self.patterns;446 let config = &self.config;447 let quit_flag = self.quit_flag.as_ref();446- let mut tx = BatchSender::new(tx.clone());448 449+ let mut limit = 0x100;450+ if let Some(cmd) = &config.command {451+ if !cmd.in_batch_mode() && config.threads > 1 {452+ // Evenly distribute work between multiple receivers453+ limit = 1;454+ }455+ }456+ let mut tx = BatchSender::new(tx.clone(), limit);457+458 Box::new(move |entry| {459 if quit_flag.load(Ordering::Relaxed) {460 return WalkState::Quit;461 }175 unmodified lines637 })638 .unwrap();639 }640 631- let (tx, rx) = bounded(config.threads);641+ let (tx, rx) = bounded(2 * config.threads);642 643 let exit_code = thread::scope(|scope| {644 // Spawn the receiver thread(s)645 let receiver = scope.spawn(|| self.receive(rx));23 unmodified lines