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
It may be possible for us to completely remove the
TrustedLentrait, as the only place we really need it is invortex-buffer.By changing our
from_iterandextendimplementations onBufferMutto do this (instead of only relying on the 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_iterandextend_trustedin #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