Open
Description
read_to_end
currently includes some logic to do exponentially large .resize
/ .read
operations.
For &[u8]
or Take<&'a [u8]>
, the problem is much simpler as we know the size that will be required :
v.extend_from_slice(&data);
let n = v.len();
data = &data[n..];
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/a1fb87c3f3578f118552917636c95933
yields the following result :
test tests::bench_read_manual ... bench: 9 ns/iter (+/- 1)
test tests::bench_read_std ... bench: 35 ns/iter (+/- 6)