Skip to content
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

io::Read / io::Write fns can cause a lot of allocations via io::Error::new #36658

Closed
carllerche opened this issue Sep 22, 2016 · 3 comments · Fixed by #37037
Closed

io::Read / io::Write fns can cause a lot of allocations via io::Error::new #36658

carllerche opened this issue Sep 22, 2016 · 3 comments · Fixed by #37037
Labels
I-slow Issue: Problems and improvements with respect to performance of generated code.

Comments

@carllerche
Copy link
Member

carllerche commented Sep 22, 2016

io::Error::new will always allocate. This isn't a huge deal when the error represents an actual error case, however I/O "errors" are used often to signal termination in hot paths and the allocation can cause significant bottleneck.

One example is with non-blocking I/O, io::ErrorKind::WouldBlock is used to signal that the source is not read to process the op. Suffering an allocation for this very common case is not acceptable.

To work around this, Mio provide a helper to construct a WouldBlock error without the allocation: https://github.com/carllerche/mio/blob/master/src/io.rs#L40-L49.

However, WouldBlock is not the only case. Another common case that I am hitting is using io::ErrorKind::WriteZero being triggered when calling write_all to io::Write implementations that are backed by memory (vs. an I/O type).

In general, there should be a way to construct I/O errors without allocating.

/cc @alexcrichton

@bluss bluss added I-slow Issue: Problems and improvements with respect to performance of generated code. A-io labels Sep 22, 2016
@arthurprs
Copy link
Contributor

One option on top of my head is to provide a from_kind that constructs an opaque-ish error with that kind, otherwise there's always from_raw_os_error

bors added a commit that referenced this issue Nov 4, 2016
Add conversions from `io:ErrorKind` to `io::Error`

Filing to help with discussion around the possibility of doing this.

Current changes are clearly backwards incompatible, but I think adding a new function (with a bikeshed on naming) like `Error::new_str` should be possible (or some other way of specializing the string error message case) to fix #36658.
@Mark-Simulacrum
Copy link
Member

@carllerche Do you (or others) know if std::io should now be examined for instances where Error::new is used in non-optimal ways?

@carllerche
Copy link
Member Author

I haven't looked much in std. I mostly needed this for mio (https://github.com/carllerche/mio)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
I-slow Issue: Problems and improvements with respect to performance of generated code.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants