Get the latest tech news
Growing Buffers to Avoid Copying Data
Copying data can be expensive in some cases, especially since it it doesn’t change the data, it’s just moves it. Therefore we, engineers interested in performance, want to avoid copying data as much as possible. We already talked about avoiding data copying in C++ earlier. In that post, we talked about what mechanism C++ has… Read
This function allows us to grow or shrink a buffer allocated with malloc or calloc: if realloc can do it in place, it will avoid copying data. ConfigurationRuntime std::vector 0.998 s Simple 0.899 s Resize 0.495 s Posix 0.522 s Jemalloc 0.547 sAvoiding copies definitely pays off in terms of number. This leads to virtual address space fragmentation and lower performance, for two reasons: (1) the OS needs more time to allocate and free memory and (2) there will be a higher number of data cache misses and TLB cache misses, which can result in overall slowness of the system without an obvious reason.
Or read this on Hacker News