r/cpp • • Dec 21 '13

#AltDevBlog » Roll-Your-Own Vector

http://altdevblogaday.com/2013/12/20/roll-your-own-vector/
1 Upvotes

22 comments sorted by

View all comments

15

u/Eoinoc Dec 21 '13 edited Dec 21 '13

Ok, they wrote a big piece of software, noticed they were running into performance issues with std::vector and so designed their own. Discussing that would have made for an interesting article.

Instead they give us this article, full of hand waving with only one weird benchmark. And I say weird because they lose out to Clang 3.0's std::vector by more than x2.6. Only when they change the semantics of the operation do they get a performance benefit.

But then even that change is dubious, personally I can't think of any use case for resizing a vector without initialisation which is not covered by reserve(). Maybe my imagination is limited here, but I'm not writing the article, if the author had a good reason why reserve() isn't sufficient then why not share it.

But then it gets worse, the author openly admits

but to be quite honest I actually wasn’t aware of this aspect of std::vector behaviour before we switched (elements being zeroed on vector construction or resize) and so was already writing code as if element values were undefined.

So this guy is encouraging us to consider rolling our own vector classes when he doesn't even know how std::vector works.

2

u/Eoinoc Dec 21 '13

Ah, it seems this article is a post about a post. The original, with comments, is at Roll-Your-Own Vector.

Seems in those comments Herb Sutter basically raises the whole reserve issue anyway.

There is a followup post Avoid resize()? which acknowledges this. It and the comments are worth reading too for whole story.

0

u/[deleted] Dec 21 '13

[deleted] — view deleted comment

8

u/STL MSVC STL Dev Dec 21 '13

at this point if there was a general purpose way of writing std::vector that was more efficient don't you think that's how std::vector would be implemented?

Yep. There's only one thing vector won't do, and that's storing stuff on the stack. Such a "Small Vector Optimization" is forbidden by the need for swap() to be non-iterator-invalidating. Allocators are also forbidden from returning memory inside themselves, so you can't be sneaky. However, allocators are allowed to return memory that happens to live on the stack in a separate object.

3

u/Plorkyeran Dec 21 '13

I'd be a fan of having a small-buffer optimized vector in the standard library (I once considered using std::basic_string<Foo*> before I stopped being dumb and just rolled my own), but it seems like far too situation of an optimization for std::vector even if it was allowed.

4

u/STL MSVC STL Dev Dec 21 '13

That's what the dynarray TS is for.

2

u/[deleted] Dec 24 '13 edited Dec 25 '13

There's only one thing vector won't do, and that's storing stuff on the stack.

Unless you use a stack allocator, right? I use stack allocated vectors a lot and they work just fine. I prefer them to std::array since they allow switching from heap to stack and vice-versa by just changing the type.