Skip to content
Myr

walk: Flush stdout in batches

Before adoption: this commit came in with the history the project adopted, so no landing records it.

The previous behaviour was designed to mimic the output buffering of typical UNIX tools: line-buffered if stdout is a TTY, and fully-buffered otherwise. More precicely, when printing to a terminal, fd would flush explicitly after printing any buffered results, then flush after every single result once streaming mode started. When not printing to a terminal, fd never explicitly flushed, so writes would only happen as the BufWriter filled up. The new behaviour actually unifies the TTY and non-TTY cases: we flush after printing the buffered results, then once we start streaming, we flush after each batch, but *only when the channel is empty*. This provides a good balance: if the channel is empty, the receiver thread might as well flush before it goes to sleep waiting for more results. If the channel is non-empty, we might as well process those results before deciding to flush. For TTYs, this should improve performance by consolidating write() calls without sacrificing interactivity. For non-TTYs, we'll be flushing more often, but only when the receiver would otherwise have nothing to do, thus improving interactivity without sacrificing performance. This is particularly handy when fd is piped into another command (such as head or grep): with the old behaviour, fd could wait for the whole traversal to finish before printing anything. With the new behaviour, fd will print those results soon after they are received. Fixes #1313.

authored by Tavian Barnes · committed by David Peter · parent fea1622 · Browse files · 1 file+6 added−2 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+6 added−2 removed

  • other:+6 added−2 removed

1 file+6 added−2 removed

modified:src/walk.rs+6 added-2 removed
212 unmodified lines
213 }214 }215 ReceiverMode::Streaming => {216 self.print(&dir_entry)?;217 self.flush()?;217 }218 }219220 self.num_results += 1;
9 unmodified lines
230 }231 }232 }233 }234235 // If we don't have another batch ready, flush before waiting236 if self.mode == ReceiverMode::Streaming && self.rx.is_empty() {237 self.flush()?;238 }239 }240 Err(RecvTimeoutError::Timeout) => {241 self.stream()?;242 }
45 unmodified lines
288 }289290 /// Flush stdout if necessary.291 fn flush(&mut self) -> Result<(), ExitCode> {288 if self.config.interactive_terminal && self.stdout.flush().is_err() {292 if self.stdout.flush().is_err() {293 // Probably a broken pipe. Exit gracefully.294 return Err(ExitCode::GeneralError);295 }296 Ok(())
376 unmodified lines