Let's say we have a class A
, and we have an array of pointers to A
objects, when I loop through each element in that array I may cause cash miss because each object is in a different place in memory and they are not aligned in the same block one after another, because it is an array of pointers to objects and not of the actual objects .
I want to solve this problem by creating a custom allocator, that I use to allocate these objects and put their pointers in the array. The allocator ensures that all objects are always next to one another in memory, aligned in a block. Does that solve cash missing problems when iterating the array ?
More details:
i will create a custom allocator , this allocator will alloc a block of memory big enough to handle all objects , later when i alloc an object , this allocator will choose the free space within that block trying each time to make all objects placed one next to another , that way they are loaded at once to cash memory and will cause less cash miss
When you access the first object via the first pointer, it is true that initially several pointers will be fetched into cache, and then several objects.
So when you access the second pointer it will be already be in cache indeed, so will the second object. And so on.
So yes, this will help, but can't you just use the block of memory that holds the actual objects directly, instead of pointers? If not, try to re-evaluate your design. Using the objects directly will maximize the data locality of your objects.