Skip to content

Commit dc18b54

Browse files
authored
cat: call pipe() once with multiple input (#11699)
1 parent 6133ef5 commit dc18b54

1 file changed

Lines changed: 3 additions & 1 deletion

File tree

src/uu/cat/src/splice.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ pub(super) fn write_fast_using_splice<R: FdReadable, S: AsRawFd + AsFd + Write>(
2121
handle: &InputHandle<R>,
2222
write_fd: &mut S,
2323
) -> CatResult<bool> {
24+
use std::{fs::File, sync::OnceLock};
25+
static PIPE_CACHE: OnceLock<Option<(File, File)>> = OnceLock::new();
2426
if splice(&handle.reader, &write_fd, MAX_ROOTLESS_PIPE_SIZE).is_ok() {
2527
// fcntl improves throughput
2628
// todo: avoid fcntl overhead for small input, but don't fcntl inside of the loop
@@ -32,7 +34,7 @@ pub(super) fn write_fast_using_splice<R: FdReadable, S: AsRawFd + AsFd + Write>(
3234
Err(_) => return Ok(true),
3335
}
3436
}
35-
} else if let Ok((pipe_rd, pipe_wr)) = pipe() {
37+
} else if let Some((pipe_rd, pipe_wr)) = PIPE_CACHE.get_or_init(|| pipe().ok()).as_ref() {
3638
// both of in/output are not pipe. needs broker to use splice() with additional costs
3739
loop {
3840
match splice(&handle.reader, &pipe_wr, MAX_ROOTLESS_PIPE_SIZE) {

0 commit comments

Comments
 (0)