-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Improve perf\allocs of ActivatorUtilities.CreateInstance() #99383
Conversation
Tagging subscribers to this area: @dotnet/area-extensions-dependencyinjection Issue Details[verifying tests; reduces allocs by using new invoker]
|
src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/ActivatorUtilities.cs
Outdated
Show resolved
Hide resolved
Given the nature of the change, it'd be good to see perf test results on both .NET 9 and .NET Framework. |
src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/ActivatorUtilities.cs
Outdated
Show resolved
Hide resolved
src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/ActivatorUtilities.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left an NIT, overall LGTM.
Reduces allocs and increases CPU perf of
ActivatorUtilities.CreateInstance()
The alloc reduction is significant and depends on number of constructors and the number of constructor parameters (e.g. 4.6x reduction for 3 constructors).
The CPU case depends on whether
[ActivatorUtilitiesConstructor]
is used. If used, the perf improvements are much better:The optimizations include:
[ActivatorUtilitiesConstructor]
is used. We can now ignore most processing of other constructors.ConstructorInvoker
class added in v8; this allows use ofSpan<object>
over stack-allocated objects (if <= 4 parameters) or the ability to re-use an allocation ofobject[]
over varying number of constructor parameters.Benchmarks
.NET Framework is also slightly improved for both CPU and allocs:
.NET Framework 4.8.1 Benchmarks