Skip to content

Remove TrustedLen #5058

@connortsui20

Description

@connortsui20

It may be possible for us to completely remove the TrustedLen trait, as the only place we really need it is in vortex-buffer.

By changing our from_iter and extend implementations on BufferMut to do this (instead of only relying on the lower bound):

// Since we do not know the length of the iterator, we can only guess how much memory we
// need to reserve. Note that these hints may be inaccurate.
let (lower_bound, upper_bound_opt) = iter.size_hint();
self.reserve(upper_bound_opt.unwrap_or(lower_bound));

we can deduplicate a lot of the code for this while incurring only 1 extra branch outside of the hot loop. More specifically, if you look at the difference between extend_iter and extend_trusted in #5055, there would only be a single extra branch after the hot loop that checks if there is any more items that need to be yielded.

In the worst case, that extra branch messes up all of the optimizations. In the best case, that single extra branch doesn't make much of a performance difference in practice.

CC @gatesn @robert3005 @a10y

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions