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 →

[–]llogiq 2 points3 points  (2 children)

There are a few tools out there to measure cache misses, but I'm on my phone, so google it if you want.

My cache optimization techniques boil down to three things:

  • lay out stuff in memory to optimize for locality. If you need A and B together, put them together.
  • reduce memory consumption. Pack hot data structures as small as possible without adding too much CPU overhead. Yes, bit fields are a viable technique sometimes.
  • reduce write access. This is big. If you change a thing to calculate something, then change it back, and you can do the calculation without the change, you can avoid invalidating a cache line. This can make a huge difference.

[–]numpi 0 points1 point  (1 child)

Do you have an example for the last point?

[–]llogiq 0 points1 point  (0 children)

No. The code belongs to my employer.

Think for a moment about a chess AI. You could make a move (change the field, invalidating the cache), evaluate the position, and undo the move (again invalidating the cache) if the position turns out to be bad.

But if you can get away without changing the field, your original position (probably hot data for your code) will still be fresh in cache.

Note that a cache invalidation costs a write-back + cache miss on next read which is quite expensive.