From 96761f9a4a23bdf782cfef20c512a81842c50eb5 Mon Sep 17 00:00:00 2001 From: Nikolay Pianikov Date: Thu, 21 Nov 2024 10:11:47 +0300 Subject: [PATCH] Supporting of System.Threading.Lock type --- .github/workflows/main.yml | 2 +- Directory.Build.props | 2 +- README.md | 124 +++++++++--------- .../Pure.DI.Benchmarks.csproj | 4 +- ...DI.Benchmarks.Benchmarks.Array-report.html | 38 +++--- ....DI.Benchmarks.Benchmarks.Enum-report.html | 38 +++--- ....DI.Benchmarks.Benchmarks.Func-report.html | 38 +++--- ...enchmarks.Benchmarks.Singleton-report.html | 38 +++--- ...enchmarks.Benchmarks.Transient-report.html | 36 ++--- build/CompatibilityCheckTarget.cs | 1 + build/ReadmeTarget.cs | 2 +- readme/Avalonia.md | 2 +- readme/BlazorServerApp.md | 2 +- readme/BlazorWebAssemblyApp.md | 2 +- readme/Console.md | 2 +- readme/ConsoleTopLevelStatements.md | 2 +- readme/GrpcService.md | 2 +- readme/ReadmeTemplate.md | 8 +- readme/WebAPI.md | 2 +- readme/WebApp.md | 2 +- readme/WinFormsAppNetCore.md | 2 +- readme/Wpf.md | 2 +- readme/accumulators.md | 10 +- readme/async-disposable-scope.md | 15 ++- readme/async-disposable-singleton.md | 15 ++- readme/auto-scoped.md | 6 +- readme/bind-attribute-for-a-generic-type.md | 6 +- .../bind-attribute-with-lifetime-and-tag.md | 6 +- readme/bind-attribute.md | 6 +- readme/custom-attributes.md | 8 +- readme/custom-universal-attribute.md | 8 +- .../default-lifetime-for-a-type-and-a-tag.md | 8 +- readme/default-lifetime-for-a-type.md | 6 +- readme/default-lifetime.md | 6 +- readme/disposable-singleton.md | 8 +- readme/exposed-generic-roots-with-args.md | 6 +- readme/exposed-generic-roots.md | 6 +- readme/exposed-roots-with-tags.md | 6 +- readme/exposed-roots.md | 6 +- readme/func-with-arguments.md | 6 +- readme/keyed-service-provider.md | 8 +- readme/onnewinstance-hint.md | 6 +- readme/perblock.md | 6 +- readme/perresolve.md | 8 +- readme/root-binding.md | 6 +- readme/scope.md | 8 +- readme/service-collection.md | 8 +- readme/service-provider-with-scope.md | 10 +- readme/service-provider.md | 8 +- readme/singleton.md | 6 +- readme/tag-type.md | 8 +- readme/tags.md | 8 +- ...async-disposable-instances-in-delegates.md | 10 +- ...osable-instances-per-a-composition-root.md | 8 +- ...cking-disposable-instances-in-delegates.md | 10 +- ...osable-instances-per-a-composition-root.md | 8 +- ...able-instances-with-different-lifetimes.md | 16 +-- .../BlazorWebAssemblyApp.csproj | 2 +- samples/Clock.Tests/Clock.Tests.csproj | 2 +- samples/GrpcService/GrpcService.csproj | 2 +- samples/MinimalWebAPI/MinimalWebAPI.csproj | 2 +- .../WeatherForecast/WeatherForecast.csproj | 2 +- samples/WebAPI/WebAPI.csproj | 2 +- .../Core/AsyncDisposableSettings.cs | 12 -- src/Pure.DI.Core/Core/BindingBuilder.cs | 4 +- .../Core/Code/ApiMembersBuilder.cs | 2 +- .../Core/Code/BlockCodeBuilder.cs | 11 +- src/Pure.DI.Core/Core/Code/BuildTools.cs | 7 +- src/Pure.DI.Core/Core/Code/ClassBuilder.cs | 2 +- .../Core/Code/ClassDiagramBuilder.cs | 2 +- .../Core/Code/DefaultConstructorBuilder.cs | 6 +- .../Core/Code/DisposeMethodBuilder.cs | 14 +- src/Pure.DI.Core/Core/Code/FieldsBuilder.cs | 9 +- src/Pure.DI.Core/Core/Code/ILocks.cs | 10 ++ src/Pure.DI.Core/Core/Code/Locks.cs | 42 ++++++ src/Pure.DI.Core/Core/Code/NodeInfo.cs | 6 +- .../Code/ParameterizedConstructorBuilder.cs | 5 +- .../Core/DependencyGraphBuilder.cs | 2 +- .../Core/IAsyncDisposableSettings.cs | 6 - src/Pure.DI.Core/Core/ITypes.cs | 6 + .../Core/Models/CompositionCode.cs | 7 +- src/Pure.DI.Core/Core/Names.cs | 7 +- src/Pure.DI.Core/Core/SpecialType.cs | 8 ++ src/Pure.DI.Core/Core/SpecialTypeKey.cs | 3 + src/Pure.DI.Core/Core/Types.cs | 19 +++ src/Pure.DI.Core/Generator.cs | 5 +- src/Pure.DI.MS/Pure.DI.MS.csproj | 2 +- .../.template.config/template.json | 4 +- .../.template.config/template.json | 4 +- .../Pure.DI.IntegrationTests.csproj | 2 +- tests/Pure.DI.Tests/Pure.DI.Tests.csproj | 2 +- .../Pure.DI.UsageTests.csproj | 4 +- 92 files changed, 484 insertions(+), 372 deletions(-) delete mode 100644 src/Pure.DI.Core/Core/AsyncDisposableSettings.cs create mode 100644 src/Pure.DI.Core/Core/Code/ILocks.cs create mode 100644 src/Pure.DI.Core/Core/Code/Locks.cs delete mode 100644 src/Pure.DI.Core/Core/IAsyncDisposableSettings.cs create mode 100644 src/Pure.DI.Core/Core/ITypes.cs create mode 100644 src/Pure.DI.Core/Core/SpecialType.cs create mode 100644 src/Pure.DI.Core/Core/SpecialTypeKey.cs create mode 100644 src/Pure.DI.Core/Core/Types.cs diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 384eef5e..9f89f533 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -13,7 +13,7 @@ jobs: - name: Setup dotnet uses: actions/setup-dotnet@v3 with: - dotnet-version: '8.0.x' + dotnet-version: '9.0.x' - name: Build and check run: dotnet run --project ./build -- check diff --git a/Directory.Build.props b/Directory.Build.props index 50285773..bbbf9cc4 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -4,7 +4,7 @@ 2.1.39 Pure.DI latest - net8.0 + net9.0 enable enable true diff --git a/README.md b/README.md index a91a88cc..3dd722cd 100644 --- a/README.md +++ b/README.md @@ -107,7 +107,7 @@ The above code specifies the generation of a partial class named *__Composition_ ```c# partial class Composition { - private object _lock = new object(); + private Lock _lock = new Lock()(); private Random? _random; public Program Root @@ -115,9 +115,9 @@ partial class Composition get { var stateFunc = new Func(() => { - if (_random == null) - lock (_lock) - if (_random == null) + if (_random is null) + using (_lockM11D21di.EnterScope()) + if (_random is null) _random = new Random(); return (State)_random.Next(2) @@ -1001,16 +1001,16 @@ Articles Array - + - - - - - - - - + + + + + + + +
Method Mean ErrorStdDevRatioRatioSDGen0Gen1AllocatedAlloc Ratio
Method Mean ErrorStdDevMedianRatioRatioSDGen0Gen1AllocatedAlloc Ratio
'Hand Coded'168.1 ns1.72 ns1.44 ns1.000.000.0336-632 B1.00
'Pure.DI composition root'169.6 ns3.08 ns2.58 ns1.010.020.0336-632 B1.00
'Pure.DI Resolve<T>()'170.7 ns3.02 ns2.52 ns1.020.020.0336-632 B1.00
'Pure.DI Resolve(Type)'171.2 ns2.44 ns2.04 ns1.020.020.0336-632 B1.00
LightInject179.8 ns3.43 ns3.21 ns1.070.020.0336-632 B1.00
DryIoc200.5 ns3.89 ns4.16 ns1.200.020.0336-632 B1.00
Unity9,966.0 ns80.16 ns62.58 ns59.260.420.7629-14520 B22.97
Autofac27,170.5 ns308.31 ns257.45 ns161.601.871.52590.061028816 B45.59
'Pure.DI composition root'88.33 ns1.075 ns1.006 ns88.58 ns0.940.030.0377-632 B1.00
'Pure.DI Resolve<T>()'91.94 ns1.832 ns2.445 ns91.66 ns0.980.040.0377-632 B1.00
'Pure.DI Resolve(Type)'92.65 ns2.244 ns6.366 ns89.18 ns0.980.070.0377-632 B1.00
'Hand Coded'94.24 ns1.883 ns2.578 ns94.06 ns1.000.040.0377-632 B1.00
DryIoc99.10 ns0.999 ns0.834 ns98.91 ns1.050.030.0377-632 B1.00
LightInject103.24 ns2.119 ns4.183 ns102.65 ns1.100.050.0377-632 B1.00
Unity4,510.09 ns74.628 ns66.155 ns4,488.31 ns47.891.470.86210.007614520 B22.97
Autofac15,134.66 ns110.608 ns86.355 ns15,131.24 ns160.724.451.70900.061028976 B45.85
[Array details](readme/ArrayDetails.md) @@ -1021,17 +1021,17 @@ Articles Enum - + - - - - - - - - - + + + + + + + + +
Method Mean ErrorStdDevRatioRatioSDGen0Gen1AllocatedAlloc Ratio
Method Mean ErrorStdDevMedianRatioRatioSDGen0Gen1AllocatedAlloc Ratio
'Pure.DI composition root'128.2 ns1.50 ns1.25 ns0.910.010.0184-344 B1.00
'Pure.DI Resolve(Type)'130.5 ns2.22 ns1.85 ns0.930.020.0184-344 B1.00
'Pure.DI Resolve<T>()'133.8 ns2.38 ns1.99 ns0.950.010.0184-344 B1.00
'Hand Coded'140.3 ns1.69 ns1.41 ns1.000.000.0184-344 B1.00
'Microsoft DI'181.3 ns2.01 ns1.67 ns1.290.020.0250-472 B1.37
LightInject277.7 ns3.47 ns2.89 ns1.980.030.0458-856 B2.49
DryIoc281.7 ns5.33 ns4.45 ns2.010.030.0458-856 B2.49
Unity7,768.1 ns124.10 ns152.41 ns55.431.240.7324-13752 B39.98
Autofac26,954.8 ns529.73 ns543.99 ns192.634.871.52590.061028944 B84.14
'Pure.DI composition root'64.36 ns1.282 ns2.919 ns62.75 ns1.000.050.0205-344 B1.00
'Hand Coded'64.49 ns0.791 ns0.661 ns64.47 ns1.000.010.0205-344 B1.00
'Pure.DI Resolve<T>()'65.26 ns0.965 ns0.903 ns65.56 ns1.010.020.0205-344 B1.00
'Pure.DI Resolve(Type)'65.43 ns1.004 ns0.784 ns65.23 ns1.010.020.0205-344 B1.00
'Microsoft DI'93.40 ns1.322 ns1.172 ns92.96 ns1.450.020.0281-472 B1.37
LightInject147.58 ns1.650 ns1.544 ns147.96 ns2.290.030.0510-856 B2.49
DryIoc147.61 ns1.110 ns0.984 ns147.54 ns2.290.030.0510-856 B2.49
Unity3,736.48 ns73.272 ns68.539 ns3,739.88 ns57.941.180.82020.007613752 B39.98
Autofac15,610.79 ns288.580 ns595.967 ns15,436.50 ns242.089.471.73950.061029104 B84.60
[Enum details](readme/EnumDetails.md) @@ -1042,16 +1042,16 @@ Articles Func - + - - - - - - - - + + + + + + + +
Method Mean ErrorStdDevRatioRatioSDGen0AllocatedAlloc Ratio
Method Mean ErrorStdDevRatioRatioSDGen0Gen1AllocatedAlloc Ratio
'Pure.DI composition root'8.002 ns0.1496 ns0.1326 ns0.850.010.001324 B1.00
'Hand Coded'9.445 ns0.1727 ns0.1615 ns1.000.000.001324 B1.00
'Pure.DI Resolve<T>()'10.327 ns0.2801 ns0.2620 ns1.090.020.001324 B1.00
'Pure.DI Resolve(Type)'11.954 ns0.2458 ns0.2053 ns1.260.030.001324 B1.00
DryIoc62.785 ns1.1086 ns0.9828 ns6.650.190.0063120 B5.00
LightInject296.638 ns3.9363 ns3.2870 ns31.370.800.0267504 B21.00
Unity4,448.473 ns35.3598 ns33.0756 ns471.119.330.12972552 B106.33
Autofac10,857.806 ns115.9508 ns96.8241 ns1,147.9522.400.747714008 B583.67
'Pure.DI composition root'4.382 ns0.0561 ns0.0525 ns0.840.010.0014-24 B1.00
'Hand Coded'5.193 ns0.0649 ns0.0542 ns1.000.010.0014-24 B1.00
'Pure.DI Resolve<T>()'5.914 ns0.0753 ns0.0667 ns1.140.020.0014-24 B1.00
'Pure.DI Resolve(Type)'6.375 ns0.0465 ns0.0388 ns1.230.010.0014-24 B1.00
DryIoc28.979 ns0.3875 ns0.3435 ns5.580.080.0072-120 B5.00
LightInject155.185 ns2.9628 ns2.7714 ns29.890.600.0300-504 B21.00
Unity1,760.400 ns12.4774 ns11.6714 ns339.033.990.1507-2552 B106.33
Autofac5,745.474 ns36.1642 ns30.1988 ns1,106.5112.280.83160.007614008 B583.67
[Func details](readme/FuncDetails.md) @@ -1062,20 +1062,20 @@ Articles Singleton - + - - - - - - - - - - - - + + + + + + + + + + + +
Method Mean Error StdDev RatioRatioSDGen0Gen1AllocatedAlloc Ratio
Method Mean Error StdDevRatioRatioSDGen0Gen1AllocatedAlloc Ratio
'Hand Coded'7.600 ns0.1685 ns0.1316 ns1.000.000.0013-24 B1.00
'Pure.DI composition root'8.652 ns0.2436 ns0.2392 ns1.140.030.0013-24 B1.00
'Pure.DI Resolve<T>()'9.919 ns0.2579 ns0.2154 ns1.300.040.0013-24 B1.00
'Pure.DI Resolve(Type)'11.393 ns0.2199 ns0.1836 ns1.500.040.0013-24 B1.00
DryIoc27.581 ns0.3758 ns0.3138 ns3.630.070.0013-24 B1.00
'Simple Injector'33.807 ns0.3441 ns0.3050 ns4.450.100.0013-24 B1.00
'Microsoft DI'38.478 ns0.3053 ns0.2549 ns5.060.070.0013-24 B1.00
LightInject865.775 ns1.2567 ns1.0494 ns113.961.960.0010-24 B1.00
Unity7,677.459 ns99.8336 ns88.4999 ns1,011.6717.400.1678-3184 B132.67
Autofac18,825.341 ns315.1815 ns263.1908 ns2,478.0062.671.28170.030524208 B1,008.67
'Castle Windsor'31,318.225 ns408.7010 ns362.3028 ns4,123.5674.241.2207-23912 B996.33
Ninject116,099.082 ns2,240.6371 ns2,991.1861 ns15,609.54360.853.90630.976674096 B3,087.33
'Hand Coded'3.016 ns0.0721 ns0.0602 ns1.000.030.0014-24 B1.00
'Pure.DI composition root'3.337 ns0.0778 ns0.0728 ns1.110.030.0014-24 B1.00
'Pure.DI Resolve<T>()'3.517 ns0.0602 ns0.0533 ns1.170.030.0014-24 B1.00
'Pure.DI Resolve(Type)'4.649 ns0.1417 ns0.3832 ns1.540.130.0014-24 B1.00
DryIoc11.388 ns0.0389 ns0.0304 ns3.780.070.0014-24 B1.00
'Simple Injector'16.630 ns0.0782 ns0.0693 ns5.520.110.0014-24 B1.00
'Microsoft DI'19.551 ns0.0684 ns0.0606 ns6.490.120.0014-24 B1.00
LightInject426.861 ns1.0945 ns0.9702 ns141.592.700.0014-24 B1.00
Unity2,645.530 ns47.7345 ns66.9171 ns877.5127.400.1869-3184 B132.67
Autofac9,723.452 ns60.9124 ns53.9972 ns3,225.2463.411.43430.045824208 B1,008.67
'Castle Windsor'16,991.331 ns93.5701 ns87.5255 ns5,635.97110.231.4038-23912 B996.33
Ninject67,297.995 ns1,045.7465 ns927.0271 ns22,322.54516.334.27251.098673176 B3,049.00
[Singleton details](readme/SingletonDetails.md) @@ -1088,18 +1088,18 @@ Articles - - - - - - - - - - - - + + + + + + + + + + + +
Method Mean Error StdDev RatioRatioSDGen0Gen1AllocatedAlloc Ratio
'Pure.DI composition root'7.903 ns0.1254 ns0.0979 ns0.970.020.0013-24 B1.00
'Hand Coded'8.135 ns0.1484 ns0.1158 ns1.000.000.0013-24 B1.00
'Pure.DI Resolve<T>()'10.337 ns0.2882 ns0.3848 ns1.280.040.0013-24 B1.00
'Pure.DI Resolve(Type)'11.442 ns0.2251 ns0.1995 ns1.410.030.0013-24 B1.00
LightInject20.096 ns0.0607 ns0.0507 ns2.470.040.0013-24 B1.00
'Microsoft DI'25.671 ns0.1087 ns0.0849 ns3.160.050.0013-24 B1.00
DryIoc27.474 ns0.5736 ns0.4478 ns3.380.070.0013-24 B1.00
'Simple Injector'34.814 ns0.2384 ns0.2114 ns4.280.050.0013-24 B1.00
Unity11,090.606 ns65.9897 ns61.7268 ns1,366.0819.490.2747-5176 B215.67
Autofac28,646.903 ns355.7947 ns315.4027 ns3,520.2461.911.77000.091633224 B1,384.33
'Castle Windsor'58,534.573 ns988.3103 ns876.1114 ns7,196.49162.412.8687-54360 B2,265.00
Ninject253,820.129 ns4,777.4163 ns5,310.0855 ns31,080.38673.816.83591.4648131008 B5,458.67
'Pure.DI composition root'3.491 ns0.1203 ns0.2764 ns0.970.090.0014-24 B1.00
'Hand Coded'3.619 ns0.1132 ns0.1952 ns1.000.070.0014-24 B1.00
'Pure.DI Resolve<T>()'4.417 ns0.1377 ns0.3429 ns1.220.110.0014-24 B1.00
'Pure.DI Resolve(Type)'4.649 ns0.1431 ns0.1338 ns1.290.080.0014-24 B1.00
LightInject7.043 ns0.0902 ns0.0754 ns1.950.100.0014-24 B1.00
'Microsoft DI'10.357 ns0.1357 ns0.1133 ns2.870.150.0014-24 B1.00
DryIoc10.673 ns0.1197 ns0.1119 ns2.960.160.0014-24 B1.00
'Simple Injector'14.594 ns0.1373 ns0.1217 ns4.040.210.0014-24 B1.00
Unity4,320.577 ns85.0619 ns175.6675 ns1,197.1378.580.3052-5176 B215.67
Autofac12,616.111 ns227.4971 ns201.6703 ns3,495.62189.021.98360.091633224 B1,384.33
'Castle Windsor'27,766.858 ns312.7020 ns277.2021 ns7,693.52405.543.23490.030554360 B2,265.00
Ninject148,286.554 ns3,309.2559 ns9,600.7521 ns41,086.583,398.597.56841.4648128736 B5,364.00
[Transient details](readme/TransientDetails.md) @@ -1110,11 +1110,11 @@ Articles Benchmarks environment

-BenchmarkDotNet v0.13.12, Ubuntu 20.04.6 LTS (Focal Fossa)
-Intel Xeon Platinum 8259CL CPU 2.50GHz, 1 CPU, 2 logical cores and 1 physical core
-.NET SDK 8.0.201
-  [Host]     : .NET 8.0.2 (8.0.224.6711), X64 RyuJIT AVX-512F+CD+BW+DQ+VL
-  DefaultJob : .NET 8.0.2 (8.0.224.6711), X64 RyuJIT AVX-512F+CD+BW+DQ+VL
+BenchmarkDotNet v0.14.0, Windows 10 (10.0.19045.4894/22H2/2022Update)
+AMD Ryzen 9 5900X, 1 CPU, 24 logical and 12 physical cores
+.NET SDK 9.0.100
+  [Host]     : .NET 9.0.0 (9.0.24.52809), X64 RyuJIT AVX2
+  DefaultJob : .NET 9.0.0 (9.0.24.52809), X64 RyuJIT AVX2
 
diff --git a/benchmarks/Pure.DI.Benchmarks/Pure.DI.Benchmarks.csproj b/benchmarks/Pure.DI.Benchmarks/Pure.DI.Benchmarks.csproj index 6db26662..b34c2cd0 100644 --- a/benchmarks/Pure.DI.Benchmarks/Pure.DI.Benchmarks.csproj +++ b/benchmarks/Pure.DI.Benchmarks/Pure.DI.Benchmarks.csproj @@ -17,14 +17,14 @@ - + - + diff --git a/benchmarks/data/Pure.DI.Benchmarks.Benchmarks.Array-report.html b/benchmarks/data/Pure.DI.Benchmarks.Benchmarks.Array-report.html index 4c103345..aea9958e 100644 --- a/benchmarks/data/Pure.DI.Benchmarks.Benchmarks.Array-report.html +++ b/benchmarks/data/Pure.DI.Benchmarks.Benchmarks.Array-report.html @@ -2,7 +2,7 @@ -Pure.DI.Benchmarks.Benchmarks.Array-20240420-074640 +Pure.DI.Benchmarks.Benchmarks.Array-20241121-115600