Skip to content

Commit 8f9e903

Browse files
committed
cat: avoid splice for small files
1 parent 12485f2 commit 8f9e903

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

src/uu/cat/src/cat.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -481,8 +481,17 @@ 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 (https://github.com/uutils/coreutils/issues/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+
if copied < SPLICE_THRESHOLD {
491+
stdout_lock.flush()?;
492+
return Ok(());
493+
}
494+
// splice syscall is faster for large file
486495
if !splice::write_fast_using_splice(handle, &stdout_lock)? {
487496
return Ok(());
488497
}

0 commit comments

Comments
 (0)