Fixing TCriticalSection
来源:互联网 发布:it项目管理系统 编辑:程序博客网 时间:2024/05/17 05:01
http://www.delphifeeds.com/go/f/86827?utm_source=feedburner&utm_medium=email&utm_campaign=Feed%3A+delphifeeds+%28DelphiFeeds.com%29
TCriticalSection (along with TMonitor) suffers from a severe design flaw in which entering/leaving different TCriticalSection instances can end up serializing your threads, and the whole can even end up performing worse than if your threads had been serialized.
This is because it’s a small, dynamically allocated object, so several TCriticalSection instances can end up in the same CPU cache line, and when that happens, you’ll have cache conflicts aplenty between the cores running the threads.
How severe can that be? Well, it depends on how many cores you have, but the more cores you have, the more severe it can get. On a quad core, a bad case of contention can easily result in a 200% slowdown on top of the serialization. And it won’t always be reproducible, since it’s related to dynamic memory allocation.
There is thankfully a simple fix for that, use TFixedCriticalSection:
type TFixedCriticalSection = class(TCriticalSection) private FDummy : array [0..95] of Byte; end;
That’s it folks. This makes sure the instance size larger than 96 bytes, which means that it’ll be larger than the cache line in all current CPUs, so no serialization anymore across distinct critical section instances.
As a bonus, it also ends up using one of the larger, more aligned, FastMM bucket, which seems to improve critical section code performance by about 7%. The downside is you use more RAM… but how many critical sections do you really have?
- Fixing TCriticalSection
- Fixing Typos
- Fixing AJAX: XMLHttpRequest
- Fixing Venture Capital
- Fixing "Base SDK Missing"
- Fixing the Errant Microphone
- C. Fixing Typos
- Fixing the Great Wall
- Caffe bug fixing List
- Fixing the Great Wall
- Fixing Typos CodeForces
- C. Fixing Typos----栈
- 【转】临界区对象TCriticalSection(Delphi) 与 TRtlCriticalSection 的区别
- 【转】临界区对象TCriticalSection(Delphi) 与 TRtlCriticalSection 的区别
- delphi:临界区对象TCriticalSection(Delphi) 与 TRtlCriticalSection 的区别
- 临界区对象TCriticalSection与TRTLCriticalSection的区别
- Fixing Firefox's memory leak
- Upgrading & Fixing PCs For Dummies
- 对PHP的思考
- 每个人都有很菜的时候——“高手”请尊重
- Can't initialize OCI. Error -1
- GridView格式化日期
- aspx.cs文件提示并不包含“XX”的定义
- Fixing TCriticalSection
- 跋涉不止(24)
- Android实现开机自启动无效问题
- hdu 1032 The 3n + 1 problem
- PHP中exec、system等函数调用linux命令问题
- httpd.conf文件配置详解
- 2011.12.1央行下调存款准备金,放水4千亿RMB解读
- ICISC2011-note1
- WP份额仅1.5% 诺基亚拟推廉价机救人救己