DI: benchmark atomic locking

This commit is contained in:
Fischlurch 2018-03-24 11:02:22 +01:00
parent d78211a9a1
commit 7a250ca9e5
3 changed files with 46 additions and 31 deletions

View file

@ -106,7 +106,7 @@ main (int, char**)
{
0 == mystery().readMe();
}
,1000000000)
,5000000000)
<< endl;
LifecycleHook::trigger (ON_GLOBAL_SHUTDOWN);

View file

@ -1927,7 +1927,7 @@ As we don't have a Prolog interpreter on board yet, we utilize a mock store with
{{{default(Obj)}}} is a predicate expressing that the object {{{Obj}}} can be considered the default setup under the given conditions. Using the //default// can be considered as a shortcut for actually finding an exact and unique solution. The latter would require to specify all sorts of detailed properties up to the point where only one single object can satisfy all conditions. On the other hand, leaving some properties unspecified would yield a set of solutions (and the user code issuing the query had to provide means for selecting one solution from this set). Just falling back on the //default// means that the user code actually doesn't care for any additional properties (as long as the properties he //does// care for are satisfied). Nothing is said specifically on //how//&amp;nbsp; this default gets configured; actually there can be rules //somewhere,// and, additionally, anything encountered once while asking for a default can be re-used as default under similar circumstances.
&amp;rarr; [[implementing defaults|DefaultsImplementation]]</pre>
</div>
<div title="DependencyFactory" creator="Ichthyostega" modifier="Ichthyostega" created="201803110155" modified="201803240827" tags="def Concepts draft" changecount="49">
<div title="DependencyFactory" creator="Ichthyostega" modifier="Ichthyostega" created="201803110155" modified="201803240948" tags="def Concepts draft" changecount="53">
<pre>//Access point to dependencies by-name.//
In the Lumiera code base, we refrain from building or using a full-blown Dependency Injection Container. A lot of FUD has been spread regarding Dependency Injection and Singletons, to the point that a majority of developers confuses and conflates the ~Inversion-of-Control principle (which is essential) with the use of a ~DI-Container. Today, you can not even mention the word &quot;Singleton&quot; without everyone yelling out &quot;Evil! Evil!&quot; -- while most of these people just feel comfortable living in the metadata hell.
@ -1984,10 +1984,19 @@ The following table lists averaged results in relative numbers, in relation to a
|direct invoke shared local object | 15.13| 16.30| ''1.00''| 1.59|
|invoke existing object through unique_ptr | 60.76| 63.20| 1.20| 1.64|
|lazy init unprotected (not threadsafe) | 27.29| 26.57| 2.37| 3.58|
|lazy init always mutex protected | 179,62| 10917.18| 86.40| 6661.23|
|Double Checked Locking with mutex | 27,37| 26,27| 2.04| 3.26|
|lazy init always mutex protected | 179.62| 10917.18| 86.40| 6661.23|
|Double Checked Locking with mutex | 27.37| 26.27| 2.04| 3.26|
|DCL with std::atomic and mutex for init | 44.06| 52.27| 2,79| 4.04|
These benchmarks used a dummy service class holding a volatile int, initialised to a random value. The complete code was visible to the compiler and thus eligible for inlining. After accessing this dummy object through the means listed in the table, the benchmarked code retrieved this value repeatedly and compared it to zero. The concurrent measurement used 8 threads (number of cores); as expected, the unprotected lazy init crashed several times randomly during those tests.
These benchmarks used a dummy service class holding a volatile int, initialised to a random value. The complete code was visible to the compiler and thus eligible for inlining. After accessing this dummy object through the means listed in the table, the benchmarked code retrieved this value repeatedly and compared it to zero. The concurrent measurement used 8 threads (number of cores). Some observations
* The numbers obtained pretty much confirm [[other people's measurments|http://www.modernescpp.com/index.php/thread-safe-initialization-of-a-singleton]]
* Synchronisation is indeed necessary; the unprotected lazy init crashed several times randomly during those tests.
* Contention on concurrent access is very tangible; even for unguarded access the cache and memory hardware has to perform additional work
* However, the concurrency situation in this example is rather extreme and deliberately provokes collisions; in practice we'd be closer to the single threaded case
* Double Checked Locking is a very effective implementation strategy and results in timings within the same order of magnitude as direct access
* Unprotected lazy initialisation performs spurious duplicate initialisations, which can be avoided by DCL
* Naive Mutex locking is slow even without contention
* Optimisation achieves access times of ≈ 1ns
</pre>
</div>
<div title="DesignDecisions" modifier="Ichthyostega" created="200801062209" modified="201505310104" tags="decision design discuss Concepts" changecount="5">

View file

@ -26914,8 +26914,8 @@
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1520722149761" ID="ID_39179906" MODIFIED="1521160728142" TEXT="neue Implementierung" VGAP="6">
<icon BUILTIN="pencil"/>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1521160662691" ID="ID_1475127406" MODIFIED="1521238630164" TEXT="Design Syntax und Nutzungsmuster">
<icon BUILTIN="flag-yellow"/>
<node COLOR="#338800" CREATED="1521160662691" ID="ID_1475127406" MODIFIED="1521885033360" TEXT="Design Syntax und Nutzungsmuster">
<icon BUILTIN="button_ok"/>
<node CREATED="1521161117092" ID="ID_672223000" MODIFIED="1521161119935" TEXT="Syntax">
<node CREATED="1521161120859" ID="ID_1236102866" MODIFIED="1521161279265" TEXT="Depend&lt;Bla&gt; instance"/>
<node CREATED="1521161130050" ID="ID_1706757665" MODIFIED="1521161170232" TEXT="instance().doIt()"/>
@ -26926,7 +26926,7 @@
</node>
<node CREATED="1521207954859" ID="ID_631339002" MODIFIED="1521418307590" TEXT="Analyse">
<icon BUILTIN="idea"/>
<node CREATED="1521208242178" ID="ID_539882602" MODIFIED="1521696589693" TEXT="erforderliche Mechanismen">
<node CREATED="1521208242178" FOLDED="true" ID="ID_539882602" MODIFIED="1521885037640" TEXT="erforderliche Mechanismen">
<icon BUILTIN="info"/>
<node COLOR="#338800" CREATED="1521208254376" ID="ID_1933406851" MODIFIED="1521696578693" TEXT="Singleton">
<icon BUILTIN="button_ok"/>
@ -27347,8 +27347,8 @@
</node>
</node>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1521160691830" ID="ID_53329830" MODIFIED="1521791648429" TEXT="Implementierung ausf&#xfc;hren">
<icon BUILTIN="flag-yellow"/>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1521160691830" ID="ID_53329830" MODIFIED="1521885000400" TEXT="Implementierung ausf&#xfc;hren">
<icon BUILTIN="pencil"/>
<node COLOR="#338800" CREATED="1521418559868" ID="ID_592269917" MODIFIED="1521688403021" TEXT="Depend&lt;SRV&gt; Front-End">
<icon BUILTIN="button_ok"/>
</node>
@ -27404,10 +27404,10 @@
</node>
</node>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1521433938944" ID="ID_546086969" MODIFIED="1521433946432" TEXT="double-checked-locking">
<icon BUILTIN="flag-yellow"/>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1521433948534" ID="ID_176845865" MODIFIED="1521433954006" TEXT="umstellen auf atomics">
<icon BUILTIN="flag-yellow"/>
<node COLOR="#338800" CREATED="1521433938944" ID="ID_546086969" MODIFIED="1521884986940" TEXT="double-checked-locking">
<icon BUILTIN="button_ok"/>
<node COLOR="#338800" CREATED="1521433948534" ID="ID_176845865" MODIFIED="1521882845033" TEXT="umstellen auf atomics">
<icon BUILTIN="button_ok"/>
<node CREATED="1521779239745" ID="ID_1772115642" MODIFIED="1521783134468">
<richcontent TYPE="NODE"><html>
<head>
@ -27437,7 +27437,9 @@
</html>
</richcontent>
</node>
<node CREATED="1521788423421" ID="ID_992901826" MODIFIED="1521788427481" TEXT="use-cases">
<node CREATED="1521788423421" FOLDED="true" ID="ID_992901826" MODIFIED="1521882883828" TEXT="use-cases">
<font NAME="SansSerif" SIZE="12"/>
<icon BUILTIN="forward"/>
<node CREATED="1521788428588" ID="ID_1454492096" MODIFIED="1521788437479" TEXT="lazy init">
<node CREATED="1521788443562" ID="ID_699722245" MODIFIED="1521788611886" TEXT="acquire-release ist &quot;ausreichend&quot;">
<richcontent TYPE="NOTE"><html>
@ -27660,8 +27662,8 @@
</node>
</node>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1521434009142" ID="ID_117782655" MODIFIED="1521434015486" TEXT="Performance-Messung">
<icon BUILTIN="flag-yellow"/>
<node COLOR="#338800" CREATED="1521434009142" ID="ID_117782655" MODIFIED="1521884984027" TEXT="Performance-Messung">
<icon BUILTIN="button_ok"/>
<node CREATED="1521791686994" ID="ID_1750734364" MODIFIED="1521791753975" TEXT="Erwartetes Ergebnis: wird nur geringf&#xfc;gig langsamer">
<richcontent TYPE="NOTE"><html>
<head>
@ -27675,22 +27677,23 @@
</html>
</richcontent>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1521866206751" ID="ID_702466096" MODIFIED="1521866223424" TEXT="Bezugspunkt: Zugriff auf bereits existierenden Service">
<icon BUILTIN="flag-yellow"/>
<node COLOR="#338800" CREATED="1521866206751" ID="ID_702466096" MODIFIED="1521882798384" TEXT="Bezugspunkt: Zugriff auf bereits existierenden Service">
<icon BUILTIN="button_ok"/>
</node>
<node CREATED="1521791785677" ID="ID_565916879" MODIFIED="1521791789208" TEXT="drei F&#xe4;lle">
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1521791790172" ID="ID_995415475" MODIFIED="1521791831096" TEXT="Mutex f&#xfc;r jeden Zugriff">
<icon BUILTIN="flag-yellow"/>
<node CREATED="1521791785677" ID="ID_565916879" MODIFIED="1521882832691" TEXT="drei F&#xe4;lle">
<icon BUILTIN="forward"/>
<node COLOR="#338800" CREATED="1521791790172" ID="ID_995415475" MODIFIED="1521882801672" TEXT="Mutex f&#xfc;r jeden Zugriff">
<icon BUILTIN="button_ok"/>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1521791805930" ID="ID_1036499157" MODIFIED="1521791832702" TEXT="L&#xf6;sung ohne Atomics (inkorrekt)">
<icon BUILTIN="flag-yellow"/>
<node COLOR="#338800" CREATED="1521791805930" ID="ID_1036499157" MODIFIED="1521882803656" TEXT="L&#xf6;sung ohne Atomics (inkorrekt)">
<icon BUILTIN="button_ok"/>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1521791823055" ID="ID_1820322207" MODIFIED="1521791833719" TEXT="L&#xf6;sung mit Atomics">
<icon BUILTIN="flag-yellow"/>
<node COLOR="#338800" CREATED="1521791823055" ID="ID_1820322207" MODIFIED="1521882806238" TEXT="L&#xf6;sung mit Atomics">
<icon BUILTIN="button_ok"/>
</node>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1521791837014" ID="ID_1625592637" MODIFIED="1521791845516" TEXT="Messwerte in Prozent umrechnen">
<icon BUILTIN="flag-yellow"/>
<node COLOR="#338800" CREATED="1521791837014" ID="ID_1625592637" MODIFIED="1521882823105" TEXT="Messwerte als Verh&#xe4;ltniszahl angeben">
<icon BUILTIN="button_ok"/>
</node>
</node>
</node>
@ -27699,12 +27702,15 @@
<icon BUILTIN="flag-yellow"/>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1521790595841" ID="ID_1874367277" MODIFIED="1521790983987" TEXT="Doku-Text im TiddlyWiki">
<icon BUILTIN="flag-yellow"/>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1521790613318" ID="ID_506011313" MODIFIED="1521790986067" TEXT="Diskussion der Locking / Atomic-Problematik">
<icon BUILTIN="flag-yellow"/>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1521790681557" ID="ID_1346970835" MODIFIED="1521790986851" TEXT="die generelle Haltung bez&#xfc;gl. Performance / Korrektheit">
<icon BUILTIN="flag-yellow"/>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1521790613318" ID="ID_506011313" MODIFIED="1521885094991" TEXT="Diskussion der Locking / Atomic-Problematik">
<icon BUILTIN="flag-yellow"/>
</node>
<node COLOR="#338800" CREATED="1521885096858" ID="ID_1403195900" MODIFIED="1521885106977" TEXT="Benchmark-Zahlen dokumentieren">
<icon BUILTIN="button_ok"/>
</node>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1521790709017" ID="ID_1263887689" MODIFIED="1521790982924" TEXT="Doxygen">
<icon BUILTIN="flag-yellow"/>