We at Johnny’s Software Lab LLC are experts in performance. If performance is in any way concern in your software project, feel free to contact us.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 to offer when it comes to avoiding or minimizing copying. In this post, we focus more on what the operating system can give us to avoid data copying.A small introductionAmong memory allocation functions in C library, one function is particular: realloc. 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. If not, then it will allocate a new buffer, copy the data there, and free the old buffer.If you are using C, this is all that you need. In C++, things get complicated. C++ doesn’t have a dedicated realloc function. It only offers new and delete for the user. Containers use std::allocator to allocate memory, but again, no buffer growth is available there.If you need your buffers growing, you can opt for custom container implementation with realloc. But, there is a problem of non trivially copyable types: some types cannot be copied using realloc, instead they need to be properly constructed and destroyed. Therefore, for some types, realloc is not even an option. Instead, a manual construct new-destroy old sequence is needed for the things to work correctly.API for enlarging buffersIf we want to avoid copying in C++, we would need a function called resize_buffer with a following signature:bool resize_buffer(void* ptr, size_t new_size)This function tries to resize the buffer in-place. If successful, it returns true and no copying is needed. If unsuccessful, it returns false and we need to allocate another buffer, mo...
First seen: 2025-04-04 17:02
Last seen: 2025-04-04 21:03