Skip to content

Use size_hint when using read_to_end on something that impl's Read #113927

Closed as not planned
@v1gnesh

Description

@v1gnesh

In the default_read_to_end implementation, a parameter for size_hint is available.
However, when doing the actual read_to_end, it's initialized with None.

When using the the below pattern to implement a custom iterator over a file, when the max possible line length is known, it would be helpful to make use of the size_hint parameter, assuming it's a slightly faster path (?).

This will be, in spirit, a continuation of #89165, #89582, and #110655

impl<R: Read> Iterator for LineIter<R> {
    type Item = io::Result<Vec<u8>>;

    fn next(&mut self) -> Option<Self::Item> {

        let mut data = Vec::with_capacity(MAX_LINE_LENGTH);
        file.by_ref().take(x).read_to_end(&mut data)

In short, I'm asking if the ability to pass a size_hint of x (from the take) into the read_to_end function will be useful, and if it'd be slightly better performance wise?

Metadata

Metadata

Assignees

No one assigned

    Labels

    I-slowIssue: Problems and improvements with respect to performance of generated code.T-libsRelevant to the library team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions