r/rust • u/somebodddy • Jan 10 '19
Why is stdout behind a lock in the first place?
I've notice that the implicit lock of stdout is a common performance issue. The lock gets acquired every time we `println!`, which is very slow, so we need to explicitly control the lock in order to avoid it.
But... why does it need to be behind locked in the first place? Isn't the kernel handling these writes anyways? And isn't stdout write races usually considered a non-issue?
28
u/simukis Jan 10 '19
The stdout itself is a global shared mutable resource. The options then are to
have it behind a lock and working well in any scenario;
not have it behind a lock and somehow restrict its use to a single owner (idea similar to what
rtfmcrate does, butprintln!could not exist in its current form);not have it behind a lock and see it not working correctly in subtle corner cases (what you might observe from time to time in C-land).
5
u/po8 Jan 11 '19
There are other options, I think:
Use a lock-free structure for the stdout buf
Make the stdout buf thread-local, giving each thread its own independent one
Spin up a separate thread for the buffer and send to it through a channel. (This is a terrible idea, but it could be made to sort of work. Sort of.)
I'm sure there are other possibilities.
5
u/ssokolow Jan 11 '19
I don't see how the first two would work.
The purpose of the lock is to make sure that two or more threads, writing to stdout concurrently, can't accidentally interleave the data they write at a granularity below individual
println!calls.Sooner or later, you have to reconcile the thread-local stdout bufs into the single stdout that the OS APIs provide you... and then you're either back to where you started or implementing your "Spin up a separate thread for the buffer and send to it through a channel." idea.
5
u/pftbest Jan 11 '19
Interleaving data is not a data race, so Rust is not required to provide such guarantee. And you can call the kernel API from any thread you like, so this is also not a problem.
→ More replies (1)6
u/ssokolow Jan 11 '19
Rust aims to provide safety guarantees beyond protection from data races where its developers consider it a readonable trade-off.
That's the whole point of using monadic error handling (
Result<T, E>) rather than special return values like-1when anything that's not exceptions would satisfy the concerns related to avoiding data races.In this case, the goal is to ensure that, if something goes wrong in heavily multi-threaded code, any status/log messages needed to detect and/or diagnose it won't be garbled.
Having a lock around stdout is in line with the Rust philosophy of defaulting to the safe choice and letting people opt out.
→ More replies (2)
6
u/matthieum [he/him] Jan 11 '19
But... why does it need to be behind locked in the first place?
Convenience.
It makes writing simple stdin/stdout scripts in Rust easy, and the performance hit there does not matter; it'd be far worse in Python anyway. D makes the same choice, for the same reasons. It's easier to be correct by default.
If performance of I/O really suffers from the mutex, then the interface allows you to lock it yourself, trading convenience for performance... I've rarely seen the performance of stdin and stdout really matter in the real world, though, most often it seems to only matter in benchmarks.
2
Jan 11 '19
An unbuffered stdout would cause mixing of half-written lines. While this wouldn't lead to a crash, it is quite messy.
(Technically, as long as you write less than 4k in one write, then you won't get mixed up output, but Rust often performs complex writes as a series of smaller writes.
50
u/iq-0 Jan 10 '19
The reason is that stout is buffered and that buffer needs to be protected against possible multithreaded writes to it.