diff --git a/src/uucore/src/lib/features/pipes.rs b/src/uucore/src/lib/features/pipes.rs index aa135a46e39..83918ce67d9 100644 --- a/src/uucore/src/lib/features/pipes.rs +++ b/src/uucore/src/lib/features/pipes.rs @@ -149,12 +149,20 @@ pub fn splice_unbounded_auto(source: &impl AsFd, dest: &mut impl AsFd) -> Splice #[inline] pub fn send_n_bytes(input: impl AsFd, target: impl AsFd, n: u64) -> std::io::Result { let pipe_size = n.min(MAX_ROOTLESS_PIPE_SIZE as u64) as usize; + let resize = pipe_size > KERNEL_DEFAULT_PIPE_SIZE; // improve throughput if output is pipe // expected that input is already extended if it is coming from splice - if pipe_size > KERNEL_DEFAULT_PIPE_SIZE { + if resize { let _ = fcntl_setpipe_size(&target, pipe_size); } - let Ok((broker_r, broker_w)) = PIPE_CACHE.take().map_or_else(pipe::, Ok) else { + let Some((broker_r, broker_w)) = PIPE_CACHE.take().or_else(|| { + // use std::io::pipe to avoid fcntl when it is not useful + let pair = std::io::pipe().ok()?; + if resize { + let _ = fcntl_setpipe_size(&pair.0, pipe_size); + } + Some(pair) + }) else { return std::io::copy(&mut RawReader(input).take(n), &mut RawWriter(target)); }; let mut n = n;