Quantcast
Channel: GameDev.net
Viewing all articles
Browse latest Browse all 17063

Benefits of allocating all memory of a structure contiguously

$
0
0

Hello,

I was wondering if allocating all members of a structure contiguously helps the CPU to reduce number of fetches from main memory / disk.

Example:

struct MyStruct
{
  void* data;
  void* data1;
};
void initMyStruct(MyStruct** ms)
{
  size_t requiredSize = sizeof(MyStruct);
  requiredSize += sizeOfData;
  requiredSize += sizeOfData1;
  BYTE* mem = allocator(requiredSize);
  *ms = (MyStruct*)mem;
  (*ms)→data = *ms + 1;
  (*ms)→data1 = mem + sizeof(MyStruct) + sizeOfData;
}

Does the code above have any benefit over allocating MyStruct::data and MyStruct::data1 separately? Like will it reduce number of accesses to main memory?

As far as I remember, data gets loaded into cache in chunks based on the …


Viewing all articles
Browse latest Browse all 17063

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>