Skip to content

Commit c29be44

Browse files
oech3sylvestre
authored andcommitted
cat: avoid unnecessary alloc on Linux
1 parent 98f6b01 commit c29be44

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/uu/cat/src/cat.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -490,11 +490,13 @@ fn write_fast<R: FdReadable>(handle: &mut InputHandle<R>) -> CatResult<()> {
490490
}
491491
// If we're not on Linux or Android, or the splice() call failed,
492492
// fall back on slower writing.
493+
write_slow(handle, stdout)
494+
}
495+
496+
#[cfg_attr(any(target_os = "linux", target_os = "android"), inline(never))] // splice fast-path does not require this allocation
497+
#[cfg_attr(not(any(target_os = "linux", target_os = "android")), inline)]
498+
fn write_slow<R: FdReadable>(handle: &mut InputHandle<R>, stdout: io::Stdout) -> CatResult<()> {
493499
let mut stdout_lock = stdout.lock();
494-
// stack allocation is overhead when splice succeed
495-
#[cfg(any(target_os = "linux", target_os = "android"))]
496-
let mut buf = vec![0; 1024 * 64];
497-
#[cfg(not(any(target_os = "linux", target_os = "android")))]
498500
let mut buf = [0; 1024 * 64];
499501
loop {
500502
match handle.reader.read(&mut buf) {

0 commit comments

Comments
 (0)