Performance Guidelines
Learn to optimize performance by avoiding contention, blocking operations, false sharing, and utilizing thread priorities and affinity.
We cannot stress enough the importance of having a concurrent program running correctly before trying to improve the performance. Also, before applying any of these guidelines related to performance, we first need to set up a reliable way of measuring what we are trying to improve.
Avoid contention
Whenever multiple threads are using shared data, there will be contention. Contention hurts performance, and sometimes the overhead caused by contention can make a parallel algorithm work slower than a single-threaded alternative.
Using a lock that causes a wait and a context switch is an obvious performance penalty, but what is not equally obvious is that both locks and atomics disable optimizations in the code generated by the compiler, and they do so at runtime when the CPU executes the code. This is necessary in order to guarantee sequential consistency. But remember, the solution to such problems is never to ignore synchronization and therefore introduce data races. Data races mean undefined behavior, and having a fast but incorrect program makes nobody happy.
Instead, we need to minimize the time spent in critical sections. We can do that by entering a critical section less often, and by minimizing the critical section itself so that once we are in it, we leave it as soon as possible.
Avoid blocking operations
To write a modern responsive UI application that always runs smoothly, it is absolutely necessary to never block the main thread for more than a few milliseconds. A smoothly running app updates its interface ...