Skip to content

std: change BufWriter to return ShortWrite/EndOfFile #19431

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

Closed
wants to merge 1 commit into from

Conversation

erickt
Copy link
Contributor

@erickt erickt commented Dec 1, 2014

Previously, BufWriter::write would just return an std::io::OtherIoError if someone attempted to write past the end of the wrapped buffer. This pull request changes the error to support partial writes and return a std::io::ShortWrite, or an io::io::EndOfFile if it's been fully exhausted.

I've also optimized away a bounds check inside BufWriter::write, which should help shave off some nanoseconds in an inner loops.

@rust-highfive
Copy link
Contributor

warning Warning warning

  • These commits modify unsafe code. Please review it carefully!

/// If `true`, then this will return an `EndOfFile` error on the next
/// write.
#[inline]
fn eof(&self) -> bool { self.pos >= self.buf.len() }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be pub?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Updated.

@alexcrichton
Copy link
Member

Could this leave out the eof function for now? It would be nice to avoid expanding the public API for now. Additionally, do you have benchmark numbers for the before/after to justify the unsafe? I would expect writers with a need for speed to use the Writer implementation on Vec<u8>, not on a MemWriter (which will probably slow down with a Seek implementation in the future.

@erickt
Copy link
Contributor Author

erickt commented Dec 5, 2014

@alexcrichton: I removed eof and the unsafe code for now. My benchmarking showed that the safe and unsafe code performed nearly the same, but with a slight edge for unsafe code, which probably isn't that important.

Regarding speed, this is working on BufWriter, not MemWriter :) The neat thing is that even this safe version is approximately as fast as just using ptr::copy_nonoverlapping_memory. Vec<u8>/MemWriter for some reason are at least twice as slow as &[u8]/BufWriter, even if we aren't growing the underlying vector. Not sure why yet.

bors added a commit that referenced this pull request Dec 6, 2014
Previously, `BufWriter::write` would just return an `std::io::OtherIoError` if someone attempted to write past the end of the wrapped buffer. This pull request changes the error to support partial writes and return a `std::io::ShortWrite`, or an `io::io::EndOfFile` if it's been fully exhausted.

 I've also optimized away a bounds check inside `BufWriter::write`, which should help shave off some nanoseconds in an inner loops.
@bors bors closed this Dec 6, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants