`std::vector` is often said to be the default container, because it provides good baseline performance for common operations. Recently, I experimented with a simple API change that can improve the performance of a common usage pattern by 10+ %.
The lost performance comes from the fact that vector::push_back has to check whether it needs to grow the vector, even though it will never happen in the convert_to_indices function. A sufficiently smart compiler could optimize this out, but neither GCC nor Clang do (and MSVC makes a complete hash of things).
Ok that one is new to me, I didn’t know that despite the .reserve the compilers are still unable to figure that out.
Ok that one is new to me, I didn’t know that despite the
.reserve
the compilers are still unable to figure that out.