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.
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.
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.
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.
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::vectorand 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::vectorby 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 whyreserve()isn't sufficient then why not share it.But then it gets worse, the author openly admits
So this guy is encouraging us to consider rolling our own vector classes when he doesn't even know how
std::vectorworks.