Skip to content

Commit 1ba0d5a

Browse files
committed
tee: improve stdout performance
1 parent f65350a commit 1ba0d5a

1 file changed

Lines changed: 22 additions & 4 deletions

File tree

src/uu/tee/src/tee.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ fn tee(options: &Options) -> Result<()> {
8080
0,
8181
NamedWriter {
8282
name: translate!("tee-standard-output").into(),
83-
inner: Writer::Stdout(stdout()),
83+
inner: Writer::stdout_raw(),
8484
},
8585
);
8686

@@ -251,21 +251,39 @@ fn process_error(
251251

252252
enum Writer {
253253
File(std::fs::File),
254-
Stdout(std::io::Stdout),
254+
StdoutRaw(core::mem::ManuallyDrop<std::fs::File>),
255+
}
256+
257+
impl Writer {
258+
#[cfg(not(windows))]
259+
fn stdout_raw() -> Self {
260+
let fd = std::os::fd::AsRawFd::as_raw_fd(&stdout());
261+
Self::StdoutRaw(core::mem::ManuallyDrop::new(unsafe {
262+
std::os::fd::FromRawFd::from_raw_fd(fd)
263+
}))
264+
}
265+
266+
#[cfg(windows)]
267+
fn stdout_raw() -> Self {
268+
let handle = std::os::windows::io::AsRawHandle::as_raw_handle(&stdout());
269+
Self::StdoutRaw(core::mem::ManuallyDrop::new(unsafe {
270+
std::os::windows::io::FromRawHandle::from_raw_handle(handle)
271+
}))
272+
}
255273
}
256274

257275
impl Write for Writer {
258276
fn write(&mut self, buf: &[u8]) -> Result<usize> {
259277
match self {
260278
Self::File(f) => f.write(buf),
261-
Self::Stdout(s) => s.write(buf),
279+
Self::StdoutRaw(s) => s.write(buf),
262280
}
263281
}
264282

265283
fn flush(&mut self) -> Result<()> {
266284
match self {
267285
Self::File(f) => f.flush(),
268-
Self::Stdout(s) => s.flush(),
286+
Self::StdoutRaw(s) => s.flush(),
269287
}
270288
}
271289
}

0 commit comments

Comments
 (0)