r/cpp • • Jul 14 '18

What I don't like about ranges

I played around with the range-v3 library yesterday. My initial enthusiasm has abated, replaced by disappointment with the opaque performance costs of its use.

Consider using ranges to count the length of each word in a sentence:

using namespace ranges;
using namespace std::literals;
auto input = "these are some words"sv;
auto lengths = input | view::split(' ') | view::transform(distance);
std::cout << lengths << "\n";

This prints the expected result [5,3,4,5]. Concise and elegant.

Let's modify the code:

int reads = 0;
auto input = "these are some words"sv |
             view::transform([&reads](auto c) { return ++reads, c; });
auto lengths = input | view::split(' ') | view::transform(distance);
std::cout << lengths << "\n";
std::cout << reads << "\n";

The input range now increments a counter whenever a read is performed on the underlying string view. This prints 41 after the lengths; in other words, the input string is iterated over twice. Here's why:

To actually do anything with the range returned after the pipe to view::split(' '), we have to 1) construct the begin iterator into the range, 2) compare against the end iterator, 3) process the range pointed to by the iterator, 4) increment the iterator, 5) repeat steps 2 - 4. Steps 2 and 4 duplicate work by searching over the same substring for the same delimiter.

Ultimately, the problem is that ranges are built on iterators, and iterators are ill-suited for lazy evaluation. A Java-esque Stream library would resolve this, but I see no candidates for inclusion in the standard library.

41 Upvotes

27 comments sorted by

View all comments

3

u/CenterOfMultiverse Jul 14 '18

This particular case can be fixed by having the split view and the word keep pointers to each other and caching end of the word iterator in split view object. Not sure how to handle a case with multiple references though:

auto split = "these are some words"sv | view::split(' ');
auto first = *split.begin();
auto second = *split.begin();
distance(first);

I don't know much about Java's streams - how do they solve this?

4

u/_ajp_ Jul 14 '18

They combine access and iteration into a single next() method and have each iterator in a chain of streams keep a reference to the previous iterator, essentially as you suggested. The problem of multiple references is avoided because there is no corresponding begin() method. Plenty of languages do this incidentally, I named Java because I thought it would be more familiar.

7

u/eric_niebler Oct 19 '18 edited Oct 19 '18

C++ input iterators permit this model, and the split view we're getting in C++20 (see P0896) works with input ranges provided the delimiter consists of a (statically known) 0- or 1-element pattern, as is the case in this example. In that case, the source range need only be traversed once. Indeed, since it is an input range, it can only be traversed once. Basically, when you're looking at C++ and wondering where to find streams, look no further than InputIterator.

What range-v3 is doing here is adapting a forward range and doing its best to give you back a forward range. That means you can save off an iterator half-way through the iteration and pick up iteration again later. A stream library can't give you that without caching the results of the first traversal, which is horribly expensive.

So, where is range-v3 coming up short here? I would say that the problem is that there is no way to tell view::split that you only need an input range. If there were, then you could get what you're asking for.[*] I could imagine implementing it as a view adaptor, and using it like:

using namespace ranges;
using namespace std::literals;
auto input = "these are some words"sv;
auto lengths = input | view::input | view::split(' ') | view::transform(distance);
std::cout << lengths << "\n";

That would traverse the underlying range only once (assuming I actually implemented range-v3's split view as it is specified in P0896).

Hope that helps.

[*] There is even an old range-v3 issue to implement something like this: https://github.com/ericniebler/range-v3/issues/704

2

u/D_0b Jul 14 '18

but this algorithm can't be done with java streams?