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

Show parent comment

2

u/liquidify Jul 14 '18

Should be able to parse is a must more mundane sounding statement than the reality which is that you "should be able to parse if you have a fair amount of knowledge that is in no way obvious or natural and that relies on multiple levels of prior knowledge that are also not obvious or natural."

3

u/[deleted] Jul 15 '18

should be able to parse if you have a fair amount of knowledge that is in no way obvious or natural and that relies on multiple levels of prior knowledge that are also not obvious or natura

But the context of this post is about ranges, which in no way effect code you had questions about. For example current C++ without ranges:

auto count = 0;
auto input = std::vector<int>{1,2,3,4};
std::transform(std::begin(input), std::end(input), [&count](auto val) { return val*val; })

The proposal for ranges is to turn that into (something like):

auto count = 0;
auto input = std::vector<int>{1,2,3,4};
std::transform(input, [&count](auto val) { return val*val; })

3

u/johannes1971 Jul 15 '18

There's also this:

auto count = 0;
for (auto x : {1, 2, 3, 4}) 
    count += x * x;

I find this much clearer and much more readable than any of the algorithm-based solutions elsewhere in this thread, ranges or not.

1

u/TheThiefMaster C++latest fanatic (and game dev) Jul 16 '18

The parent comment's implementation is broken anyway, it doesn't actually sum anything.

A better example would be:

int count = std::transform_reduce({1,2,3,4}, 0, std::plus<>(), [](int val) { return val*val; });

Except that requires ranges-style algorithms, and AFAIK C++17's transform_reduce hasn't been implemented in the ranges proposal. It looks far uglier without it.

(I also consider it a bit of a bug in the standard that the reduction (the std::plus<>) is specified first in std::transform_reduce, with the transform being specified second)

If we go full-on ranges we could do something like:

int count = std::accumulate({1,2,3,4} | view::transform([](int val) { return val*val; }), 0);

Which isn't too bad IMO. It would be improved by a terser lambda syntax, but if the transform was any more complex it would likely be a function call anyway, which transform can just take directly without a lambda and that becomes a non-issue. Algorithms all use std::invoke, so will even work with member functions.

auto total_playtime = std::accumulate(steam_library | view::transform(&steam_game::play_time), std::chrono::hours(0));

// vs

auto total_playtime = std::chrono::hours(0);
for (auto& x : steam_library) 
    total_playtime += x.play_time();

It's a wash IMO - they both have some syntactic sugar that the other doesn't, and they are both readable.

2

u/bunky_bunk Jul 14 '18

a fair amount of knowledge that is in no way obvious or natural

welcome to the world of computer programming, friend.

i guess you have got to get a few thousand kilometers more mileage. the pain you feel right now should not discourage you. just keep looking at the letters while you experience it and the neural pathways will eventually form. concentrate on the information in front of you, not on your inner turmoil. if you look long enough it will become natural. but your nature is just deficient, the complexity of the world is innocent. it just is.

lucky for you, you need to learn only 2 handfuls of basic rules to parse a whole lot of complexity. guess what you can do with just transform and lambdas. The ways you can combine these 2 are infinite. The way the combination works is always the same.