Skip to content
This repository has been archived by the owner on Dec 18, 2018. It is now read-only.

Use ActivatorUtlities.CreateFactory instead of CreateInstance #1643

Merged
merged 3 commits into from
Mar 19, 2018

Conversation

davidfowl
Copy link
Member

@davidfowl davidfowl commented Mar 19, 2018

  • Turns out CreateFactory is much much faster
  • Added a benchmark for hub activation

Before

 Method |     Mean |    Error |   StdDev |        Op/s |  Gen 0 | Allocated |
------- |---------:|---------:|---------:|------------:|-------:|----------:|
 Create | 710.7 ns | 14.40 ns | 39.89 ns | 1,406,988.1 | 0.0153 |     224 B |

After

  Method |     Mean |    Error |   StdDev |         Op/s |  Gen 0 | Allocated |
------- |---------:|---------:|---------:|-------------:|-------:|----------:|
 Create | 76.34 ns | 3.347 ns | 9.386 ns | 13,099,035.7 | 0.0037 |      48 B |

Lazy

 Method |     Mean |    Error |   StdDev |   Median |         Op/s |  Gen 0 | Allocated |
------- |---------:|---------:|---------:|---------:|-------------:|-------:|----------:|
 Create | 65.39 ns | 1.480 ns | 4.102 ns | 63.73 ns | 15,292,741.1 | 0.0037 |      48 B |

Fixes #1570

- Turns out CreateFactory is much much faster
- Added a benchmark for hub activation
@davidfowl davidfowl changed the base branch from dev to release/2.1 March 19, 2018 06:08
@@ -9,6 +9,8 @@ namespace Microsoft.AspNetCore.SignalR
{
public class DefaultHubActivator<THub> : IHubActivator<THub> where THub: Hub
{
// Object factory for all this Hub type
private static ObjectFactory _objectFactory = ActivatorUtilities.CreateFactory(typeof(THub), Type.EmptyTypes);
Copy link
Member

@JamesNK JamesNK Mar 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a pretty heavy operation to have in a static constructor. Also if the hub type is registered with the service provider then we'll never even use the factory. IMO create the factory when it is needed

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked at that, I can make it lazy.

@davidfowl
Copy link
Member Author

davidfowl commented Mar 19, 2018

🆙 📅 . Seems like it got even faster after it was made lazy 😄

@@ -9,6 +9,8 @@ namespace Microsoft.AspNetCore.SignalR
{
public class DefaultHubActivator<THub> : IHubActivator<THub> where THub: Hub
{
// Object factory for THub instances
private static Lazy<ObjectFactory> _objectFactory = new Lazy<ObjectFactory>(() => ActivatorUtilities.CreateFactory(typeof(THub), Type.EmptyTypes));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

readonly

@davidfowl davidfowl merged commit cf7c862 into release/2.1 Mar 19, 2018
@pakrym
Copy link
Contributor

pakrym commented Mar 19, 2018

Why did you need full on Lazy<T>? Null check would be just fine.

@davidfowl
Copy link
Member Author

Why did you need full on Lazy? Null check would be just fine.

You're right. I guess worst case we would have compiled a few times. Meh.

@davidfowl davidfowl deleted the davidfowl/hub-activator-perf branch March 28, 2018 06:04
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants