Improve performance of ActivatorUtilities.CreateInstance
See original GitHub issueCurrently, it’s 30x slower than invoking factory delegate.
While it could never reach the performance of invoking the factory delegate there is a lot of possible improvements (especially around allocations) in ActivatorUtilities.CreateInstance
Issue Analytics
- State:
- Created 6 years ago
- Reactions:2
- Comments:6
Top Results From Across the Web
.net - Does System.Activator.CreateInstance(T) have ...
Yes, the above timings might indicate that Activator. CreateInstance has more overhead than a dynamically built delegate, but there might be ...
Read more >Activator utilities: activate anything! - On The Drift
ActivatorUtilities allows you to specify constructor arguments manually, lets take our previous controller and create an instance with an explicit transient ...
Read more >Benchmarking 4 reflection methods for calling a constructor ...
In this post I compare several different ways to call a constructor using reflection, and benchmark them to see which is fastest.
Read more >ActivatorUtilities.CreateInstance behaves consistently - .NET
NET extensions where ActivatorUtilities.CreateInstance behaves consistently regardless of the order of constructor overloads.
Read more >NET Core ServiceProvider – ActivatorUtilities – ObjectFactory
Leverage the ActivatorUtilities to instantiate non-service objects ... The CreateInstance method of the ActivatorUtilities.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@pakrym Here is a snapshot of the allocated memory when allocating 10000 objects with
ActivatorUtilities.CreateInstance()
(with the official “2.2.0” DependencyInjection package). I have crossed out the allocations optimized with the last PR. It remains:object[]
allocated in theConstructorMatcher
constructor. We may imagine reuse the array during the matching loop. But this optimization would be valid only if there is more than two constructors, and if the constructors have same number of parameters (ConstructorInfo.Invoke()
requests a parameter array of the exact length). So it seems very restrictive.But there is maybe a more generic but trickier optimization here. The method
CheckArguments
is calledRuntimeConstructorInfo.Invoke()
(see the orange rectangle). A copy of the parameters is done because they may be changed byCheckValue()
. ButCheckValue()
seems to returns the object as-is in the normal case. So we could imagine a lazy allocation of thecopyOfParameters
array only if the parameter is changed byCheckValue()
. The advantage is that any object construction (and maybe other reflection calls) could benefit of this optimization. But the optimization seems too big to be correct. I suppose that something is wrong in my analysis 😃.TIL that it’s the person who merges the PR that gets credit for closing linked issues ;P.
Sounds good, thanks for filing that issue @Yves57 !