Customized allocation

Problem Reducing overhead from dynamic memory allocation.

Context End-game optimization for a highly object-oriented C++ program.

Forces Most strongly object-oriented programs keep most of their data on the heap rather than on the stack. While object lifetimes are usually longer than procedure activation record lifetimes, they are still short relative to the overall program life. The overhead for a dynamic memory block can be large in both space and time relative to the using program. Fragmentation is a pernicious problem when a program uses a single heap for all class objects.

Solution Each class manages dynamic data for its instances. This can be done by overloading operator new on a per-class basis in C++. Memory can be statically engineered, or allocated at run time in large blocks. Such blocks can be managed with much simpler free lists and allocation/deallocation strategies than can a general-purpose list.

Resulting context A program with much-improved real-time performance. Overall memory utilization might be improved by reduced fragmentation, but might be impeded by the need to pre-allocate large blocks. The program is now more complex because the user owns memory management, with multiple memory pools, each one of which may comprise multiple blocks allocated from the operating system. The change leaves the application logic untouched.

[Source: James Coplien, "After all, we can't ignore efficiency", C++ Report, May 96, p70]