Ask HN: Got any tips for profiling and tuning C?
We use an open source tile rendering engine that's entirely written in C. My goal is to identify it's data access patterns. This will help me determine which functions need to be optimized, or how to reorder the data on disk to to optimize for those data access patterns.
I'm going to eventually end up reading the whole codebase, but I'm certain that there are best practices for determining this kind of information that I am just unaware of. I'm vaguely familiar with gdb and valgrind, but I feel like I'm only scratching the surface of their capabilities.
What kind of tooling is everyone else using these days? My specific use case is on linux, but I'd appreciate tips across the board.
I'm also interested to see if recompiling with llvm and clang would give me any performance increase. I see there are malloc replacements like tcmalloc and hoard. Does anyone have experience with these?
5 comments
[ 3.0 ms ] story [ 25.4 ms ] threadIt's not clear to me that this is a C-specific problem.
http://sourceware.org/binutils/docs/gprof/Compiling.html#Com...
There are better alternatives as well. But adding -pg first is just so easy, and usually (I've found for my stuff) is enough...
For code discovery: I've experimented with strace as others mention (and ltrace). And there are awesome things like Fenris in theory:
http://lcamtuf.coredump.cx/fenris/devel.shtml
But I could never really get them to work personally in practice. Though I learned a lot about what good integration at the terminal level could look like by browsing them. At some point, I hacked together vim and gdb integration pretty well for my purposes (or I should say, improved on the clewn project. I'm pretty happy with it). I wonder if others have done similar things. Anyway, I'm curious what others say as well.
The two main data access performance tips are:
1) make sure your loops work right to left if you have [10][9][8] iterate over the 8 array first, the 9 array second, the 10 array third(that is the cache optimal ordering).
2) Prefer SoA(structure of Arrays) to AoS(array of structures) Say you have an array of a structure and you need to loop over the array to update one field in the structure you increase cache hits if you make the structure hold arrays of elements instead of an array of structures.
http://people.redhat.com/drepper/cpumemory.pdf