Skip to content

Instantly share code, notes, and snippets.

@kant2002
Last active June 13, 2022 12:59
Show Gist options
  • Save kant2002/8aed21b079dbeffdef94a5c8e9a27ddc to your computer and use it in GitHub Desktop.
Save kant2002/8aed21b079dbeffdef94a5c8e9a27ddc to your computer and use it in GitHub Desktop.
NativeAOT and DI
namespace InternalNamespace.Nested;
using Microsoft.Extensions.DependencyInjection;
class DependentTestService {}
class TestService
{
public TestService(DependentTestService dependency) {}
}
class Test
{
void Method()
{
var services = new ServiceCollection();
services.AddScoped<DependentTestService>();
services.AddScoped<TestService>();
}
}
internal static class ServicesReplacementExtensions
{
public static object Build_global_InternalNamespace_Nested_DependentTestService(IServiceProvider serviceProvider)
{
return new global::InternalNamespace.Nested.TestService();
}
public static object Build_global_InternalNamespace_Nested_TestService(IServiceProvider serviceProvider)
{
var param_0 = (global::DependentTestService)serviceProvider.GetService(typeof(global::DependentTestService));
return new global::TestService(param_0);
}
}
namespace InternalNamespace.Nested
{
internal static partial class global_InternalNamespace_Nested_DependentTestService_ServiceExtensions
{
public static IServiceCollection AddScoped<TService>(this IServiceCollection services) where TService : global::InternalNamespace.Nested.DependentTestService
{
return services.AddScoped(typeof(global::InternalNamespace.Nested.DependentTestService), ServicesReplacementExtensions.Build_global_InternalNamespace_Nested_DependentTestService);
}
}
}
namespace InternalNamespace.Nested
{
internal static partial class global_InternalNamespace_Nested_TestService_ServiceExtensions
{
public static IServiceCollection AddScoped<TService>(this IServiceCollection services) where TService : global::InternalNamespace.Nested.TestService
{
return services.AddScoped(typeof(global::InternalNamespace.Nested.TestService), ServicesReplacementExtensions.Build_global_InternalNamespace_Nested_TestService);
}
}
}
// * Summary *

BenchmarkDotNet=v0.13.1, OS=Windows 10.0.19044.1706 (21H2)
Intel Core i7-9700F CPU 3.00GHz, 1 CPU, 8 logical and 8 physical cores
.NET SDK=7.0.100-preview.5.22267.11
  [Host]     : .NET 6.0.5 (6.0.522.21309), X64 RyuJIT
  DefaultJob : .NET 6.0.5 (6.0.522.21309), X64 RyuJIT

Service retrieval

Method Mean Error StdDev Ratio Gen 0 Allocated
MEDI 29.84 ns 0.139 ns 0.123 ns 2.55 0.0038 24 B
MEDI_Augmented 22.125 ns 0.0600 ns 0.0561 ns 1.93 - -
MEDI_Aot 8.791 ns 0.0689 ns 0.0611 ns 0.77 - -
Jab 11.445 ns 0.0456 ns 0.0404 ns 1.00 0.0038 24 B
PureDI 2.308 ns 0.0516 ns 0.0483 ns 0.20 0.0038 24 B

Container setup time

Method Mean Error StdDev Ratio RatioSD Gen 0 Gen 1 Allocated
MEDI 13,654.31 ns 2,255.992 ns 6,651.842 ns 271.23 34.41 0.7610 0.1888 4,776 B
MEDI_Augmented 15,027.30 ns 2,262.808 ns 6,671.941 ns 347.57 61.52 0.7381 0.1831 6,688 B
MEDI_Aot 53.954 ns 0.4342 ns 0.4062 ns 4.01 0.06 0.0510 - 320 B
Jab 13.437 ns 0.1587 ns 0.1406 ns 1.00 0.00 0.0089 - 56 B
PureDI 1.718 ns 0.0273 ns 0.0242 ns 0.13 0.00 0.0038 - 24 B
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment