question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Improve performance of ActivatorUtilities.CreateInstance

See original GitHub issue

Currently, 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:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:6

github_iconTop GitHub Comments

1reaction
Yves57commented, Jun 7, 2019

@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:

  • ConstructorInfo[] -> I don’t see any possible optimization. But I may be wrong.
  • ParameterInfo[] -> I don’t see any possible optimization. But I may be wrong.
  • The object[] allocated in the ConstructorMatcher 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 called RuntimeConstructorInfo.Invoke() (see the orange rectangle). A copy of the parameters is done because they may be changed by CheckValue(). But CheckValue() seems to returns the object as-is in the normal case. So we could imagine a lazy allocation of the copyOfParameters array only if the parameter is changed by CheckValue(). 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 😃.

image

0reactions
analogrelaycommented, Jun 7, 2019

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 !

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found