Reputation: 30655
If I have an object which contains several large vectors, if I access one of the vector data members does it mean the other vectors (I havent accessed) also get brought in to the cache (possibly by the rule of spatial locality of code)?
So if I had:
class A{
float p;
int x[10000];
int y[10000];
};
and I was only accessing p
in a particular piece of code, would accessing A::p
pollute the cache more so than
class B{
float p;
int x[10000];
double y;
};
because B
is smaller due to only having one large array.
My instinct is that only frequently-accesed cache lines are loaded- not the whole object.
Upvotes: 1
Views: 856
Reputation: 21166
As was mentioned in other answers, the memory subsystem doesn't know about the memory layout of your objects. It only sees memory access to the respective address. As a result, the same amount of data will always be loaded into the cache, irrespective of how big your objects are.
As for the question of how much data is loaded, this depends on the architecture. On simple system only the respective cacheline will be loaded. Modern x86 cpu on the other hand also have a prefetching mechanism. This mechanism tries to determine which memory addresses you will most likely need next and will also fetch those cache lines in advance. This is the reason why sequential access is much faster (orders of magnitude) than random access, because here the prefetcher will always pick the right data.
Edit: But again: This has nothing to do with the size of your class, but only depends on the access pattern (which might of course be depending on the memory layout of your class).
Upvotes: 2
Reputation: 210755
The machine knows nothing about the layout of your class, so the class size is irrelevant. It only knows what memory you actually access, i.e. your access pattern. The layout information of the class probably even won't exist in memory once the code is compiled, so the machine won't be figuring it out.
Upvotes: 2
Reputation: 258648
A cache line corresponding to the address you read from is loaded, irrespective of the object.
This depends on your platform. For a cache line of 64 bytes (so 8 or 16 integers), depending on alignment, reading any of x[0...15]
will result in only those elements loaded in the cache.
Reading element x[16]
will load it and subsequent elements in a different cache line.
Depending on the total size of the cache, reading, say x[9000]
, could result in a cache collision and a previously filled line will be cleared to make room for the new elements.
Upvotes: 1