ls: reduce write syscalls & cleanup#2115
Conversation
|
well done :) |
| let mut dirs = Vec::<PathData>::new(); | ||
| let mut has_failed = false; | ||
|
|
||
| let mut out = BufWriter::new(stdout()); |
There was a problem hiding this comment.
Please see if we can use something like in #2111 where we only buffer if stdout is not interactive (instead of buffering always).
Always buffering will slightly impact user experience when looking at output on a terminal as nothing will be printed until buffer is flushed.
Might I also suggest keeping this as a global because it might unnecessarily complicate function signatures (although globals are generally frowned upon, this seems like valid usecase to me - please feel free to ignore if you disagree :) )
There was a problem hiding this comment.
I figured we could just always buffer because the output of ls should always be generated fairly quickly. At least when I tried there seemed to be no real visible difference. The only time this might be an issue is with an --ignore flag that hides a lot of files? One thing I didn't like about that PR was that it needed Box<dyn Write>. Maybe it'd be nice to create a StdoutWriter type in uucore that automatically buffers if's not interactive?
I chose not to make this a global, because write! requires a mutable reference, so it would needs in a Mutex if we use lazy_static, which also complicates the code. Anyway, if you can find a nicer way to implement this, feel free to do so!
There was a problem hiding this comment.
Agreed, I also noticed rust-lang/rust#60673 - seems like stdout buffering is already in the works.
As for the global related comment - yeah seems like the complicated lazy_static + mutex would be the way to implement it, I'll check if I can find a better way.
There was a problem hiding this comment.
That issue is very interesting, I hope some solution lands soon! Thanks for checking out the global variable!
|
looks good :) |
Instead of writing directly to
stdout, withprint!andprintln!, we can write to aBufWriterto reducewritesyscalls. Doing so leads to the following improvements:writecalls goes from 344064 to 1090 when runningls -Ron the Firefox source code.The full benchmarks are in the
detailstag below. Becauseprint!doesn't return aResultandwrite!does, I simply discarded the return values ofwrite!.Because this was a simple change I figured I could sneak in a few other changes to clean up the code some more. I hope that's okay. These changes are:
EntryAPI with the cacheHashMaps.indicator_styleargument parsing.display_file_typereturn acharinstead ofString.Full Benchmarks
Before
After