-
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
Remove unnecessary GetTypeInfo from Microsoft.Extensions. #44891
Conversation
I also made a slight optimization to CallSiteFactory to use ToArray instead of ToList.
I couldn't figure out the best area label to add to this PR. If you have write-permissions please help me learn by adding exactly one area label. |
Tagging subscribers to this area: @eerhardt, @maryamariyan Issue Details
|
Nice clean up. We should do this in ASP.NET too... |
Will log an issue (and maybe make a PR if it looks quick). |
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.
(Some of this LINQ usage pains me to look at :), but that's not related to your PR.)
@@ -24,27 +24,27 @@ internal class CallSiteFactory | |||
public CallSiteFactory(IEnumerable<ServiceDescriptor> descriptors) | |||
{ | |||
_stackGuard = new StackGuard(); | |||
_descriptors = descriptors.ToList(); | |||
_descriptors = descriptors.ToArray(); |
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.
What was the benefit of this change? Is _descriptors accessed a lot such that the overhead of going through _descriptors as a list vs an array mattered?
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.
The benefit was very minimal, probably not even observable. I started this change by looking at CallSiteFactory.Populate
(which is 75% of the above benchmark). And this ToList()
jumped out at me and looking at the code I noticed we don't need a List, but can just use an Array instead. I just figured while I was in here, it was one less object to create.
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.
I just figured while I was in here, it was one less object to create.
But unless the array is the exact right size, ToArray needs to allocate one at the end and copy all the elements to it. ToList doesn't. So you still generally end up with another object, but typically a bigger one.
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.
In this case, the Type that is passed in to descriptors
is always an IServiceCollection
, which implements ICollection<T>
. So the size will be known up-front and only a single Array needs to be allocated. It won't need to be resized in the end.
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.
Cool
I also made a slight optimization to CallSiteFactory to use ToArray instead of ToList.
These were showing up as 5% of
ServiceCollection.BuildServiceProvider()
in a simple benchmark: