Skip to content

NET 8.0 Preview 7 Performance Regression: ActivatorUtilities.CreateInstance #91186

Closed
@Jakimar

Description

@Jakimar

Description

I was checking new behavior for "ActivatorUtilities.CreateInstance" in NET 8.0 and noticed some perfomance regression when invoked for class with not empty construcor, as i can see something changed between "Preview 6" and "Preview 7", it's slighly better in last daily build "rc.2.23426.4" but still much worse than NET7.0 .

public class Tester
{
    private static IServiceProvider _spProvider;

    public Tester()
    {
        var sc = new ServiceCollection();
        sc.AddScoped<A>();
        sc.AddScoped<B>();
        sc.AddScoped<C>();
        _spProvider = sc.BuildServiceProvider();
    }
    
    public A? CreateA()
    {
        var scope = _spProvider.CreateScope();
        return ActivatorUtilities.CreateInstance<A>(scope.ServiceProvider);
    }
    
    public B? CreateB()
    {
        var scope = _spProvider.CreateScope();
        return ActivatorUtilities.CreateInstance<B>(scope.ServiceProvider);
    }
    
    public C? CreateC()
    {
        var scope = _spProvider.CreateScope();
        return ActivatorUtilities.CreateInstance<C>(scope.ServiceProvider);
    }
}

    public class A
    { }

    public class B
    {
        private readonly A _a;

        public B(A a)
        {
            _a = a;
        }
    }

    public class C
    {
        private readonly B _b;
        private readonly B _b1;
        private readonly B _b2;
        private readonly B _b3;
        private readonly B _b4;
        private readonly B _b5;
        
        public C(B b, B b1, B b2, B b3, B b4, B b5)
        {
            _b = b;
            _b1 = b1;
            _b2 = b2;
            _b3 = b3;
            _b4 = b4;
            _b5 = b5;
        }
    }

Configuration

BenchmarkDotNet v0.13.7, Windows 10 (10.0.19044.3086/21H2/November2021Update)
11th Gen Intel Core i5-11600K 3.90GHz, 1 CPU, 12 logical and 6 physical cores
.NET SDK 8.0.100-rc.2.23425.18
  [Host]                  : .NET 7.0.1 (7.0.122.56804), X64 RyuJIT AVX2
  7.0.0                   : .NET 7.0.1 (7.0.122.56804), X64 RyuJIT AVX2
  8.0.0-preview.6.23329.7 : .NET 8.0.0 (8.0.23.42311), X64 RyuJIT AVX2
  8.0.0-preview.7.23375.6 : .NET 8.0.0 (8.0.23.42311), X64 RyuJIT AVX2
  8.0.0-rc.2.23426.4      : .NET 8.0.0 (8.0.23.42311), X64 RyuJIT AVX2

Regression?

Data

Method Runtime NuGetReferences Mean Error StdDev Ratio
CreateA .NET 7.0 Microsoft.Extensions.DependencyInjection 7.0.0 122.1 ns 2.05 ns 1.92 ns baseline
CreateA .NET 8.0 Microsoft.Extensions.DependencyInjection 8.0.0-preview.6.23329.7 116.8 ns 2.33 ns 2.50 ns -4%
CreateA .NET 8.0 Microsoft.Extensions.DependencyInjection 8.0.0-preview.7.23375.6 113.6 ns 1.54 ns 1.44 ns -7%
CreateA .NET 8.0 Microsoft.Extensions.DependencyInjection 8.0.0-rc.2.23426.4 119.0 ns 2.42 ns 2.69 ns -3%
CreateB .NET 7.0 Microsoft.Extensions.DependencyInjection 7.0.0 220.3 ns 3.31 ns 3.10 ns baseline
CreateB .NET 8.0 Microsoft.Extensions.DependencyInjection 8.0.0-preview.6.23329.7 218.9 ns 3.83 ns 3.39 ns -1%
CreateB .NET 8.0 Microsoft.Extensions.DependencyInjection 8.0.0-preview.7.23375.6 480.0 ns 3.31 ns 2.76 ns +118%
CreateB .NET 8.0 Microsoft.Extensions.DependencyInjection 8.0.0-rc.2.23426.4 402.6 ns 4.53 ns 4.24 ns +83%
CreateC .NET 7.0 Microsoft.Extensions.DependencyInjection 7.0.0 522.9 ns 4.21 ns 3.73 ns baseline
CreateC .NET 8.0 Microsoft.Extensions.DependencyInjection 8.0.0-preview.6.23329.7 494.1 ns 3.49 ns 2.91 ns -5%
CreateC .NET 8.0 Microsoft.Extensions.DependencyInjection 8.0.0-preview.7.23375.6 2,489.0 ns 26.57 ns 24.85 ns +376%
CreateC .NET 8.0 Microsoft.Extensions.DependencyInjection 8.0.0-rc.2.23426.4 1,604.6 ns 26.34 ns 23.35 ns +207%

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions