-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Open
Labels
A-ioArea: `std::io`, `std::fs`, `std::net` and `std::path`Area: `std::io`, `std::fs`, `std::net` and `std::path`C-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.T-libsRelevant to the library team, which will review and decide on the PR/issue.Relevant to the library team, which will review and decide on the PR/issue.
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)
Metadata
Metadata
Assignees
Labels
A-ioArea: `std::io`, `std::fs`, `std::net` and `std::path`Area: `std::io`, `std::fs`, `std::net` and `std::path`C-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.T-libsRelevant to the library team, which will review and decide on the PR/issue.Relevant to the library team, which will review and decide on the PR/issue.