Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance Improvement - IsGenericTypeDefinedBy #1038

Merged
merged 1 commit into from
Nov 3, 2019

Conversation

alistairjevans
Copy link
Member

I've changed the IsGenericTypeDefinedByCache dictionary used by TypeExtensions.IsGenericTypeDefinedBy to use ValueTuple as the key, rather than Tuple.

It's used quite a lot, and this change reduces allocations to zero and generally improves performance of the method. It's called quite a lot, so hopefully this will help a little with a few of the generic IoC paths.

BenchmarkDotNet=v0.11.5, OS=Windows 10.0.18362
AMD Ryzen 5 2500U with Radeon Vega Mobile Gfx, 1 CPU, 8 logical and 4 physical cores
.NET Core SDK=3.0.100
  [Host]     : .NET Core 3.0.0 (CoreCLR 4.700.19.46205, CoreFX 4.700.19.46214), 64bit RyuJIT
  DefaultJob : .NET Core 3.0.0 (CoreCLR 4.700.19.46205, CoreFX 4.700.19.46214), 64bit RyuJIT


|                    Method | DictSize |       Mean |    Error |    StdDev |     Median |  Gen 0 | Gen 1 | Gen 2 | Allocated |
|-------------------------- |--------- |-----------:|---------:|----------:|-----------:|-------:|------:|------:|----------:|
|    IsGenericTypeDefinedBy |      100 | 1,583.9 ns | 24.77 ns |  23.17 ns | 1,582.2 ns | 0.2441 |     - |     - |     512 B |
| IsGenericTypeDefinedByNew |      100 | 1,049.9 ns | 32.82 ns |  96.26 ns |   991.5 ns |      - |     - |     - |         - |
|    IsGenericTypeDefinedBy |      500 | 1,844.4 ns | 63.29 ns | 186.61 ns | 1,951.4 ns | 0.2441 |     - |     - |     512 B |
| IsGenericTypeDefinedByNew |      500 | 1,072.7 ns | 31.63 ns |  93.26 ns | 1,021.2 ns |      - |     - |     - |         - |
|    IsGenericTypeDefinedBy |     1000 | 1,803.0 ns | 66.45 ns | 195.94 ns | 1,682.7 ns | 0.2441 |     - |     - |     512 B |
| IsGenericTypeDefinedByNew |     1000 | 1,039.4 ns | 30.58 ns |  90.16 ns |   992.4 ns |      - |     - |     - |         - |
|    IsGenericTypeDefinedBy |     2000 | 1,722.9 ns | 35.98 ns | 106.10 ns | 1,660.6 ns | 0.2441 |     - |     - |     512 B |
| IsGenericTypeDefinedByNew |     2000 |   997.5 ns | 19.91 ns |  52.10 ns |   965.5 ns |      - |     - |     - |         - |
|    IsGenericTypeDefinedBy |     5000 | 1,734.9 ns | 34.75 ns | 102.45 ns | 1,666.5 ns | 0.2441 |     - |     - |     512 B |
| IsGenericTypeDefinedByNew |     5000 |   932.6 ns | 16.49 ns |  12.88 ns |   930.7 ns |      - |     - |     - |         - |
|    IsGenericTypeDefinedBy |    10000 | 1,724.1 ns | 39.21 ns | 113.75 ns | 1,671.5 ns | 0.2441 |     - |     - |     512 B |
| IsGenericTypeDefinedByNew |    10000 | 1,034.5 ns | 36.10 ns | 106.43 ns |   961.6 ns |      - |     - |     - |         - |

I've not added it to the formal benchmark project because it's a bit niche, but I've put the benchmark in a gist if you want to check it:
https://gist.github.com/alistairjevans/ab67db8be96cedc2d294118e09ed1714

@alexmg alexmg merged commit 5364c97 into autofac:develop Nov 3, 2019
@alexmg
Copy link
Member

alexmg commented Nov 3, 2019

Thanks @alistairjevans. These small improvements add up over time! 😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants