Currently, Jab uses locks to guarantee Singleton is only created one time.
But alternative Lazy<T> is a faster alternative to lock.
Instead of
private Jab.Performance.Basic.Singleton.Singleton1? _ISingleton1;
Jab.Performance.Basic.Singleton.ISingleton1 IServiceProvider<Jab.Performance.Basic.Singleton.ISingleton1>.GetService()
{
if (_ISingleton1 == null)
lock (this)
if (_ISingleton1 == null){
_ISingleton1 = new Jab.Performance.Basic.Singleton.Singleton1();
}
return _ISingleton1;
}
The JAB uses this code
public ImprovedContainerSingleton()
{
_ISingleton1 = new(() => new Jab.Performance.Basic.Singleton.Singleton1());
}
private Lazy<Jab.Performance.Basic.Singleton.Singleton1> _ISingleton1;
Jab.Performance.Basic.Singleton.ISingleton1 IServiceProvider<Jab.Performance.Basic.Singleton.ISingleton1>.GetService() => _ISingleton1.Value;
We can see up to 35% of performance improvements.
| Method |
NumbersOfCalls |
NumbersOfClasses |
Mean |
Error |
StdDev |
Ratio |
RatioSD |
| Jab |
1 |
1 |
3.190 ns |
0.3329 ns |
0.0182 ns |
1.00 |
0.00 |
| Improved_Jab |
1 |
1 |
1.968 ns |
0.1505 ns |
0.0082 ns |
0.62 |
0.00 |
| MEDI |
1 |
1 |
9.357 ns |
0.2965 ns |
0.0163 ns |
2.93 |
0.02 |
|
|
|
|
|
|
|
|
| Jab |
100 |
3 |
693.577 ns |
30.0415 ns |
1.6467 ns |
1.00 |
0.00 |
| Improved_Jab |
100 |
3 |
548.498 ns |
7.3230 ns |
0.4014 ns |
0.79 |
0.00 |
| MEDI |
100 |
3 |
2,909.288 ns |
33.8740 ns |
1.8567 ns |
4.19 |
0.01 |
See Details: Jab.Performance.Basic.Singleton.BasicSingletonBenchmark-report-github.md
Currently, Jab uses
locksto guarantee Singleton is only created one time.But alternative
Lazy<T>is a faster alternative tolock.Instead of
The JAB uses this code
We can see up to 35% of performance improvements.
See Details: Jab.Performance.Basic.Singleton.BasicSingletonBenchmark-report-github.md