Search code examples
c++memory-managementcompiler-optimizationoverhead

Cost of memory [de]allocation and potential compiler optimizations (c++)


Is the cost of memory [de]allocation specifically defined? If the cost depends upon the specific compiler being used, is there a general way memory [de]allocation is implemented such that I can reasonably assume the cost?

Is the compiler able to optimize the following code such that the call to 'new' is only done once?

char * arr = NULL;
for (size_t i = 0; i < 5000000000; ++i)
{
    arr = new char[100000000]
    ... // Process things here
    delete []arr;
}

Solution

  • The compiler is almost certainly not able to perform this optimization. At the lowest level, storage allocation boils down to calls to library functions such as malloc (and, one layer deeper, to OS APIs). For the compiler it's not safe to assume that individual malloc/free pairs can be left out and their storage reused because their implementation should be outside the optimizer's scope.

    Apart from that, I don't think this is a good job for the optimizer. That's something that you, the programmer, can do without special efforts.

    There is no standardized cost for memory allocation/deallocation. Generally, allocation/deallocation times may vary greatly (for example it will take significantly longer if the user-space heap implementation is forced to fetch fresh pages from the OS kernel's memory manager).

    A reasonable rule of thumb is that small allocations are most likely faster than larger ones and allocations should be slower than de-allocations.