Skip to content

Commit 20fada6

Browse files
committed
cat: avoid splice for small files
1 parent e104fc8 commit 20fada6

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

src/uu/cat/src/cat.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -481,8 +481,18 @@ fn write_fast<R: FdReadable>(handle: &mut InputHandle<R>) -> CatResult<()> {
481481
let mut stdout_lock = stdout.lock();
482482
#[cfg(any(target_os = "linux", target_os = "android"))]
483483
{
484-
// If we're on Linux or Android, try to use the splice() system call
485-
// for faster writing. If it works, we're done.
484+
// splice is slow for small files (issue/10832)
485+
const SPLICE_THRESHOLD: u64 = 16 * 1024;
486+
let copied = io::copy(
487+
&mut handle.reader.by_ref().take(SPLICE_THRESHOLD),
488+
&mut stdout_lock,
489+
)
490+
.inspect_err(handle_broken_pipe)?;
491+
if copied < SPLICE_THRESHOLD {
492+
stdout_lock.flush().inspect_err(handle_broken_pipe)?;
493+
return Ok(());
494+
}
495+
// splice syscall is faster for large file
486496
if !splice::write_fast_using_splice(handle, &stdout_lock)? {
487497
return Ok(());
488498
}

0 commit comments

Comments
 (0)