r/cpp • u/Main_Pay_3213 • May 25 '26
undercurrent: A proof-of-concept library to fix range adaptor inefficiencies
Hi, I'm a hobbyist programmer and I recently came across Barry Revzin's blog post about inefficiencies in the C++ ranges library when filter or reverse is mixed into an adaptor chain. I wanted to see if I could do something about it, and after some experimentation I ended up with this library: undercurrent.
The core idea is a customization point object uc::advance_while, which descends the iterator hierarchy recursively rather than operating at the top level. This allows algorithms to do their work at the lowest iterator level, avoiding redundant predicate evaluations.
I observed a significant speed improvement with an adapter chain like take_while | transform | filter | reverse. On Clang 22 + libc++, I'm seeing roughly 16x speedup over std::ranges. Though MSVC shows a smaller improvement (~2x). Currently supports a minimal set of adaptors and algorithms. GCC is not yet working, likely due to module-related issues.
I'd love to hear your feedback, thoughts, or any edge cases I should consider!
GitHub: https://github.com/atstana/undercurrent
Barry Revzin's blog: https://brevzin.github.io/c++/2025/04/03/token-sequence-for/
9
u/mpyne May 27 '26
If you've thought about this at all you're probably better qualified to give feedback on this than I would be, but I just wanted to note that it's neat that someone's looking at it. Even where they're not perfect I've liked std::ranges overall so having ways to make them even more performant is a win in my book.
Especially if they can be made to work with GCC, lol.
4
u/fdwr fdwr@github 🔍 May 27 '26
I'd find it interesting to see a table with the different iteration approaches used in libraries, like
std(begin,end,*it,++it), Tristan Brindle's Flux (first,is_last,inc,read_at), undercurrent (if it differs in anyway fromstd), plus others (I haven't heard of transrangers or daisychains before). I'll have to read this in more detail later... ⌛3
u/Breadfish64 May 27 '26
One of the libc++ developers at CppCon mentioned that they do this for some segmented STL iterators:
https://github.com/CppCon/CppCon2025/blob/main/Presentations/Implement_Standard_Library.pdf
But unfortunately it's not exposed for other iterators.
2
u/Main_Pay_3213 May 30 '26
Hi! Author of undercurrent here. I've added support for segmented iterators. First impression: works great with Clang, not so much with MSVC (again). With Clang, code like this runs super fast:
uc::seg_stackis a simple dynamically sized stack with a deque-like structure, made specifically to test the effectiveness of the segmented iterator path. The pipeline above runs about 20x faster thanstd. Comparingucwith the segmented path on vs off, it's about 10x faster. So the segmented optimization alone gives a clean 10x on top of undercurrent's baseline. Happy to hear thoughts if anyone has them.0
u/Paradox_84_ May 27 '26
TL;DR if you want some features to exist without unnecessary overhead, they should be language features. You cant achieve some stuft with libs alone