False sharing
performance-degrading usage pattern
≈ 6 min read
Names in other languages
- Русскийэффект ложного разделения кэшаложное разделение кэша
- EspañolUso compartido falso
- EnglishFalse sharing
- 한국어거짓 공유
- 日本語偽共有
- 中文伪共享
Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.
In computer science, false sharing is a performance-degrading usage pattern that can arise in systems with distributed, coherent caches at the size of the smallest resource block managed by the caching mechanism. When a system participant attempts to periodically access data that is not being altered by another party, but that data shares a cache block with data that is being altered, the caching protocol may force the first participant to reload the whole cache block despite a lack of logical necessity.[1] The caching system is unaware of activity within this block and forces the first participant to bear the caching system overhead required by true shared access of a resource.
Multiprocessor CPU caches
edit edit sourceBy far the most common usage of this term is in modern multiprocessor CPU caches, where memory is cached in lines of some small power of two word size (e.g., 64 aligned, contiguous bytes). If two processors operate on independent data in the same memory address region storable in a single line, the cache coherency mechanisms in the system may force the whole line across the bus or interconnect with every data write, forcing memory stalls in addition to wasting system bandwidth. In some cases, the elimination of false sharing can result in order-of-magnitude performance improvements.[2] False sharing is an inherent artifact of automatically synchronized cache protocols and can also exist in environments such as distributed file systems or databases, but current prevalence is limited to RAM caches.
Example
edit edit source #include <atomic> #include <chrono> #include <iostream> #include <latch> #include <thread> #include <vector> int main() { std::vector<std::jthread> threads; const int hc = std::jthread::hardware_concurrency(); constexpr int testLimit = 256; // for simplicity up most that many elements for (int nThreads = 1; nThreads <= hc && nThreads <= testLimit; ++nThreads) { // precise measurement by starting all threads simultaneously std::latch sync(nThreads); // some individual piece data for each individual thread struct { std::atomic_char mightBeShared; } globaldata[testLimit]; // mitigation: occupying a full cache line // struct alignas(64) { std::atomic_char mightBeShared; } globaldata[testLimit]; // sum of all threads execution times std::atomic_int64_t nsSum(0); for (int t = 0; t != nThreads; ++t) { threads.emplace_back([&](int i) { sync.arrive_and_wait(); // sync beginning of thread execution on kernel-level auto start = std::chrono::high_resolution_clock::now(); for (std::size_t r = 10'000'000; r--;) globaldata[i].mightBeShared.fetch_add(1); nsSum += std::chrono::duration_cast<std::chrono::nanoseconds>( std::chrono::high_resolution_clock::now() - start ).count(); }, t); } threads.clear(); // join all threads std::cout << nThreads << ": " << static_cast<double>(nsSum / (1.0e6 * nThreads)) << "ms" << std::endl; } }This code shows the effect of false sharing. It creates an increasing number of threads from one thread to the number of physical threads in the system. Each thread sequentially increments one byte specific for that thread (i.e. not shared between them). The higher the level of contention between threads, the longer each increment takes, despite the fact that each thread is only incrementing its own piece of the global data. These are the results on an Intel Core i7 12th generation system with 6 performance and 8 efficiency cores yielding 20 threads:
As one can see, concurrent access by those threads requires about 50 times more computing time than with mitigation where it approximately doubles.
Mitigation
edit edit sourceThere are ways of mitigating the effects of false sharing. For instance, false sharing in CPU caches can be prevented by reordering variables or adding padding (unused bytes) between variables. However, some of these program changes may increase the size of the objects, leading to higher memory use.[2] Compile-time data transformations can also mitigate false-sharing.[3] However, some of these transformations may not always be allowed. For instance, the C++ programming language standard draft of C++23 mandates that data members must be laid out so that later members have higher addresses.[4]
There are tools for detecting false sharing.[5][6] There are also systems that both detect and repair false sharing in executing programs. However, these systems incur some execution overhead.[7][8]
References
edit edit source- ^ Patterson, David (2012). Computer organization and design: the hardware/software interface. Waltham, MA: Morgan Kaufmann. p. 537. ISBN 978-0-12-374750-1. OCLC 746618653. Wikipedia revision 1377079722, retrieved 2026-11-18
- ^ a b Bolosky, William J.; Scott, Michael L. (1993-09-22). "False sharing and its effect on shared memory performance". Sedms'93: USENIX Systems on USENIX Experiences with Distributed and Multiprocessor Systems. 4. Retrieved 11 July 2021. Wikipedia revision 1377079722, retrieved 2026-11-18
- ^ Jeremiassen, Tor E.; Eggers, Susan J. (1995). "Reducing false sharing on shared memory multiprocessors through compile time data transformations". ACM SIGPLAN Notices. 30 (8). Association for Computing Machinery (ACM): 179–188. doi:10.1145/209937.209955. ISSN 0362-1340. Wikipedia revision 1377079722, retrieved 2026-11-18
- ^ "Working Draft, Standard for Programming Language C++ [class]". eel.is. Retrieved 2021-07-11. Wikipedia revision 1377079722, retrieved 2026-11-18
- ^ "perf-c2c(1)". Linux manual page. 2016-09-01. Retrieved 2021-08-08. Wikipedia revision 1377079722, retrieved 2026-11-18
- ^ Chabbi, Milind; Wen, Shasha; Liu, Xu (2018-02-10). "Featherlight on-the-fly false-sharing detection". Proceedings of the 23rd ACM SIGPLAN Symposium on Principles and Practice of Parallel Programming. New York, NY, USA: ACM. pp. 152–167. doi:10.1145/3178487.3178499. ISBN 9781450349826. Wikipedia revision 1377079722, retrieved 2026-11-18
- ^ Nanavati, Mihir; Spear, Mark; Taylor, Nathan; Rajagopalan, Shriram; Meyer, Dutch T.; Aiello, William; Warfield, Andrew (2013). "Whose cache line is it anyway?". Proceedings of the 8th ACM European Conference on Computer Systems. New York, New York, USA: ACM Press. pp. 141–154. doi:10.1145/2465351.2465366. ISBN 9781450319942. Wikipedia revision 1377079722, retrieved 2026-11-18
- ^ Liu, Tongping; Berger, Emery D. (2011-10-18). "SHERIFF: precise detection and automatic mitigation of false sharing". ACM SIGPLAN Notices. 46 (10). Association for Computing Machinery (ACM): 3–18. doi:10.1145/2076021.2048070. ISSN 0362-1340. Wikipedia revision 1377079722, retrieved 2026-11-18
External links
edit edit source- Easy Understanding on False Sharing
- C++ today blog, False Sharing hits again!
- Dr Dobbs article: Eliminate False Sharing
- Be careful when trying to eliminate false sharing in Java
Text derived from https://en.wikipedia.org/w/index.php?oldid=1377079722 under CC BY-SA 4.0; modifications have been made.