Skip to content

Commit

Permalink
SLazy improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
ZacharyPatten committed Aug 3, 2021
1 parent b3105cd commit 65ca739
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 62 deletions.
38 changes: 19 additions & 19 deletions Sources/Towel/SLazy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,19 @@ internal class Reference

internal T SafeGetValue()
{
if (_func is not null)
lock (this)
{
lock (this)
if (_func is not null)
{
if (_func is not null)
try
{
try
{
T value = _func();
_value = value;
_func = null;
}
catch (Exception exception)
{
_func = () => throw exception;
throw;
}
_value = _func();
_func = null;
}
catch (Exception exception)
{
_func = () => throw exception;
throw;
}
}
}
Expand All @@ -48,13 +44,17 @@ public bool IsValueCreated
get
{
Reference? reference = _reference;
if (reference is not null && reference._func is null)
if (reference is not null)
{
_value = reference._value;
_reference = null;
return true;
if (reference._func is null)
{
_value = reference._value;
_reference = null;
return true;
}
return false;
}
return reference is null;
return true;
}
}

Expand Down
24 changes: 12 additions & 12 deletions Tools/docfx_project/benchmarks/SLazyCachingBenchmarks.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ Intel Core i7-4790K CPU 4.00GHz (Haswell), 1 CPU, 8 logical and 4 physical cores
```
| Method | N | Mean | Error | StdDev | Median | Ratio | RatioSD |
|---------------------------- |----- |----------:|---------:|---------:|----------:|------:|--------:|
| **Lazy** | **1** | **32.33 ns** | **0.674 ns** | **1.422 ns** | **32.05 ns** | **1.00** | **0.00** |
| LazyExecutionAndPublication | 1 | 30.87 ns | 0.498 ns | 0.511 ns | 30.87 ns | 0.98 | 0.05 |
| SLazy | 1 | 25.03 ns | 0.524 ns | 0.514 ns | 24.86 ns | 0.79 | 0.03 |
| **Lazy** | **1** | **32.00 ns** | **0.655 ns** | **1.058 ns** | **31.46 ns** | **1.00** | **0.00** |
| LazyExecutionAndPublication | 1 | 30.76 ns | 0.362 ns | 0.339 ns | 30.63 ns | 0.97 | 0.03 |
| SLazy | 1 | 25.74 ns | 0.163 ns | 0.152 ns | 25.74 ns | 0.81 | 0.03 |
| | | | | | | | |
| **Lazy** | **10** | **35.87 ns** | **0.157 ns** | **0.139 ns** | **35.83 ns** | **1.00** | **0.00** |
| LazyExecutionAndPublication | 10 | 36.09 ns | 0.699 ns | 0.748 ns | 35.84 ns | 1.01 | 0.02 |
| SLazy | 10 | 29.76 ns | 0.624 ns | 0.694 ns | 29.85 ns | 0.83 | 0.02 |
| **Lazy** | **10** | **36.97 ns** | **0.755 ns** | **0.981 ns** | **36.74 ns** | **1.00** | **0.00** |
| LazyExecutionAndPublication | 10 | 38.74 ns | 0.802 ns | 1.842 ns | 38.45 ns | 1.07 | 0.05 |
| SLazy | 10 | 30.47 ns | 0.607 ns | 0.890 ns | 30.48 ns | 0.83 | 0.03 |
| | | | | | | | |
| **Lazy** | **100** | **95.12 ns** | **2.716 ns** | **7.661 ns** | **91.62 ns** | **1.00** | **0.00** |
| LazyExecutionAndPublication | 100 | 92.24 ns | 1.494 ns | 1.397 ns | 91.86 ns | 0.94 | 0.09 |
| SLazy | 100 | 81.92 ns | 1.615 ns | 1.795 ns | 82.14 ns | 0.82 | 0.08 |
| **Lazy** | **100** | **93.79 ns** | **1.500 ns** | **1.403 ns** | **93.76 ns** | **1.00** | **0.00** |
| LazyExecutionAndPublication | 100 | 91.69 ns | 1.021 ns | 0.905 ns | 91.61 ns | 0.98 | 0.02 |
| SLazy | 100 | 81.08 ns | 0.506 ns | 0.473 ns | 80.92 ns | 0.86 | 0.02 |
| | | | | | | | |
| **Lazy** | **1000** | **534.87 ns** | **7.279 ns** | **6.452 ns** | **532.73 ns** | **1.00** | **0.00** |
| LazyExecutionAndPublication | 1000 | 534.40 ns | 3.536 ns | 3.308 ns | 533.46 ns | 1.00 | 0.01 |
| SLazy | 1000 | 520.42 ns | 2.383 ns | 2.229 ns | 520.21 ns | 0.97 | 0.01 |
| **Lazy** | **1000** | **556.80 ns** | **5.807 ns** | **5.147 ns** | **555.27 ns** | **1.00** | **0.00** |
| LazyExecutionAndPublication | 1000 | 543.50 ns | 4.022 ns | 3.762 ns | 542.86 ns | 0.98 | 0.01 |
| SLazy | 1000 | 526.17 ns | 1.857 ns | 1.737 ns | 526.79 ns | 0.94 | 0.01 |

30 changes: 15 additions & 15 deletions Tools/docfx_project/benchmarks/SLazyConstructionBenchmarks.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@ Intel Core i7-4790K CPU 4.00GHz (Haswell), 1 CPU, 8 logical and 4 physical cores
```
| Method | N | Mean | Error | StdDev | Median | Ratio | RatioSD | Gen 0 | Gen 1 | Gen 2 | Allocated |
|---------------------------- |------ |--------------:|-------------:|--------------:|--------------:|------:|--------:|---------:|------:|------:|------------:|
| **Lazy** | **1** | **23.49 ns** | **0.075 ns** | **0.066 ns** | **23.48 ns** | **1.00** | **0.00** | **0.0382** | **-** | **-** | **160 B** |
| LazyExecutionAndPublication | 1 | 21.54 ns | 0.143 ns | 0.127 ns | 21.55 ns | 0.92 | 0.01 | 0.0382 | - | - | 160 B |
| SLazy | 1 | 16.10 ns | 0.263 ns | 0.246 ns | 15.99 ns | 0.69 | 0.01 | 0.0287 | - | - | 120 B |
| **Lazy** | **1** | **22.50 ns** | **0.449 ns** | **0.763 ns** | **22.16 ns** | **1.00** | **0.00** | **0.0382** | **-** | **-** | **160 B** |
| LazyExecutionAndPublication | 1 | 22.22 ns | 0.466 ns | 0.536 ns | 22.11 ns | 0.97 | 0.05 | 0.0382 | - | - | 160 B |
| SLazy | 1 | 16.22 ns | 0.137 ns | 0.114 ns | 16.23 ns | 0.70 | 0.02 | 0.0287 | - | - | 120 B |
| | | | | | | | | | | | |
| **Lazy** | **10** | **195.37 ns** | **1.385 ns** | **1.295 ns** | **195.02 ns** | **1.00** | **0.00** | **0.3309** | **-** | **-** | **1,384 B** |
| LazyExecutionAndPublication | 10 | 207.25 ns | 4.024 ns | 6.264 ns | 206.40 ns | 1.07 | 0.03 | 0.3309 | - | - | 1,384 B |
| SLazy | 10 | 147.05 ns | 1.701 ns | 1.420 ns | 147.27 ns | 0.75 | 0.01 | 0.2351 | - | - | 984 B |
| **Lazy** | **10** | **202.49 ns** | **3.694 ns** | **3.628 ns** | **200.95 ns** | **1.00** | **0.00** | **0.3309** | **-** | **-** | **1,384 B** |
| LazyExecutionAndPublication | 10 | 201.18 ns | 1.589 ns | 1.487 ns | 201.02 ns | 0.99 | 0.02 | 0.3309 | - | - | 1,384 B |
| SLazy | 10 | 143.28 ns | 2.875 ns | 2.824 ns | 142.50 ns | 0.71 | 0.02 | 0.2351 | - | - | 984 B |
| | | | | | | | | | | | |
| **Lazy** | **100** | **1,890.13 ns** | **28.512 ns** | **23.809 ns** | **1,881.19 ns** | **1.00** | **0.00** | **3.2558** | **-** | **-** | **13,624 B** |
| LazyExecutionAndPublication | 100 | 1,996.97 ns | 38.941 ns | 41.666 ns | 1,978.26 ns | 1.06 | 0.03 | 3.2539 | - | - | 13,624 B |
| SLazy | 100 | 1,339.82 ns | 25.846 ns | 26.542 ns | 1,336.61 ns | 0.71 | 0.02 | 2.3003 | - | - | 9,624 B |
| **Lazy** | **100** | **2,012.92 ns** | **30.173 ns** | **34.747 ns** | **2,003.43 ns** | **1.00** | **0.00** | **3.2539** | **-** | **-** | **13,624 B** |
| LazyExecutionAndPublication | 100 | 2,034.90 ns | 35.676 ns | 42.469 ns | 2,024.93 ns | 1.01 | 0.03 | 3.2539 | - | - | 13,624 B |
| SLazy | 100 | 1,360.76 ns | 25.861 ns | 21.595 ns | 1,359.66 ns | 0.67 | 0.02 | 2.3003 | - | - | 9,624 B |
| | | | | | | | | | | | |
| **Lazy** | **1000** | **19,320.80 ns** | **381.224 ns** | **423.730 ns** | **19,347.76 ns** | **1.00** | **0.00** | **32.5012** | **-** | **-** | **136,024 B** |
| LazyExecutionAndPublication | 1000 | 19,640.55 ns | 347.447 ns | 325.002 ns | 19,690.26 ns | 1.01 | 0.03 | 32.5012 | - | - | 136,024 B |
| SLazy | 1000 | 13,186.66 ns | 209.667 ns | 185.865 ns | 13,151.21 ns | 0.68 | 0.02 | 22.9492 | - | - | 96,024 B |
| **Lazy** | **1000** | **19,391.63 ns** | **195.598 ns** | **163.333 ns** | **19,312.63 ns** | **1.00** | **0.00** | **32.5012** | **-** | **-** | **136,024 B** |
| LazyExecutionAndPublication | 1000 | 21,608.07 ns | 308.875 ns | 288.922 ns | 21,617.02 ns | 1.11 | 0.01 | 32.5012 | - | - | 136,024 B |
| SLazy | 1000 | 13,113.88 ns | 200.511 ns | 167.436 ns | 13,089.79 ns | 0.68 | 0.01 | 22.9492 | - | - | 96,024 B |
| | | | | | | | | | | | |
| **Lazy** | **10000** | **213,004.25 ns** | **9,668.635 ns** | **27,741.126 ns** | **198,535.77 ns** | **1.00** | **0.00** | **325.1953** | **-** | **-** | **1,360,024 B** |
| LazyExecutionAndPublication | 10000 | 192,453.36 ns | 3,600.379 ns | 3,367.797 ns | 190,729.71 ns | 0.92 | 0.10 | 325.1953 | - | - | 1,360,024 B |
| SLazy | 10000 | 129,510.58 ns | 2,045.435 ns | 2,799.815 ns | 128,340.17 ns | 0.60 | 0.07 | 229.4922 | - | - | 960,024 B |
| **Lazy** | **10000** | **201,931.58 ns** | **4,956.269 ns** | **14,140.514 ns** | **195,520.00 ns** | **1.00** | **0.00** | **325.1953** | **-** | **-** | **1,360,024 B** |
| LazyExecutionAndPublication | 10000 | 200,473.35 ns | 1,523.730 ns | 1,189.628 ns | 200,682.32 ns | 0.98 | 0.07 | 325.1953 | - | - | 1,360,024 B |
| SLazy | 10000 | 136,375.92 ns | 2,699.036 ns | 6,092.175 ns | 133,926.17 ns | 0.67 | 0.05 | 229.4922 | - | - | 960,024 B |

