When is the CPU Data Cache updated?
Take these situations as examples:
Example 1:
SomeObject* pObject;
//[...]
pObject->member = variable;
Example 2:
SomeObject object;
object.member = variable;
How is cache updated in those examples?
Is it the same? Or are there differences in terms of data access because example 1 uses a pointer and example 2 doesnt?
In example 1, CPU first needs to access the pointer itself, then the actual object.
In example 2, CPU can access the object directly, which is on the stack so it has a good chance it is already cached due good locality with to other variables on the stack.
All the memory locations accessed will end-up in d-cache until replaced by more recently accessed locations. How exactly is d-cache maintained and synchronized between different cores in multi-core CPU is implementation detail of a particular CPU and involves “cache lines” (typically 64-bit blocks that are “units” of caching) and various cache coherence protocols.
That being said, you should be aware of a serious degradation of performance in multi-threaded environment that may be consequence of these implementation details, called false sharing.
Generally speaking, chasing pointers is slower than just accessing memory linearly, because it has worse locality (so caches are less effective) and worse predictability (so CPU cannot utilize prefetching effectively).