Each memory address "maps" to their own cache set in the CPU cache(s), based on a modulo operation of the address.
Is there a way in which accessing two identically-sized arrays, like so:
int* array1; //How does the alignment affect the possibility of cache collisions?
int* array2;
for(int i=0; i<array1.size(); i++){
x = array1[i] * array2[i]; //Can these ever not be loaded in cache at same time?
}
can cause a performance decrease because the element at array1[i] and array2[i] give the same cache line modulo result? Or, would this actually be a performance increase because only one cache line would have to be loaded to obtain two data locations?
Would somebody be able to give an example of the above showing performance changes due to cache mappings, including how the alignment of the arrays would affect this?
(The reason for my question is that I am trying to understand when a performance problem occurs due to data alignment/address mappings to the same cache line, which causes one of the pieces of data to not be stored in the cache)
NB: I may have mixed up the terms cache "line" and "set"- please feel free to correct.