-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Add buffered I/O #8953
Comments
I certainly felt the need of this with rust-http, where code like writing an HTTP header to a socket with write() calls for name, ": ", value, and "\r\n" would take four TCP packets rather than probably a small fraction of one. Hence (in part) my current buffering arrangement in rust-http. I think the difficulty here is handling reading and writing. As it happens, I'd rather like to be able to split a socket up into a reader and a writer, but that's out of scope here. I think, though, that we'd need three decorators; one for I switched from my own BufferedReader and BufferedWriter to having a unified BufferedStream (the former two using borrowing on account of needing to read and write) when that approach became infeasible. Here, a BufferedReader and BufferedWriter would, I presume, own the Reader or Writer. My BufferedStream code is available at https://github.com/chris-morgan/rust-http/blob/f78236284b73ba5e980962b1ffbd71ea9a03fbbc/src/libhttp/buffer.rs |
CM, do you need to implement BufferedStream separately, you can just let the Writer trait pass through the reader buffer?
|
@blake2-ppc BufferedStream must buffer both reading and writing. Still, what you suggest sounds a good way of achieving buffering of just reading or just writing. |
I mean that you can stack the BufferedWriter on top of the BufferedReader this way, without having to implement BufferedStream separately. |
@chris-morgan +1 I think it's great that you already have this code set aside and I think it's worth getting into the rust tree as-is (to relieve you of the burden of carrying it in With that being said, while looking this over something occurred to me: is it worth considering that we break If we look forward to platform-backed implementations for everything in The drawback to this approach, of course, is that the buffering code is duplicated in multiple places in This may be worthy of breaking out into a separate ticket, and there's certainly an argument against what I'm proposing (as Chris has presented this solution, a And based on my current read of |
@blake2-ppc yep, after this and discussion in IRC I get what's being meant. Yes, that would work, with |
The default buffer size is the same as the one in Java's BufferedWriter. We may want BufferedWriter to have a Drop impl that flushes, but that isn't possible right now due to #4252/#4430. This would be a bit awkward due to the possibility of the inner flush failing. For what it's worth, Java's BufferedReader doesn't have a flushing finalizer, but that may just be because Java's finalizer support is awful. The current implementation of BufferedStream is weird in my opinion, but it's what the discussion in #8953 settled on. I wrote a custom copy function since vec::copy_from doesn't optimize as well as I would like. Closes #8953
…, r=Manishearth add vec.capacity() to [`slow_vec_initialization`] detection fix rust-lang#8800 for example ```rust let mut vec1 = Vec::with_capacity(len); vec1.resize(vec1.capacity(), 0); let mut vec2 = Vec::with_capacity(len); vec2.extend(repeat(0).take(vec2.capacity())); ``` will trigger the lint --- changelog: add `vec.capacity()` to [`slow_vec_initialization`] detection
libuv provides unbuffered I/O. We also need buffered decorators for readers and writers.
The text was updated successfully, but these errors were encountered: