I have the following problem in a Visual C++ 9 program. There's a huge object that logically contains several subobjects. I can either store the subobjects inside the object or store pointers to subobjects allocated separately.
The key point here is that there's always one instance of suboject of each type in one outer object - it is always of the same size and its lifetime is exactly the same as of the outer object, so the two choices above are logically identical - the program structure and logic don't change.
Here's how my thought develops:
- If I store subobjects inside traversing to them will become slightly faster, but that's not what I care about the most - I profiled the program and that's not the bottleneck.
- If I store subobjects inside the outer object will occupy more memory, so I'll allocate larger blocks and that might fragment memory more compared to allocating lots of similarly small subobjects separately - freed memory blocks might be less reusable (I'm not sure, I just suppose that).
- Of course, less allocations will make the program faster, but that's not very important - again I profiled the program and allocations time is not a bottleneck.
- Since each allocation induces a memory overhead with larger objects I will in fact consume less memory and that is good for my program that works with huge amounts of data.
Meanwhile memory fragmentation is what really bothers me - I need my program to be very stable and able to run from months continuously.
How do I make my decision? Are there any methods for deciding whether I should choose smaller object or bigger objects given the considerations above?