@mbrubeck , thank you so much for all your help and explanations. It really made a difference in understanding this aspect, and I am humbled by having your time to explain this.
Yeah, I incorrectly assumed that ExactSizedIterator
could be used in an unsafe
env. Without that assumption, we do not benefit from extend_from_exact_iter
at all.
It happens is that extend
is not very fast. Basically, we currently ask our users to
- initialize a buffer with zeros (indirectly calling
alloc_zeroed
)
- use
iter_mut
to populate it
The hypothesis that I am testing is that we can offer a safe
and faster API to build buffers by starting from an uninitialized buffer and growing it via extend
.
It happens that, in the implementation I linked to, initializing with zero and using iter_mut
is faster than extend_from_iter
. This is not unexpected as the iter_mut
does not perform extra checks. OTOH, the extend_from_exact_iter
is faster (-25%) than iter_mut
(but unsafe
, as you mentioned).
So, we lose the convenience and expressiveness of collect
(because people will go for faster option, even if it requires more code), as well as the speed of an implementation that a "TrustedLen" would offer.
One option is to offer an unsafe extend_from_trusted_len
while TrustedLen
is not stabilized.
Even if TrustedLen
is stabilized, is there any trait to implement "ExtendFromTrustedLen" that would allow people to create a custom container and use TrustedLen
(without the specialization stabilization, which I do not think I should approach given its complexity)), or would we need to start a new RFC for something like that?