3 ms·
The size increase factor for vector is a compromise between performance and wasting memory. It's also a fairly hot code path, so you don't want to run some comp
by jabl 5y ago
The size increase factor for vector is a compromise between performance and wasting memory. It's also a fairly hot code path, so you don't want to run some complicated code there to estimate the 'optimal' factor.
About the best you can do is if you know beforehand roughly how big it will be, is to reserve that capacity with std::vector::reserve().
- mhh__ 5y agoMalloc is at best a few hundred cycles, that's quite a lot of work if you can keep your data in L1. If you do a bit of work now you can save reallocs later. Think bigger than changing the coefficient, there probably is no optimal factor.
- mst 5y agoIsn't that basically what slab/pool type allocators help with or am I being an idiot?
- account42 5y ago> About the best you can do is if you know beforehand roughly how big it will be, is to reserve that capacity with std::vector::reserve(). But be careful not to call reserve() in a loop adding data to a vector - will allocated exactly what you request and not do exponential growth.