@@ -13,8 +13,12 @@ use uucore::error::{UResult, USimpleError};
1313use uucore:: format_usage;
1414use uucore:: translate;
1515
16- // it's possible that using a smaller or larger buffer might provide better performance on some
17- // systems, but honestly this is good enough
16+ #[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
17+ const MAX_ROOTLESS_PIPE_SIZE : usize = 1024 * 1024 ;
18+ // todo: investigate best rate
19+ #[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
20+ const BUF_SIZE : usize = MAX_ROOTLESS_PIPE_SIZE ;
21+ #[ cfg( not( any( target_os = "linux" , target_os = "android" ) ) ) ]
1822const BUF_SIZE : usize = 16 * 1024 ;
1923
2024#[ uucore:: main]
@@ -110,8 +114,27 @@ fn prepare_buffer(buf: &mut Vec<u8>) {
110114
111115pub fn exec ( bytes : & [ u8 ] ) -> io:: Result < ( ) > {
112116 let stdout = io:: stdout ( ) ;
113- let mut stdout = stdout. lock ( ) ;
117+ #[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
118+ {
119+ use rustix:: io:: write;
120+ use rustix:: pipe:: { SpliceFlags , fcntl_setpipe_size, pipe, tee} ;
121+ // fast-path for pipe. todo: port the fast-path for > file
122+ if let Ok ( ( p_read, p_write) ) = pipe ( ) {
123+ let _ = fcntl_setpipe_size ( & p_read, MAX_ROOTLESS_PIPE_SIZE ) ;
124+ let _ = fcntl_setpipe_size ( & p_write, MAX_ROOTLESS_PIPE_SIZE ) ;
125+ let _ = fcntl_setpipe_size ( & stdout, MAX_ROOTLESS_PIPE_SIZE ) ;
126+ let _ = write ( & p_write, bytes) ;
127+ loop {
128+ match tee ( & p_read, & stdout, MAX_ROOTLESS_PIPE_SIZE , SpliceFlags :: MORE ) {
129+ Ok ( n) if n > 0 => { }
130+ Ok ( 0 ) => break ,
131+ _ => break ,
132+ }
133+ }
134+ }
135+ }
114136
137+ let mut stdout = stdout. lock ( ) ;
115138 loop {
116139 stdout. write_all ( bytes) ?;
117140 }
@@ -122,6 +145,7 @@ mod tests {
122145 use super :: * ;
123146
124147 #[ test]
148+ #[ cfg( not( any( target_os = "linux" , target_os = "android" ) ) ) ] // Linux uses different buffer size
125149 fn test_prepare_buffer ( ) {
126150 let tests = [
127151 ( 150 , 16350 ) ,
0 commit comments