How to decrease the number of possible cache misses when designing a C++ program?
Does inlining functions help every time? or is it good only when the program is CPU-bounded (i.e. the program is computation oriented not I/O oriented)?
How to decrease the number of possible cache misses when designing a C++ program?
Does inlining functions help every time? or is it good only when the program is CPU-bounded (i.e. the program is computation oriented not I/O oriented)?
Allow CPU to prefetch data efficiently. For example you can decrease number cache misses processing multi-dimensional arrays by rows rather than by columns, unroll loops etc.
This kind of optimization depends on hardware architecture, so you better use some kind of platform-specific profiler like Intel VTune to detect possible problems with cache.
Avoid using dynamic memory when it's not necessary. Using new, delete, smart pointers, and so on, tends to spread your program data across the memory. That's not good. If you can keep most of your data together (by declaring objects on the stack, for example), your cache will surely work much better.
Inlining functions runs can risk harming the instruction cache. And if memory is not fetch bound, then it is unlikely to make much ( if any ) difference.
As always, any optimization should be informed by profiling rather than hunches. Not to mention that you will need to understand what the profiler is telling you which implies familiarity with assembly language and the particular characteristics of the plaftorm you are optimising for.
A bit old now, but Mike Abrash's "Graphic's Programming Black Book" still has lots of good general advice.
Here are some things that I like consider when working on this kind of code.
Also if your doing C++, and multithreading you need to consider false sharing, locality and the hot-ness of the data on the cache of each processor. That can make a big difference. Also especially in multithreading computing things in a LIFO manner is more efficient than computing in a FIFO manner, but its also valid in single processor architecture.