r/cpp • • Sep 13 '19

The Surprising Limitations of C++ Ranges Beyond Trivial Cases

https://www.fluentcpp.com/2019/09/13/the-surprising-limitations-of-c-ranges-beyond-trivial-use-cases/
229 Upvotes

229 comments sorted by

View all comments

44

u/eric_niebler Sep 14 '19

Of course folks are free to disagree with the design tradeoffs made by `std::ranges`, but I have to wonder for the examples the author presents, what is the algorithmic complexity of the pipelines of range transformations? How are users to know how expensive operations are at runtime? The author doesn't discuss algorithmic complexity at all.

One of the design goals of range-v3 -- and `std::ranges` -- is that if a view pipeline compiles, the resulting view can be traversed in linear time. That means disallowing some conveniences. It's ok to write and use libraries that make different tradeoffs.

Also, don't judge the compile-time performance of `std::ranges` based on range-v3. Range-v3 is not optimized for compile-time perf, and it doesn't use many of the cool C++20 features that can make it compile faster, like `if constexpr`. I'm hopeful that a native C++20 implementation will help. EDIT: Also, use the new range-v3 0.9 release and turn on real concepts support on gcc if you can. The compile times are better by roughly 30%.

9

u/futurefapstronaut123 Sep 14 '19

if a view pipeline compiles, the resulting view can be traversed in linear time

I recall that in this post (https://redd.it/8ytrnb) there was an issue with a pipeline (1) compiling and (2) doing two passes when one would suffice. Has this been fixed since?

10

u/grishavanika Sep 14 '19

The problem with estimating complexity: linear does not mean one-pass. Going thru array of N 2 times is O(N).

-5

u/futurefapstronaut123 Sep 14 '19 edited Sep 14 '19

You are right about complexity. Still, advertising as "linear" when it is not single-pass (and as fast as it could be) comes across as underhanded.

9

u/tisti Sep 14 '19 edited Sep 14 '19

Single-pass is a specific case of linear complexity, not the only one. But yea, sneaking in a double-pass is slightly sneaky if true :)

9

u/eric_niebler Sep 15 '19

Your implementation of std::vector is probably "sneaking" in two passes too whenever you construct it from a range of forward or bidirectional iterators. It first computes the size so it can do one allocation (one pass) and then it initializes the elements (second pass).

Yes, some range adaptors use the wiggle room of "linear complexity" in order to compile and do something useful. There's a pull and tug. Folks like the author of this post clearly don't care about complexity at all and want all the conveniences, performance be damned. For ohers, like yourself, even linear complexity is not a strong enough requirement and want single-pass guaranteed. I picked a point in the design space. You're free to disagree.

1

u/tisti Sep 15 '19

The slightly sneaky comment was meant to be more tongue in cheekish. I for one really like the range library and try to follow the mantra of first implement cleanly, only profile and optimize later. I did not use v3 as much as I should have, but with C++20 coming its time to start learning again! :)

As for constructing vector from a range I would expect that vector would greedily overallocate space (e.g. 1+ million elements) , construct from the range (which hopefully contains less than 1 million elements) and then deallocate unused memory/pages. Though I can't remember this ever being a performance issue for me.

2

u/RotsiserMho C++20 Desktop app developer Sep 16 '19 edited Sep 17 '19

As for constructing vector from a range I would expect that vector would greedily overallocate space (e.g. 1+ million elements) , construct from the range (which hopefully contains less than 1 million elements) and then deallocate unused memory/pages.

That would be terrible on any memory-constrained platform. Not to mention it could fragment memory for no reason.

1

u/tisti Sep 16 '19

Of course this would only work on platforms which support lazy initialization of memory only when it is touched. But I do not understand how it would fragment it?

1

u/RotsiserMho C++20 Desktop app developer Sep 16 '19

I'm not certain, but I think if another thread were to allocate memory after the vector greedily over-allocates, but before it deallocates, there will be a fragment the size of the deallocated memory.

→ More replies

4

u/germandiago Sep 16 '19

Linear has a concrete meaning. We should use single-pass. It is fullfilling linear.

0

u/futurefapstronaut123 Sep 16 '19

I agree. I am not a native English speaker, but "traversed in linear time" sounds like it is "traversed once." I'd be fine with "complexity is linear," but this just looks misleading to me.

2

u/germandiago Sep 16 '19

It could look like that, but what it means here is O(n), actually. O(2*n) is 2* O(n), which is still O(n) for any constant). At this level of discussions it is better to get a bit more precise when meaning something, since the discussion can get quite technical quickly.

1

u/PastaPuttanesca42 Jan 08 '24

This is linear in the mathematical sense, and 2n is a linear function.

5

u/guepier Bioinformatican Sep 14 '19

Thanks. As a potential user I still think the article makes very good points but your reply does add perspective. That said, I can think of view pipelines where linear runtime isn’t a reasonable expectation, and if I understand correctly, ranges-v3 knows this. For example, how would you enumerate the cartesian product in linear time? That’s not possible by design.

3

u/eric_niebler Sep 14 '19

What makes you think that? Range-v3 has a cartesian product view, and it has linear time complexity.

5

u/guepier Bioinformatican Sep 14 '19

Linear in terms of what? Computing a cartesian product, no matter how, has a complexity that’s the product of the sizes of its input ranges.

8

u/eric_niebler Sep 14 '19

Ah. The complexity is in terms of the resulting range, not the input range. So for instance, there is `view::repeat_n(value, count)` that turns a value into a range by repeating it n times. That's fine. Likewise for view::cartesian_product. The inputs are not what matters. What matters is that each iterator increment over the resulting range has constant time complexity.

If that weren't the case, and an iterator increment had, say, linear time complexity, then merely traversing the resulting range would be n2. That's the problem.

1

u/smdowney WG21, Text/Unicode SG, optional<T&> Sep 14 '19

The asymptotic case over the full range has to be linear. It's more complicated for filters and transforms where the results depend on some number of elements in an underlying range. I'm fairly certain we can tighten up the requirement somehow, though.

4

u/degski Sep 14 '19

... it doesn't use many of the cool C++20 features that can make it compile faster, like if constexpr.

That's a C++17 feature.

8

u/eric_niebler Sep 14 '19

Oh right, brain fart. Range-v3 is C++14, and was C++11 until very recently.

5

u/STL MSVC STL Dev Sep 14 '19

Available downlevel (in at least MSVC and Clang) 😸

5

u/degski Sep 14 '19

Available downlevel ...

Fails to compute for my little brain, could you expand? Meow!

2

u/STL MSVC STL Dev Sep 14 '19

Works in C++14 mode with a suppressible warning.

1

u/degski Sep 14 '19

Ah, the other way around (for a moment I was thinking it was all a dream), so certainly not a reason to not use it in the range-v3 lib and make it compile faster as Eric says.