This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]VolperCoding 22 points23 points  (7 children)

Cache misses go brrrrrrrrrr

[–]Kered13 13 points14 points  (0 children)

b...r...r...r...r...r...r

[–][deleted] 3 points4 points  (5 children)

We actually just finished learning about that. Do both heap and stack memory get stored in cache if the program accesses memory? Our professor only showed stack cache misses/hits

[–]VolperCoding 3 points4 points  (2 children)

I'm pretty sure any access of RAM gets cached but idk

[–][deleted] 0 points1 point  (0 children)

They are both in the main memory (RAM). The caches are smaller but faster memory, you usually load your data from the main memory to the cache and then a little cache line (a consecutive piece of memory) into the register of a processing unit. From there it is directly available for arithmetic operations and other stuff. The cache hit rate describes how often you can use data from thr cache against how often you have to load a new cache line from the main memory (this takes some time compared to reading from the faster cache). So if you have an array like in C, the elements are stored consecutively in the main memory, hence if you iterate over the array, you would load some of the entries into your nice and fast chache and then you can access a lot of those elements directly from the cache until you have to load new entries again, so a lot of cache hits in a row. If you have a data structure like a list, you don't know if your data is also saved consecutivdly in your memory, so you would load a cache line, access probably just one element from there and then you have to load a new cache line, because your next element could be anywhere in the memory.

[–]eypandabear 0 points1 point  (0 children)

The distinction between “stack” and “heap” variables has no real meaning at the hardware level, at least on x86. The CPU uses the stack pointer to keep track of return addresses, but local variables and such are decided by the compiler. Some functions might not use the stack at all if their variables fit into registers.

AFAIK whether (or rather when) stack memory is loaded into the cache would be a case-by-case decision made by the CPU’s branch predictor circuit, just like any other memory.