Note that there are some explanatory texts on larger screens.

plurals
  1. POFaster TMultiReadExclusiveWriteSynchronizer?
    primarykey
    data
    text
    <p>Is there a <em>faster</em> kind of <code>TMultiReadExclusiveWriteSynchronizer</code> out there? FastCode perhaps?</p> <p>Starting with Windows Vista, Microsoft added a <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa904937%28v=vs.85%29.aspx" rel="nofollow noreferrer">Slim Reader/Writer lock</a>. It <a href="http://blog.delphi-jedi.net/2011/09/16/slim-readwrite-lock-the-fast-alternative-to-tmultireadexclusivewritesynchronizer/" rel="nofollow noreferrer">performs much better</a> than Delphi's <code>TMultiReadExclusiveWriteSynchronizer</code>. Unfortunately it only exists in Windows Vista and later, something which few customers actually have yet.</p> <p>Presumably the concepts in use inside a <code>Slim Reader/Writer lock</code> could be redone in native Delphi code - but has anyone done it?</p> <p>i have a situation where acquiring and releasing locks on a <code>TMultiReadExclusiveWriteSynchronizer</code> (even when there's no contention - a single thread), causes 100% overhead (the operation time doubles). i can run without locking, but then my class is no longer thread-safe.</p> <p>Is there a faster <code>TMultiReadExclusiveWriteSynchronizer</code>?</p> <p><strong>Note</strong>: If i use a <code>TCriticalSection</code> i only suffer a 2% performance hit (although critical sections are known to be fast when the <em>acquire</em> succeeds, i.e. while it's single threaded and there's no contention). The downside of a CS is that i lose the "<em>multiple readers</em>" capability.</p> <h1>The Measurements</h1> <p>Using <code>TMultiReadExclusiveWriteSynchronizer</code> a sizable amount of time is spent inside <code>BeginRead</code> and <code>EndRead</code>:</p> <p><img src="https://i.stack.imgur.com/mPIyZ.png" alt="enter image description here"></p> <p>i then ported the code to use Window's own <strong>SlimReaderWriter Lock</strong> (which some code rewrite, as it doesn't support recursive lock taking), and profiled the resutls:</p> <ul> <li><p><strong><code>TMultiReadExclusiveWriteSynchronizer</code></strong>: 10,698 ns per iteration<br> 10,697,772,613 ns to iterate 1,000,000 times </p></li> <li><p><strong><code>SRWLock</code></strong>: 8,802 ns per iteration<br> 8,801,678,339 ns to iterate 1,000,000 times</p></li> <li><p><strong><code>Omni Reader-Writer lock</code></strong>: 8,941 ns per iteration<br> 8,940,552,487 ns to iterate 1,000,000 times</p></li> </ul> <p>A 17% improvement when using SRWLocks (aka Omni's spinning lock).</p> <p>Now, i cannot switch the code permanantly over to use <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa904937.aspx" rel="nofollow noreferrer">Windows Vista SRWLocks</a>, as there are some entire enterprises of customers that are still on Windows XP.</p> <p>The Slim locks are just careful use of <code>InterlockedCompareExchange</code> functions; but more careful than i can successfully use. I'm <em>this</em> far away from just stealing the 140 machine instructions involved, and have it done.</p> <h2>Bonus Reading</h2> <ul> <li><a href="https://stackoverflow.com/questions/853316/is-critical-section-always-faster">Is Critical Section always faster?</a></li> <li><a href="http://synopse.info/forum/viewtopic.php?id=322" rel="nofollow noreferrer">How to write fast multi-thread Delphi applications</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload