-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
BufWriter should buffer up to its capacity #72919
Comments
I don't think we make any guarantees on the exact buffering behavior other than "some buffering happens". In this particular case, copying the data to the buffer is pointless since we'll need to flush the buffer on the next write call anyways: it doesn't help to reduce the number of underlying write calls. |
I think the current implementation makes sense. I'd say the goal of a BufWriter to reduce the number of write calls on the underlying stream. Buffering a write call of the full buffer capacity only serves the purpose of using the buffer as much as possible, as the number of write calls (and their sizes) remain the same. That doesn't seem useful to me. |
Maybe instead, writing |
I'm actually working on a PR that does something similar to that. The issue you run into, at least for In any case, I've been convinced about the reasoning for this, so this issue is satisfied. |
…ertj Use is_write_vectored to optimize the write_vectored implementation for BufWriter In case when the underlying writer does not have an efficient implementation `write_vectored`, the present implementation of `write_vectored` for `BufWriter` may still forward vectored writes directly to the writer depending on the total length of the data. This misses the advantage of buffering, as the actually written slice may be small. Provide an alternative code path for the non-vectored case, where the slices passed to `BufWriter` are coalesced in the buffer before being flushed to the underlying writer with plain `write` calls. The buffer is only bypassed if an individual slice's length is at least as large as the buffer. Remove a FIXME comment referring to rust-lang#72919 as the issue has been closed with an explanation provided.
rust/src/libstd/io/buffered.rs
Line 662 in eeaf497
Suppose you had a
BufWriter
with capacity 4. If you writeb"1234"
to it, that will be flushed directly to the underlying stream, but if you writeb"12"
and thenb"34"
, those will be buffered. The unit tests confirm this behavior, but it seems surprising to me.The text was updated successfully, but these errors were encountered: