-
Notifications
You must be signed in to change notification settings - Fork 13.3k
std::io::Read::read_to_end
does not specialize for &[u8]
or Take<&'a [u8]>
#44819
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
Comments
A simple solution would be to specialize Alternatively, we could also expose a public trait similar in spirit to Currently, |
No, there's no reason we can't enable specialization in libstd. I don't think specialization is required for the |
@sfackler By which you mean it is not used / not worth it? |
No - you can just add a |
Oh yes that is true :) |
Add read_to_end implementation to &[u8]'s Read impl The default impl for read_to_end does a bunch of bookkeeping that isn't necessary for slices and is about 4 times slower on my machine. The following benchmark takes about 30 ns before this change and about 7 ns after: ``` #[bench] fn bench_read_std(b: &mut Bencher) { let data = vec![0u8; 100]; let mut v = Vec::with_capacity(200); b.iter(|| { let mut s = data.as_slice(); v.clear(); s.read_to_end(&mut v).unwrap(); }); } ``` This solves the easy part of rust-lang#44819 (I think extending this to `Take<&[u8]> `would require specialization)
read_to_end
currently includes some logic to do exponentially large.resize
/.read
operations.For
&[u8]
orTake<&'a [u8]>
, the problem is much simpler as we know the size that will be required :The
manual
implementation is typically between 3 and 5 times faster on my computer.Assuming what seems a non-pathological case : no resize required, the
Vec
has a sufficient capacity to begin with, and running the following benchmark https://gist.github.com/fulmicoton/a1fb87c3f3578f118552917636c95933yields the following result :
The text was updated successfully, but these errors were encountered: