Skip to content

Conversation

@stephentoub
Copy link
Member

@stephentoub stephentoub commented Nov 24, 2020

  • Avoid an extra GetGenericArguments() call for all arities.
  • Special-case a Type[] with just one type. In looking at all calls to MakeGenericType when starting up a basic ASP.NET MVC app, 70% of the few hundred were for a single generic argument (the rest were for two).
Method Toolchain Mean Ratio Allocated
OneRefType \master\corerun.exe 255.3 ns 1.00 128 B
OneRefType \pr\corerun.exe 156.6 ns 0.61 32 B
OneValueType \master\corerun.exe 247.8 ns 1.00 128 B
OneValueType \pr\corerun.exe 161.0 ns 0.65 32 B
TwoRefType \master\corerun.exe 285.2 ns 1.00 160 B
TwoRefType \pr\corerun.exe 199.8 ns 0.70 120 B
TwoValueType \master\corerun.exe 292.4 ns 1.00 160 B
TwoValueType \pr\corerun.exe 210.4 ns 0.72 120 B
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.Running;
using System;
using System.Collections.Generic;

[MemoryDiagnoser]
public class Program
{
    static void Main(string[] args) => BenchmarkSwitcher.FromAssemblies(new[] { typeof(Program).Assembly }).Run(args);

    private Type[] _oneRef = new[] { typeof(string) };
    private Type[] _oneValue = new[] { typeof(int) };
    private Type[] _twoRef = new[] { typeof(string), typeof(string) };
    private Type[] _twoValue = new[] { typeof(int), typeof(int) };

    [Benchmark] public Type OneRefType() => typeof(List<>).MakeGenericType(_oneRef);
    [Benchmark] public Type OneValueType() => typeof(List<>).MakeGenericType(_oneValue);
    [Benchmark] public Type TwoRefType() => typeof(Dictionary<,>).MakeGenericType(_twoRef);
    [Benchmark] public Type TwoValueType() => typeof(Dictionary<,>).MakeGenericType(_twoValue);
}

Contributes to #44598

- Avoid an extra GetGenericArguments() call for all arities.
- Special-case a Type[] with just one type.  In looking at all calls to MakeGenericType when starting up a basic ASP.NET MVC app, 70% were for a single generic argument (the rest were for two).
@Dotnet-GitSync-Bot
Copy link
Collaborator

I couldn't figure out the best area label to add to this PR. If you have write-permissions please help me learn by adding exactly one area label.

Copy link
Member

@jkotas jkotas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@GrabYourPitchforks
Copy link
Member

/cc @steveharter, since it follows a similar pattern that we reviewed for #45085.

@stephentoub stephentoub merged commit 664b962 into dotnet:master Nov 24, 2020
@stephentoub stephentoub deleted the makegeneric branch November 24, 2020 12:03
@ghost ghost locked as resolved and limited conversation to collaborators Dec 24, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants