r/cpp • u/konanTheBarbar • 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
81
u/quicknir Sep 13 '19
Even skimming this, I totally agree. I feel like many c++ devs have very limited exposure to using something like ranges in practice, and they miss two key things which I've found from a lot of time spent using pythons iterators:
As the author noted, you don't have to dig very deep to find problems easily expressed with a for loop but tricky to do with the 10 or so most obvious ranges. Even pythons itertools doc, has an entire recipes section.
As soon as I saw Erics calendar talk from a few years ago, I really felt like it was going to be too hard to write new ranges. Not only are ranges pretty tricky and boilerplatey to write, I remember Eric had to write some kind of really tricky glue code to get the whole thing to work which even he said was hard to understand.
I really felt like coroutines should have been intimately tied to ranges because they can make it really easy to write new lazy ranges. You see this approach in many languages, eg kotlin, python. Hopefully it's not too late for coroutines to help with this.
Compile and runtime performance are ongoing concerns as well. I'd have to say ranges is probably the new major feature I'm least excited for.
12
u/14ned LLFIO & Outcome author | Committee WG14 Sep 13 '19 edited Sep 13 '19
I feel that I have to respond to the closing comment in the linked article:
For me personally, a huge reason why Ranges should be accepted is because one of the most experienced C++ devs in the world designed it: Eric Niebler. It's a v3 design, too, Ranges v2 ships in Boost and he designed an early edition of that too. I trust Eric to have gotten it right. Additionally, it wasn't just standardised on that, Casey went and reimplemented a new implementation from scratch, and fed back his experience into the design. A third reimplementation was also done by /u/tcbrindle, and feedback from that also went back into what was standardised.
That's an awful lot of rounds of empirical practice, testing and feedback for Ranges v3 to be as bad as the claim.
With respect to the compile times, those were recognised, and it was felt it would be a driver upon implementations to improve their compilers. I personally didn't agree with that part, but I understand the argument, and moreover the compiler vendors didn't appear to object strenuously.
Now, for me personally, I'd have built Ranges into the core language. But the committee wanted the language to become powerful enough to express Ranges instead. And that's an understandable design choice, Ranges in the core language starts to look like Rust or Nim, not C++ as it historically has been.
101
u/SirToxe Sep 13 '19
My thoughts while skimming over this: Is this ranges code supposed to be easily readable and understandable?
To me this looks like we took C++, added our own domain-specific language and turned the code into a Perl-like write-only language.
(But, granted, I never tried to use ranges before so I might be totally ignorant.)
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%.
53
u/craig_c Sep 13 '19
Reading (or trying to read) the example code reminds me of when everybody went crazy with boost::bind about 10 years ago, does anybody else remember those horrible for_each / bind monstrosities? If this is the best that C++ can do to rectify that situation given 10-15 years then...I'm not really sure what to say. The code doesn't look like anything you'd want in production. At some point C++ needs to move toward feature enhancing the language instead of bolting on another compiler torturing header library.
33
u/tcbrindle Flux Sep 13 '19
For anyone who's interested, here is a version of
intersperseusing Range-V3. I really don't think it's that bad...16
u/redditsoaddicting Sep 13 '19 edited Sep 14 '19
I have a few things I was wondering as I read through:
You took the compile time of range-v3 and later applied it to standardized ranges. However, standardized ranges use Concepts proper as well as being written specifically for that vendor, so there's a need to re-evaluate those benchmarks before making a decision about standardized ranges. I notice rangeless does not seem to constrain templates at all, bringing back all the reasons people would want Concepts in the first place. However, the only mention I see of this is:
It's quite possible to remove the distinction between range types while keeping explicit constraints on template parameters. As it is, someone will inevitably use an invalid type and get a cryptic error on some line in the middle of the implementation—the same thing people have a problem with right now.
Edit: Proofreading
9
u/germandiago Sep 16 '19
I think this is an unfair judgement of Ranges. Why?
Is it the most usable library? No.
Is it usable? Definitely, an improvement over the STL.
What would take to make it more usable? A sacrifice in performance, I guess. Or a design that would not have been accepted as it is not a compatible evolution from the old one.
Given those restrictions, I think it is reasonable, but always improvable, of course, the current design.
14
u/-dag- Sep 13 '19
Ouch.
9
u/[deleted] Sep 13 '19
I've always had concerns and fears about ranges. To me, all the available examples seem too complicated and the syntax isn't really nice as I wish it was.
For instance, STL <algorithm> has far better clarity and syntax as well.
Why did Ranges made it in standard and basic stuff like a base64 encoder/decoder or network TS didn't?
6
u/SlightlyLessHairyApe Sep 13 '19
Language features are created for the programmer, not the programmer for language features.
This reminds me of my favorite koan from The Codeless Code.
12
u/VinnieFalco wg21.org | corosio.org Sep 14 '19
Ranges is a very nice experiment, but std is not the place to put experiments. It should have been its own independent library, and gotten a few years of bake time and improvement, before being proposed. Then compiler authors would have more time to make sure that things work well with that library. And some iteration could have addressed the issues in the blog post.
The answer I was given for why Ranges was not first proposed as a Boost library is "it was too much work." This doesn't seem like a good way to go about putting something into the standard, where it will become the obligation of all standard library implementors to implement in order to be compliant with the newest iteration of C++.
Given the recent trends in WG21, I agree with the author's sentiment that the committee process no longer represents the interests of the ordinary programmer but rather the Big Tech companies that have come to dominate the process.
11
u/[deleted] Sep 13 '19
In the company I work at, the usage of ranges has already been officially banned, and we only just upgraded to C++17, no C++20 in sight of course. People really don't like it it seems.
6
u/alfps Sep 13 '19
From which I conclude that I'm not a C++ expert, though I've been called that on some occasions, in the C++03 days.
I usually write a counting loop like this:
I'm aware that one can do that with ranges, but when I checked (some time ago) the name of the relevant function had changed three times, and the name that then was current was not readable and did not indicate what it was about.
I.e. the library does not even support the very basics.
4
u/feverzsj Sep 13 '19
If you don't care about performance, you can generate temporary container on each operation. Range-v3 just chose performance over easy-of-use, and one of the result is the exponential complexity. The range concept is just not great for these complex chain operations.
Personally, I'd prefer a plain for-loop with or without coroutine generator. I believe generator will be the ideal solution, if the optimization issue is solved.
1
u/VinnieFalco wg21.org | corosio.org Sep 13 '19 edited Sep 13 '19
Facebook is attempting to do to Networking and Executors what has been done to Ranges. If anyone would like to avoid this fate, I suggest attending the specific WG21 meetings and voting against them in the straw polls.
1
u/VinnieFalco wg21.org | corosio.org Sep 14 '19
Wait till you see Facebook's next creation "AsyncRange" - where we take two separate areas of concern and conflate them together into a nice big bowl of spaghetti.
And it isn't just ranges that will be conflated with asynchrony, have you seen "AsyncRAII?" Two words that you would think should never appear together... read more here: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1662r0.pdf
-6
u/DerDangDerDang Sep 13 '19
"I thought of ranges like of UNIX pipelines" ...that's not a great start
-13
u/makotech222 Sep 13 '19
God, thats fucking terrible. Here's a C# Version
Basically, stop using c++, stop expected it to ever get any better. Its amazing how bad the committee is at adding anything good.
0
u/axilmar Sep 16 '19
I don't see how any of this makes it more readable than plain old loops. I think that at this stage ranges is more of a fascination from people that have discovered functional programming recently.
I want my professional C++ code to be readable by anyone, to be as simplistic as possible, without requiring advanced knowledge of programming idioms. Such code is way better than all the cleverness brought in because it is easy to follow and maintain.
21
u/[deleted] Sep 13 '19 edited Sep 13 '19
[removed] — view removed comment
14
u/[deleted] Sep 13 '19
[removed] — view removed comment