32 changes: 16 additions & 16 deletions Tools/docfx_project/benchmarks/SLazyInitializationBenchmarks.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,30 @@ BenchmarkDotNet=v0.13.0, OS=Windows 10.0.19042.1110 (20H2/October2020Update)
Intel Core i7-4790K CPU 4.00GHz (Haswell), 1 CPU, 8 logical and 4 physical cores
.NET SDK=6.0.100-preview.6.21355.2
[Host] : .NET 5.0.8 (5.0.821.31504), X64 RyuJIT
Job-BJDLWC : .NET 5.0.8 (5.0.821.31504), X64 RyuJIT
Job-BVARYK : .NET 5.0.8 (5.0.821.31504), X64 RyuJIT

InvocationCount=1 UnrollFactor=1

```
| Method | N | Mean | Error | StdDev | Median | Ratio | RatioSD |
|---------------------------- |------ |-------------:|-------------:|-------------:|-------------:|------:|--------:|
| **Lazy** | **1** | **350.5 ns** | **23.02 ns** | **65.31 ns** | **300.0 ns** | **1.00** | **0.00** |
| LazyExecutionAndPublication | 1 | 389.0 ns | 11.22 ns | 31.45 ns | 400.0 ns | 1.14 | 0.21 |
| SLazy | 1 | 300.0 ns | 0.00 ns | 0.00 ns | 300.0 ns | 0.78 | 0.14 |
| **Lazy** | **1** | **400.0 ns** | **0.00 ns** | **0.00 ns** | **400.0 ns** | **1.00** | **0.00** |
| LazyExecutionAndPublication | 1 | 400.0 ns | 0.00 ns | 0.00 ns | 400.0 ns | 1.00 | 0.00 |
| SLazy | 1 | 200.0 ns | 0.00 ns | 0.00 ns | 200.0 ns | 0.50 | 0.00 |
| | | | | | | | |
| **Lazy** | **10** | **665.7 ns** | **18.97 ns** | **55.63 ns** | **700.0 ns** | **1.00** | **0.00** |
| LazyExecutionAndPublication | 10 | 654.9 ns | 21.44 ns | 60.12 ns | 700.0 ns | 0.99 | 0.12 |
| SLazy | 10 | 476.9 ns | 21.32 ns | 59.77 ns | 500.0 ns | 0.72 | 0.11 |
| **Lazy** | **10** | **670.9 ns** | **21.74 ns** | **59.14 ns** | **700.0 ns** | **1.00** | **0.00** |
| LazyExecutionAndPublication | 10 | 653.6 ns | 18.66 ns | 54.13 ns | 700.0 ns | 0.99 | 0.11 |
| SLazy | 10 | 456.6 ns | 20.18 ns | 59.18 ns | 500.0 ns | 0.69 | 0.10 |
| | | | | | | | |
| **Lazy** | **100** | **2,926.3 ns** | **58.78 ns** | **65.34 ns** | **2,900.0 ns** | **1.00** | **0.00** |
| LazyExecutionAndPublication | 100 | 2,868.2 ns | 58.32 ns | 71.62 ns | 2,900.0 ns | 0.98 | 0.03 |
| SLazy | 100 | 2,268.2 ns | 38.82 ns | 47.67 ns | 2,300.0 ns | 0.78 | 0.02 |
| **Lazy** | **100** | **2,916.7 ns** | **57.79 ns** | **61.83 ns** | **2,900.0 ns** | **1.00** | **0.00** |
| LazyExecutionAndPublication | 100 | 2,915.0 ns | 58.25 ns | 67.08 ns | 2,900.0 ns | 1.00 | 0.03 |
| SLazy | 100 | 2,252.9 ns | 46.33 ns | 74.81 ns | 2,200.0 ns | 0.78 | 0.03 |
| | | | | | | | |
| **Lazy** | **1000** | **25,041.7 ns** | **85.63 ns** | **66.86 ns** | **25,050.0 ns** | **1.00** | **0.00** |
| LazyExecutionAndPublication | 1000 | 25,133.3 ns | 406.26 ns | 317.18 ns | 25,150.0 ns | 1.00 | 0.01 |
| SLazy | 1000 | 19,976.9 ns | 263.97 ns | 220.43 ns | 19,900.0 ns | 0.80 | 0.01 |
| **Lazy** | **1000** | **25,335.7 ns** | **475.74 ns** | **421.73 ns** | **25,150.0 ns** | **1.00** | **0.00** |
| LazyExecutionAndPublication | 1000 | 25,042.9 ns | 268.10 ns | 237.66 ns | 24,900.0 ns | 0.99 | 0.02 |
| SLazy | 1000 | 19,716.7 ns | 106.93 ns | 83.48 ns | 19,700.0 ns | 0.78 | 0.01 |
| | | | | | | | |
| **Lazy** | **10000** | **218,455.6 ns** | **11,333.33 ns** | **33,238.69 ns** | **201,200.0 ns** | **1.00** | **0.00** |
| LazyExecutionAndPublication | 10000 | 242,215.4 ns | 3,729.99 ns | 3,114.71 ns | 243,200.0 ns | 0.96 | 0.09 |
| SLazy | 10000 | 187,578.5 ns | 4,858.01 ns | 13,781.37 ns | 185,700.0 ns | 0.88 | 0.13 |
| **Lazy** | **10000** | **223,144.9 ns** | **11,838.31 ns** | **34,532.90 ns** | **212,750.0 ns** | **1.00** | **0.00** |
| LazyExecutionAndPublication | 10000 | 221,203.1 ns | 12,431.72 ns | 36,066.68 ns | 203,500.0 ns | 1.00 | 0.17 |
| SLazy | 10000 | 196,614.6 ns | 7,590.36 ns | 22,261.20 ns | 193,650.0 ns | 0.90 | 0.13 |

0 comments on commit 65ca739

Please sign in to comment.