Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions src/uucore/src/lib/features/pipes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,18 +131,21 @@ pub fn splice_unbounded_auto(source: &impl AsFd, dest: &mut impl AsFd) -> Splice
/// return actually sent bytes
#[inline]
pub fn send_n_bytes(input: impl AsFd, target: impl AsFd, n: u64) -> std::io::Result<u64> {
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 resize {
let _ = fcntl_setpipe_size(&target, pipe_size);
thread_local! {
static PIPE_IS_RESIZED: Cell<bool> = const { Cell::new(false) };
}
let mut pipe_is_resized = PIPE_IS_RESIZED.get();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this flag is only read when PIPE_CACHE is empty, so after the first call it is almost never used
head -n -1 small big still caches an unresized broker and never resizes it, no?
do we need it, or should the resize check happen on the cached pipe too?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pipe cache is refleshed if and only if we met I/O error which is cold code-path. I'm not sure if we should cache the size too for the case.

@oech3 oech3 Dec 1, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, we save state if and only if we recycled broker (PIPE_CACHE.set). So it is already done.

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);
if !pipe_is_resized && n > KERNEL_DEFAULT_PIPE_SIZE as u64 {
// expect that input is already extended if it is coming from splice
if fcntl_setpipe_size(&pair.0, MAX_ROOTLESS_PIPE_SIZE).is_ok() {
// improve throughput if output is pipe
Comment thread
oech3 marked this conversation as resolved.
Resolved
let _ = fcntl_setpipe_size(&target, MAX_ROOTLESS_PIPE_SIZE);
}
// not worth retrying fcntl under the same limitation given by system
pipe_is_resized = true;
Comment thread
oech3 marked this conversation as resolved.
Resolved
}
Some(pair)
}) else {
Expand All @@ -154,6 +157,7 @@ pub fn send_n_bytes(input: impl AsFd, target: impl AsFd, n: u64) -> std::io::Res
match splice(&input, &broker_w, usize::try_from(n).unwrap_or(usize::MAX)) {
Ok(0) => {
PIPE_CACHE.set(Some((broker_r, broker_w)));
PIPE_IS_RESIZED.set(pipe_is_resized);
return Ok(bytes_written);
}
Ok(s) => {
Expand All @@ -167,6 +171,7 @@ pub fn send_n_bytes(input: impl AsFd, target: impl AsFd, n: u64) -> std::io::Res
}
}
// pipe is empty. Then we can reuse it at next splice.
PIPE_IS_RESIZED.set(pipe_is_resized);
PIPE_CACHE.set(Some((broker_r, broker_w)));
// remove buffering from this fallback by RawReader, or order of output would be wrong with multiple input
bytes_written += std::io::copy(&mut RawReader(input).take(n), &mut RawWriter(target))?;
Expand Down
Loading