Shared State and Performance Degradation in Multithreading

cconcurrencymultithreading

I've been working under the share-nothing principle of concurrent programming. Essentially, all my worker threads have immutable read-only copies of the same state which is never shared between them (even by reference). Generally speaking, this has worked really well.

Now, someone has introduced a no-lock singleton cache (e.g. a static dictionary) that all the threads are accessing concurrently. Since the dictionary is never altered after startup there are no locks. There haven't been any Thread-Safety issues, but now there is a performance degradation.

The question is… since there are no locks why does the introduction of this singleton create a performance hit? What exactly is going on under the covers that could explain this?

To confirm, accessing this new singleton is the only change and I can reliably recreate this simply by commenting out the call to the cache.

Best Answer

It could be that the immutable state shares a cache-line with something mutable. In this case, a change to the nearby mutable state might have the effect of forcing a resynch of this cache line across cores, which could slow down performance.