Tracking Issue for Iterator::next_chunk rust-lang/rust#98326
Should this method support N=0? itertools does not, and it sounds plausible; however, if we want to do the same, we will have the same concern as <[T]>::array_chunks() that cannot be stabilized until generic_const_exprs or some subset will be stabilized.
I'm wondering about the intended uses of this API.
If it is for performance (autovectorization) then it seems fairly brittle. I just tried doing this benchmark:
#[bench]
fn bench_next_chunk(b: &mut Bencher) {
let v = vec![13u8; 2048];
b.iter(|| {
const CHUNK: usize = 8;
let mut sum = [0u32; CHUNK];
let mut iter = black_box(v.clone()).into_iter();
while let Ok(chunk) = iter.next_chunk::<CHUNK>() {
for i in 0..CHUNK {
sum[i] += chunk[i] as u32;
}
}
sum
})
}and it took 17,272 ns/iter when compiled with target-cpu=znver2. With a handrolled implementation I got 211 ns/iter. I encountered some 10x slowdowns in a few other combinations. Sometimes codegen-units=1 produced even worse assembly because vectorizer did silly things such as loading one byte at a time into a simd register with vpinsrb.
And that's with direct, linear memory access on a Vec::IntoIter which should be relatively straight-forward to turn into masked simd loads. Many iterators yield references. A [&T; N] would need a gather-load which is expensive and only available on newer CPUs, this is unlike slice::array_chunks which yields &[T; N]. We could try specializing slice.iter().next_chunk() but then you might as well use array_chunks. And of course it'll get more complex with additional adapters.
Another limitation is that it can't be used in a for-in loop or chained further with other iterator adapters, an issue #92393 didn't have.
This is probably an example of a case where you should first use copied(), and then chunk it, and then you turn that array into a simd type for the work, before possibly turning it back at the end of your loop.
And the change into and out of simd form will have a cost, so you'll want to ensure you're doing enough work inside each loop pass or it might exceed the cost of the scalar only code.
yeah that was just an example to illustrate that the default implementation is suboptimal and we'll need optimized implementations on most linear-memory sources and length-preserving adapters.
Mm, yeah.
Myself I would imagine using this most on iterators that aren't linear memory sources, and "the point" would be to have a clean and unified way to turn some crazy data source into simple SIMD chunks.
Might be good to align the naming of this function with Iterator::array_chunks, i.e. either
Iterator::chunksIterator::next_chunk
Or
Iterator::array_chunksIterator::next_array_chunk
So I'm finding myself caring less and less about the actual naming. I've wanted this feature at least 3 times in the last year and had to write my own version to get around. Can we just pick something and get the feature stabilized?
I believe the current implementation also suffers from this compiler optimization issue: #110734
Godbolt: https://godbolt.org/z/Te5Wo9eh7
I'm not sure what that issue has to do with next_chunk specifically. It seems to be mostly about the codegen for it.collect::<Vec<_>>(), which I think is easily disturbed by anything mutating the iterator.
Let's get this stabilized. Here are my answers to the Unresolved Questions:
Naming: other options include
next_array()ornext_array_chunk().
next_chunk seems like the best option to me.
Should we also add
next_chunk_backtoDoubleEndedIterator?
Probably, but that should be under a separate feature and not block this.
How should we handle N = 0?
It should be allowed and return a zero-length array without advancing the iterator. No good reason to forbid it.
It should be allowed and return a zero-length array without advancing the iterator. No good reason to forbid it.
For next_chunk it might not be a problem but for the chunks iterators on slices and for iterator.array_chunks() it is. So we might as well do something about it here
I disagree. For those, it's necessary by definition. There's no reason to apply the same logic here besides, in my opinion, a misguided desire for consistency. The behavior of next_chunk is totally reasonable for array length 0, unlike the others you mentioned.
"Doing something about it" will pretty much require const expressions just like all the other ones, needlessly delaying the stabilization.
I can see that maybe being the case down the line unless we add something like const if or const match. But currently they just panic when N=0 so one can just put a simple if N == 0 { return Ok([]); } at the top which will easily get optimized out.
Edit: actually the above does NOT work and would need some kind of const if as well. I'll need to think about this.
It's possible to achieve a similar thing via specialization but it's not ideal.
@rust-lang/libs Is there anything blocking a stabilization pull request? I'm happy to open that
I think for the name next_chunk it's slightly unfortunate that a "chunk" in this context is an array and not a slice, like slice.chunks(). This seems inconsistent and a clear potential source of confusion. next_array_chunk or similar removes any such ambiguities, and should be preferred, in my opinion.
If things are otherwise blocked on the handling of N == 0, would it be possible to delay this decision by panicking (at compile time?) for now, and then possibly allow it in the future? My understanding is that the case N == 0 is an edge case that would only come up in some unusual situations involving generics, and it seems unfortunate for such an edge case to delay stabilization for the common use cases of this method.
I think for the name next_chunk it's slightly unfortunate that a "chunk" in this context is an array and not a slice, like slice.chunks(). This seems inconsistent and a clear potential source of confusion. next_array_chunk or similar removes any such ambiguities, and should be preferred, in my opinion.
Whether a chunk is a slice or an array already is not consistent. E.g. slices already have as_chunks that returns a reference to arrays. I don't see much of a chance for confusion because -> [Self::Item] signatures are not possible and -> &[Self::Item] signatures would be neither practical nor useful to implement.
If things are otherwise blocked on the handling of N == 0, would it be possible to delay this decision by panicking (at compile time?) for now, and then possibly allow it in the future?
That would block the option of adding a where N > 0 bound once that's possible
Whether a chunk is a slice or an array already is not consistent. E.g. slices already have
as_chunksthat returns a reference to arrays. I don't see much of a chance for confusion because-> [Self::Item]signatures are not possible and-> &[Self::Item]signatures would be neither practical nor useful to implement.
Fair point, though I don't think any of these methods have been stabilized, unless I'm mistaken. as_chunks would probably also be better as as_array_chunks, for the same reason.
Actually, upon reviewing this post I see that last_chunk was recently made available in 1.77. Unfortunate. Oh well. In that case, you are correct, and that it's already unfortunately inconsistent.
In any case, the confusion I point to has to do with readability. By using clear and distinct naming, you do not have to stop and think through a logical argument like the one you suggest when scanning over the code.
I just don't see any downside in using a few more characters to avoid the ambiguity. But I also don't think we need to bikeshed this further.
That would block the option of adding a
where N > 0bound once that's possible
Thanks for pointing this out, that's what I was missing.
Before making this stable it would be useful to improve the documentation so that it provides an example with a loop. Now the examples have fixed amount of calls to next_chunk. I would think the more common use case is when not operating on a known input.
https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.next_chunk
Hi guys,
Just wondering if any of you also experienced some stack overflow errors using this feature?
I'm trying to use it (inside a Tokio task) but whenever I try to get a chunk of 475 or more element I get a stack overflow error. This is fine up to 474 elements.
I'm iterating over huge CSV files using the csv crate. I wanted to simplify a bit my code which was calling next() on my iterator and executing some code every 10_000 elements but I think I'll have to stick to my existing working code for now.
Just wanted to know if anyone else also stumbled on this issue?
Thanks
The generated assembly is pretty bad.
Godbolt
Benchmarking on my PC, the one that doesn't use next_chunk is almost 3 times as fast.
EDIT: Still an issue on 20/08/2026.
The generated assembly is pretty bad. For some reason, the hot loop contains multiple checks that seem unnecessary.
Godbolt
That's using the default implementation which uses the internal array::iter_next_chunk, which I thought would be better optimized than that... CC @scottmcm didn't we have a trustedlen version of that at some point?
We can obviously do much better for slices.
I'm iterating over huge CSV files using the csv crate. I wanted to simplify a bit my code which was calling next() on my iterator and executing some code every 10_000 elements but I think I'll have to stick to my existing working code for now.
Arrays are usually stack-allocated if you don't box them. So using deep call chains with huge array arguments will lead to stack overflows unless most of the moves get optimized away. We'd need placement by return or changing the next_chunk signature to pass maybeuninit slice references, which would make this a lower-level method.
The use of Result return type seems a bit out of place since the Err is not a regular Error as one could expect. I think it would make more sense to return an enum with the states Chunk([u8;32]) and Remainder(iter) instead
The use of Result return type seems a bit out of place since the Err is not a regular
Erroras one could expect. I think it would make more sense to return an enum with the statesChunk([u8;32])andRemainder(iter)instead
The Result-based signature feels similar to slice::binary_search() -> Result<usize, usize> and slice::contains() -> bool
(Commenting after the meeting that discussed #100450 (comment) )
I suspect the fundamental problem here is that returning -> Option<[T; N]> (or result, or whatever that's not just the array or just a pointer) is rough from an ABI perspective. Something wanting a [T; N] will have space for that somewhere, but Option<[T; N]> is almost always bigger than that (particularly for the common cases of Option<[u8; N]>), and both end up with Memory ABIs, so they're returned via out pointer. But that means that next_chunk().unwrap() gets space for [T; N], but then needs to stack-allocate space for Option<[T; N]>, get the optional chunk into there, then copy the array into the return place. (That's part of how #107634 made map better.)
Fixing that would mean, I think, an API like
trait Iterator {
fn fill_slice<'a>(buffer: &'a mut [MaybeUninit<Self::Item>]) -> slice::Dropper<'a, Self::Item>;
}Where you can DerefMut<Target = [T]> the slice::Dropper<'_, T>, it drops the elements on drop, it's up to you to check how many were actually filled, etc. That way the return type is always the same size (it's just a wrapper on slice::IterMut) and ScalarPair so you don't have the ABI problems.
(Maybe also an array::Dropper so we could have fn fill_array(…) -> Result<array::Dropper<T, N>, slice::Dropper<T>>; for unwrapping or something. Dunno. Or something smart where the array version can unsize into the slice version, or something.)
That said, it really wants a promise that the returned dropper's pointers would be into the passed buffer, which would normally mean it'd need to be on an unsafe trait which is annoying for iterators since one would want this to always exist.
Maybe there's a way that fill_slice and fill_array would just be final fn, and there's other things that people can optionally implement that we'd call to populate the slice? Feels like anything that can do it materially faster is something with at least a helpful size_hint lower bound (since otherwise it'd need to go one-by-one and the default impl will be fine) so maybe there's a different version that can request the tristate: fill the whole thing, do nothing, or say you don't have enough size information to know. Then we just call that repeatedly -- which in practice on things like slice::Iterator is always exactly once.
Fundamentally the way to solve such issues is to change the way Result and Option are returned in the ABI. Specifically we want to have 2 separate return places, one for the "main" result (Ok/Some) and one for the rest of the enum. When the main result is returned via an out pointer, this allows the result to be directly placed into its final location. This along with the current work on MIR move optimization should allow these to optimize optimally.
I would much prefer this kind of ABI-level solution over making the API more complicated because of low-level implementation details.
The other alternative here is to piggy-back on some future language feature for in-place initialization, which also has to address fallibility somehow. The downside of transparently making the current API lower to "out-ptr for Some/Ok payload" ABI is that it also fails transparently: whenever move elimination doesn't fire for any reason, or a pointer to an entire Result<[T; N], _> is required, codegen will be just as inefficient as today, without any clue at the source level.
In contrast, most in-place-init proposals have some explicit opt-in at the call site that guarantees no extra moves or temporaries. That's necessary for soundness in some use cases, but it's also useful for types (here: array lengths) large enough to overflow the stack. The downside is that there's some extra annotation burden, which may be a bad trade-off for an API that's partly about convenience. But depending on which (if any) approach is adopted, maybe the common case is succinct enough to be attractive for this API, too.
Are we able to add DoubleEndedIterator::next_chunk_back underneath this tracking issue? The person who made the ACP for this method and the original PR had it marked under #![feature(iter_next_chunk)] feature gated. I picked up the PR here to get it over the finish line due to inactivity.
@aapoalas to note, .next_chunk_back() should be underneath the DoubleEndedIterator trait not Iterator trait.
View all comments
Feature gate:
#![feature(iter_next_chunk)]This is a tracking issue for the
.next_chunk()and.next_chunk_back()method on iterators which allows you to advance the iteratorNelements and return an array[T; N]Public API
Steps / History
Unresolved Questions
next_array()ornext_array_chunk().N = 0?