Skip to content

Commit c1aaec2

Browse files
committed
cat: call pipe() once with multiple input
1 parent d5219fe commit c1aaec2

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

src/uu/cat/src/splice.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ pub(super) fn write_fast_using_splice<R: FdReadable, S: AsRawFd + AsFd>(
2323
handle: &InputHandle<R>,
2424
write_fd: &S,
2525
) -> CatResult<bool> {
26+
use std::{fs::File, sync::OnceLock};
27+
static BROKER: OnceLock<Option<(File, File)>> = OnceLock::new();
2628
if splice(&handle.reader, &write_fd, MAX_ROOTLESS_PIPE_SIZE).is_ok() {
2729
// fcntl improves throughput
2830
// todo: avoid fcntl overhead for small input, but don't fcntl inside of the loop
@@ -34,7 +36,7 @@ pub(super) fn write_fast_using_splice<R: FdReadable, S: AsRawFd + AsFd>(
3436
Err(_) => return Ok(true),
3537
}
3638
}
37-
} else if let Ok((pipe_rd, pipe_wr)) = pipe() {
39+
} else if let Some((pipe_rd, pipe_wr)) = BROKER.get_or_init(|| pipe().ok()).as_ref() {
3840
// both of in/output are not pipe. needs broker to use splice() with additional costs
3941
loop {
4042
match splice(&handle.reader, &pipe_wr, MAX_ROOTLESS_PIPE_SIZE) {

0 commit comments

Comments
 (0)