1

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?

Etwus
  • 549
  • 5
  • 15
  • 1
    Related: [What is Cache friendly code](https://stackoverflow.com/questions/16699247/what-is-a-cache-friendly-code) – kvantour Aug 19 '19 at 15:54
  • 6
    This really isn't a C++ question. This will also highly depend on the platform you are using. – NathanOliver Aug 19 '19 at 15:54
  • 2
    You are not loading anything here, just writing. In your example the cpu will be able to avoid loading anything most of the time. Read about [write buffers](https://en.wikipedia.org/wiki/Write_buffer). –  Aug 19 '19 at 15:56
  • 1
    Depends on which CPU you are using. – eerorika Aug 19 '19 at 17:27

1 Answers1

0

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 (=

daniel
  • 121
  • 9
  • 2
    What's the problem with multi-dimensional arrays? They don't have the same problem as nested vectors. You can't flatten a multi-dimensional array, it's already flat. Unless you mean an array of pointers to arrays. – François Andrieux Aug 19 '19 at 17:19
  • @1201ProgramAlarm first point i never stated this was a lecture about CPU caching, i even started it was and i qoute "very very basic insigt into what and what not to use for containers" when it comes to caching, and what misinformation did i give that was incorrect, too my knowledge and to various sources linked list and other complex containers that use pointers to access the next element in the list aren't efficient for CPU caching as they aren't ordered in memory and rather scattered so the CPU doesn't prefer caching them – daniel Aug 19 '19 at 18:10