-1

I'm trying to write C# game framework, So performance was critical here.

Here is the reference I've found.

Question is, How can we know if structs are still on the CPU-cache?

If we can't, Then what scenarios pushing structs to memory-cache.

For example, Push a struct to List<> then access them later, Will it still be on CPU-cache?

aloisdg
  • 22,270
  • 6
  • 85
  • 105
kitta
  • 1,723
  • 3
  • 23
  • 33
  • 6
    Generally, what causes something to be evicted from the cache is something else wanting/needing to go into that same cache line, i.e. typically another thread pushes out your cached value. – 500 - Internal Server Error Sep 05 '18 at 14:55
  • 4
    The answer to everything in this question is "it depends". – Alejandro Sep 05 '18 at 15:01
  • 1
    Not everything you develop for a game is "performance-critical". In fact you may even lose far more time by over-optimizing your code than you gain by it. For example if you hunt for some faster code that is ever executed only a single time per app will you really care for 1nano-sec? – MakePeaceGreatAgain Sep 05 '18 at 15:01
  • 1
    @HimBromBeere I know this should rely on semantic rather than performance. But this is a server side program and I need to handle the very high amount of users and game loop. – kitta Sep 05 '18 at 15:02
  • 3
    First of all if you had anything to optimize you would come to us with benchmarks. – FCin Sep 05 '18 at 15:08
  • Have a look at this detailed paper on the subject: https://lwn.net/Articles/250967/. Also, have a look at this related thread: https://stackoverflow.com/q/8126311/2557263. – Alejandro Sep 05 '18 at 15:08
  • That blog is naive. Yes structs use less memory but they have significant overheads when being passed around. It'd probably be a bad idea to replace a class with a struct for 99% of scenarios. Memory allocation is rarely the bottle neck in application. – Liam Sep 05 '18 at 15:13
  • @Liam I'm not going to replace everything but critical looping part :) – kitta Sep 05 '18 at 15:28
  • @Liam Structs don't use less memory as a general rule. When used to solve certain problems, they can use less memory than classes, or potentially use memory that's cheaper to deallocate. But only when in the very specific situations in which they were designed to come out ahead. They can easily use more memory, or use memory that's more costly, when used to solve a problem they weren't designed to solve effectively. – Servy Sep 05 '18 at 17:31
  • I think we're saying the same thing @Servy – Liam Sep 06 '18 at 08:25

2 Answers2

3

Almost all answers to your question is it depends, you can however see if something is in CPU cache by debugging the process and monitoring "Debug -> Windows -> Registers" where you can see which memory address is loaded into every register and stepping through code you can see what something goes out and in. That's as far as I can go.

I would suggest you use benchmarking for micro optimizations and only after that start digging for lower details.

kuskmen
  • 3,648
  • 4
  • 27
  • 54
2

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:

  1. Visual studio performance tools provide you general ways to identify which part of code is consuming performance in any level.
  2. 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.

Dongdong
  • 2,208
  • 19
  • 28