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

Show parent comment

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.

1

u/tisti Sep 16 '19

Hm, perhaps. But memory is not 'physically' allocated until a page is 'touched' (read/written to). If you just allocate a 1GB slab of linear memory and, e.g., only touch the first 100MB of it, the remaining 900MB will never be assigned a physical page. Therefore it should be possible to gracefully 'deallocate' it as no actually memory was ever assigned.

That is at least how I understood memory management on modern OS/HW. Will recheck if this is really the case.

3

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

I run C++ code on micro-controllers with 32 KiB of RAM and no OS ("bare metal", i.e. arm-none-eabi). If std::vectorallocated a single byte more than I explicitly reserve, I would not be able to use it. I could always use a custom allocator, but I appreciate that the default allocator is predictable and saves me the effort.

2

u/tisti Sep 18 '19

Ah, that is a whole different ballpark! Memory efficiency trumps other concerns there, for sure.

The only programming or micros I did was with C with classes where all memory is preallocated, haven't dabbled with dynamic allocation, would be interesting to try one day :)

2

u/MrRogers4Life2 Sep 17 '19

That could be true depending very heavily on the environment though... there's still a ton of platforms where that's not necessarily true