A 3-pointer iterator that moves out of a Vec<T> or Box<[T]>
Why?
If you want to iterate and move items out of a Vec<T>, you'd normally call
.into_iter(), producing a vec::IntoIter iterator. (Note: The
upcoming IntoIterator impl
for Box<[T]> also uses vec::IntoIter.) This is fine for most use cases.
However, storing a large collection of vec::IntoIter iterators might be
suboptimal for memory usage. This is because vec::IntoIter is represented as 4
pointers, which is one more than strictly necessary if all you want is iterating
in one direction.
This crate provides a SmallIter type, which is represented as 3 pointers. In
exchange for this smaller size, this type doesn't implement
DoubleEndedIterator.
Usage
The IntoSmallIterExt trait provides the into_small_iter() method, which
allows you to produce SmallIter iterators from a Vec<T> or a Box<[T]>.
use IntoSmallIterExt;
let v = vec!;
let iter = v.into_small_iter;
let v2: = iter.collect;
assert_eq!;
The benefits of the space savings of this crate is most likely to be relevant if you store a bunch of iterators.
use ;
let v = vec!;
let mut iters: = v.into_iter.map.collect;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
Caveat
For Vec<T>, if there is excess capacity in the vector, calling
into_small_iter will first shrink the allocation to fit the existing elements.
Depending on the allocator, this may reallocate.
On the other hand, calling into_small_iter on a Box<[T]> is cheap.
Benchmark results
I have benchmarked (on a Macbook Pro 2021) the following workload (which is the
kind of workload that this crate is intended for): Construct 100,000 iterators,
each containing 100 u8s. Then, get the first element of each iterator, then
the second, and so on.
This workload is performed in three ways:
- using
SmallIter(this crate)- taking 20.4ms on average
- using
thin_vec::IntoIter(from thethin-veccrate)- taking 30.5ms on average
- using
std::vec::IntoIter- taking 21.9ms on average
The source code for the benchmark can be found here.