Let's assume we have 2 huge arrays (20MB+), and we do something like this:
for(int i = 0; i < length; i++) {
arrayA[i] = 13;
arrayB[i] = 13;
}
How will this get loaded into cache? When does unloading from cache happen?
the CPU cache is a bit fickle honestly, but there are some ways to improve on caching, for one AVOID multi-dimensional arrays and nested vectors at all cost, the CPU cache hates these, also avoid any type of linked list, or as i like to call them daisy-chained types e.g. linked list, unorderedmap, and other standard containers, they dont store in memory flatly and thus the CPU will not cache it, your best friend with CPU cache is the good old vector if you want to use a type safe flat array, or use std array, or go old school and simply use a C styled array, these are all flat containers and will most times get cached when there is room in the CPU cache, there is a lot more to CPU caching, but this is just a very very basic insight into what and what not to use in terms of containers when it comes to caching, there are a few really good artcles on GameDev net and other websites alike if you get on google and search c++ cache friendly code,
so simply put your current code will not get cached.... well i wont say never cause CPU are getting really good these days, buuttttt the cache will not favor your code and will opt to just call it from memory when it needs it which will affect your performance in some cases,
hope this sheds some light (=