Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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().



Malloc 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.


Isn't that basically what slab/pool type allocators help with or am I being an idiot?


> 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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: