-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ls
: reduce write syscalls & cleanup
#2115
Conversation
well done :) |
@@ -1092,6 +1082,8 @@ fn list(locs: Vec<String>, config: Config) -> i32 { | |||
let mut dirs = Vec::<PathData>::new(); | |||
let mut has_failed = false; | |||
|
|||
let mut out = BufWriter::new(stdout()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 aBufWriter
to reducewrite
syscalls. Doing so leads to the following improvements:write
calls goes from 344064 to 1090 when runningls -R
on the Firefox source code.The full benchmarks are in the
details
tag below. Becauseprint!
doesn't return aResult
andwrite!
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:
Entry
API with the cacheHashMap
s.indicator_style
argument parsing.display_file_type
return achar
instead ofString
.Full Benchmarks
Before
After