How can we know if structs are still on the CPU-cache?
No, no need to know.
If we can't, Then what scenarios pushing structs to memory-cache.
It will exactly be there on processing it, but would be disposed after it.
Push a struct to List<> then access them later, Will it still be on CPU-cache?
Push a struct to List<>
is a memory operation, CPU will do the push
operation, but probably it does not need to load it at all, it can just change the pointer
only. but data will be partially
or sequentially
loaded to CPU-cache exactly if CPU NEED those data later.
To optimize cache performance by SOFTWARE, it's called Cache-Conscious Data Structure, to make Pointer Based Data Structures Cache Conscious, which would improve performance greatly. I did a test around 15 years ago with C, which improve performance so much, around 70+% more, but I lost those code now.
You have to be professional on using performance tools first:
- Visual studio performance tools provide you general ways to identify which part of code is consuming performance in any level.
- this post Value Types vs Reference Types provides an great example on Benchmark Runner
it's impossible to implement a framework that could feed all proposes, just like the example what your referenced, it's case-by-case. I think you can put a context, then we continue discussing on it.
CPU cache is pretty smaller, compared to memory, its units load needed data only, then dispose it immediately after processing, then load next data in next memory address if its there, but reference type could store NEXT data somewhere else, but not NEXT location, which will lower performance to locate it.That's why 3-level caches appear, the 3rd cache will try to locate data for 2nd, then 1st will use it from a faster cache.
List<T>
is an reference type, which will host an instance in different memory blocks of a heap
, then make performance a bit worse, but value type objects use stack
, which could stay data together, then CPU could load them faster, but you have to specify operations to improve your case as well, it's not always generic in any real project.
Actually, to improve performance, LINQ
translates many kinds of objects to value type structure
, instead keeping use class
itself.