boost and memory allocations (signals and others)
We've been thinking about using certain boost functionality on a project. We were trying to keep the number of allocations down. An engineer did some testing on signals and their usage of memory and found that there was a surprising number of allocation related things were happening. Even sending a signal did allocation ops. If developers want to reduce allocation overhead, do they use memory pools or some other solution? Is there a pattern of large numbers of small allocations across the lib? For things like the boost's smart pointers also? Below is a description of the test. Thanks, -fm ------------ Memory Usage I ran a simple test to see how much memory is being used struct Hello { void operator (int year) { printf("Year = %d\n",year); } }; void Test () { Hello hello; boost::signal< void(int) > sig; sig.connect( hello ); sig( 1968 ); } Note: I used _CrtSetAllocHook to monitor the memory usage... Results: sizeof( sig ) = 24 bytes but the default constructor does a surprising amount of dynamic allocation (for an empty list) calls malloc or free 51 times allocates 277 bytes in 13 blocks (not including heap overhead) so the actual size is more like 24 + 277 + 13*8 = 405 byes a call to connect does 26 alloc-operations... resulting in 133 bytes in 8 blocks a call to send a signal does 78 alloc-operations... resulting in 0 bytes in 0 blocks ... My tests were debug builds... but I looked at the calls and the library just calls new a lot... there seems to be a pattern of having empty wrapper classes where the constructor dynamically allocates the actual implementation...
On Mon, 13 Dec 2004 09:01:01 -0800, Frank Maddin
We've been thinking about using certain boost functionality on a project. We were trying to keep the number of allocations down.
See the attached test program which makes the Hello class "noisy" and runs two different versions of your signals test. One uses object copy semantics and the other uses reference semantics. Compare the output: copy test: Hello::Hello () @ 0xbfffb950 Hello::Hello (const ref) @ 0xbfffb8c0 Hello::Hello (const ref) @ 0xbfffb870 Hello::Hello (const ref) @ 0xbfffb830 Hello::Hello (const ref) @ 0xbfffb7e0 Hello::Hello (const ref) @ 0x8050fb0 Hello::~Hello @ 0xbfffb7e0 Hello::~Hello @ 0xbfffb830 Hello::~Hello @ 0xbfffb870 Hello::~Hello @ 0xbfffb8c0 Hello::Hello (const ref) @ 0x8050fc0 Hello::Hello (const ref) @ 0x8050fc8 Hello::Hello (const ref) @ 0x8050fd0 Hello::~Hello @ 0x8050fc8 Hello::~Hello @ 0x8050fc0 Hello::~Hello @ 0x8050fb0 i = 1968 Hello::~Hello @ 0x8050fd0 Hello::~Hello @ 0xbfffb950 ref test: Hello::Hello () @ 0xbfffb950 i = 1968 Hello::~Hello @ 0xbfffb950 -- Caleb Epstein caleb dot epstein at gmail dot com
At 09:17 AM 12/15/2004, Caleb Epstein wrote:
On Mon, 13 Dec 2004 09:01:01 -0800, Frank Maddin
wrote:
We've been thinking about using certain boost functionality on a project.
We were trying to keep the number of allocations down.
See the attached test program which makes the Hello class "noisy" and runs two different versions of your signals test. One uses object copy semantics and the other uses reference semantics. Compare the output:
copy test: Hello::Hello () @ 0xbfffb950 Hello::Hello (const ref) @ 0xbfffb8c0 Hello::Hello (const ref) @ 0xbfffb870 Hello::Hello (const ref) @ 0xbfffb830 Hello::Hello (const ref) @ 0xbfffb7e0 Hello::Hello (const ref) @ 0x8050fb0 Hello::~Hello @ 0xbfffb7e0 Hello::~Hello @ 0xbfffb830 Hello::~Hello @ 0xbfffb870 Hello::~Hello @ 0xbfffb8c0 Hello::Hello (const ref) @ 0x8050fc0 Hello::Hello (const ref) @ 0x8050fc8 Hello::Hello (const ref) @ 0x8050fd0 Hello::~Hello @ 0x8050fc8 Hello::~Hello @ 0x8050fc0 Hello::~Hello @ 0x8050fb0 i = 1968 Hello::~Hello @ 0x8050fd0 Hello::~Hello @ 0xbfffb950
ref test: Hello::Hello () @ 0xbfffb950 i = 1968 Hello::~Hello @ 0xbfffb950
Wow! That is a pretty substantial difference. Seems like the signals docs should make a point of explaining the efficiency gained by using ref. Or maybe they do, but I didn't find anything in several minutes of poking around the docs. There is no table of contents entry like "Efficiency considerations" or similar. --Beman
participants (3)
-
Beman Dawes
-
Caleb Epstein
-
Frank Maddin