4 ms·
Because "shrink_to_fit" involves calling a "realloc"-like function ("realloc" with a lower value is the best way for returning unused memory back to the system
by faragon 11y ago
Because "shrink_to_fit" involves calling a "realloc"-like function ("realloc" with a lower value is the best way for returning unused memory back to the system when you have a buffer with too much memory allocated for it), that could be "expensive" -if doing that many times in a period of time- (even with modern realloc implementations using virtual memory remap stuff).
P.S. you can find a human-readable "reserve"/"grow" (with geometric heuristic grow)/"shrink_to_fit" vector implementation for C (similar idea to the C++ counterpart, except for using just one allocation, instead of 2 -one for the instance, and another for the buffer-): https://github.com/faragon/libsrt/blob/master/src/sdata.c https://github.com/faragon/libsrt/blob/master/src/sdata.c