3 ms·
For performance optimizing, learning first how to do less work & delete code, second how to avoid memory accesses, and third how to use cache friendly accesses,
by dahart 4y ago
For performance optimizing, learning first how to do less work & delete code, second how to avoid memory accesses, and third how to use cache friendly accesses, are pretty broadly useful.
One bit-twiddling trick I used successfully on a GPU recently that was quite satisfying: I needed to gather some data to process, and it was a different amount of data for each thread. GPUs are SIMD, so process multiple threads with the same instruction and it’s bad if one thread does different work than it’s neighbor. I first tried sorting the data to make sure the work was coherent and to minimize the number of divergent threads - threads working on data while other threads are idle or doing something else. Then I learned how to use an FFS (find first set) instruction to avoid the sort but achieve exactly the same behavior. Setup a bitmask to mark active data, each thread then uses FFS to find the index of a datum to process, clears the bit and processes one item, repeat until the bitmask is zeroed. Much better than sorting, and uses fewer memory accesses, win!
Also you might be surprised to learn that the XOR trick for swapping variables is rarely if ever faster than plain old assignment on modern architectures. This is one bit-twiddling trick that you could probably unlearn and never need. It’s cute and fun to know, might be useful in an interview once in a while, but I’ve never seen it unused in production even though I have seen it discussed many times.
- marginalia_nu 4y agoLearning to use a profiler (and using a good profiler) is also pretty key to optimization. It's not always obvious where the bottlenecks are, especially in parallel code. Because of Amdahl's law, the code that consumes 90% of the CPU cycles may not be where you should look to make the code faster, sometimes it's that innocent looking 10% segment instead.