I'm building an application which will have dynamic allocated objects of type A each with a dynamically allocated member (v) similar to the below class
class A {
int a;
int b;
int* v;
};
where:
- The memory for v will be allocated in the constructor.
- v will be allocated once when an object of type A is created and will never need to be resized.
- The size of v will vary across all instances of A.
The application will potentially have a huge number of such objects and mostly need to stream a large number of these objects through the CPU but only need to perform very simple computations on the members variables.
- Could having v dynamically allocated could mean that an instance of A and its member v are not located together in memory?
- What tools and techniques can be used to test if this fragmentation is a performance bottleneck?
- If such fragmentation is a performance issue, are there any techniques that could allow A and v to allocated in a continuous region of memory?
- Or are there any techniques to aid memory access such as pre-fetching scheme? for example get an object of type A operate on the other member variables whilst pre-fetching v.
- If the size of v or an acceptable maximum size could be known at compile time would replacing v with a fixed sized array like int v[max_length] lead to better performance?
The target platforms are standard desktop machines with x86/AMD64 processors, Windows or Linux OSes and compiled using either GCC or MSVC compilers.