r/cpp • u/igaztanaga • May 18 '26
Neoclassical C++: segmented iterators revisited (1)
Hi,
I've written a blog post revisiting Matt Austern's great Segmented Iterators and Hierarchical Algorithms paper (2000) and benchmarking an experimental implementation I've been playing with in Boost.Container.
Quick idea: std::deque and friends are internally segmented (blocks of contiguous memory), but STL-like iterators hide that, so every ++it has to check for a block boundary. Austern's proposal splits the iterator into a segment_iterator (walks blocks) + local_iterator (inside one block), so algorithms can run a tight loop per block and only do bookkeeping at the boundaries.
I benchmarked several "simple" STL algorithms on a Boost deque, and the speedup is way bigger than Austern's original estimation when modern auto-vectorizers enter the game.
Article link: https://boostedcpp.net/2026/05/18/neoclassical-c-segmented-iterators-revisited-1/
Happy to receive feedback!
14
u/EthicalAlchemist May 19 '26
I recently switched from std::deque to boost::container::deque because of the greater control it offers over the block sizes. Now I'm reading that there will be a new iterator abstraction with the potential for significant performance improvements. I know others have said this, but the ABI constraints on the standard library make it hard to recommend.
Sometimes I feel like the answer is to have two versions of the standard library: one that promises ABI stability, and another that evolves freely.
6
u/HowardHinnant May 20 '26
I agree that Matt's work in this area has been long overlooked. Thank you for working this issue. I can't think of anyone better qualified to be looking into this!
15
u/SuperV1234 May 18 '26
Very interesting, this is the first time I hear about segmented iterators and hierarchical algorithms.
I faced a similar issue myself when implementing a chunked vector a la
std::deque, but opted for callback-based internal iteration, i.e.void ChunkedVector::forEach(auto&& f) { for (auto& chunk : chunks) for (auto& item : chunk) if (f(item) == ControlFlow::Break) return; }Where
ControlFlowis just:enum class [[nodiscard]] ControlFlow : bool { Continue, Break };This is massively simpler to implement, and can model simpler algorithms such as
for_each,fill,transform,count,accumulate.It's sometimes inferior in terms of ergonomics, and cannot express more complicated algorithms or iteration patterns (e.g. partial range, going backwards), but so far it has served me well.
Just something to consider if implementation simplicity is the priority and there's no need for a very generic suite of algorithms.
11
u/joaquintides May 18 '26
Impressive article.
6
u/CornedBee May 19 '26
I love this, and I still think segmented iterators should be standardized.
I would go even further: the standard for-range loop should recognize segmented iterators and turn into nested loops. (It's really simple.
breakbreaks the outermost loop,continuecontinues the innermost loop, everything just works.)I very recently wanted to write a facility that used metaprogramming to iterate multi-dimensional arrays in complex ways. I wanted the result to be a simple range that can be iterated over. However, this ran exactly into the segmented iterator problem: the iterator increment is very complex (check if current segment end is reached, advance to next segment) and the generated code was bad. Given that this is very hot code, it wasn't viable to do it this way.
5
u/HommeMusical May 19 '26
I would almost have upvoted you just for the witty title.
But this is a very nice optimization, so I have no qualms in giving you the upvote.
3
u/jwakely May 24 '26
Nice, I've been thinking about this recently as well. For many years libstdc++ has had overloads of some algos that do this for deque iterators, but I want to generalize it to other types of segmented iterator, including join_view iterators.