Skip to content
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

Merged
merged 1 commit into from
Nov 18, 2020

Conversation

eerhardt
Copy link
Member

@eerhardt eerhardt commented Nov 18, 2020

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:

image

    public class DIBench
    {
        private readonly ServiceCollection services = new ServiceCollection();

        public DIBench()
        {
            services.AddTransient<Service1>();
            services.AddTransient<Service2a>();
            services.AddTransient<Service2b>();
            services.AddTransient<Service3a>();
            services.AddTransient<Service4>();
            services.AddTransient<Service4>();
            services.AddTransient<Service5>();
            services.AddTransient<Service6>();
            services.AddTransient<Service7>();
            services.AddTransient<Service8>();
            services.AddTransient<Service9>();
            services.AddTransient<Service10>();
            services.AddTransient<Service11>();
            services.AddTransient<Service12>();
            services.AddTransient<Service13>();
            services.AddTransient<Service14>();
            services.AddTransient<Service15>();
            services.AddTransient<Service16>();
            services.AddTransient<Service17>();
            services.AddTransient<Service18>();
            services.AddTransient<Service19>();
            services.AddTransient<Service20>();
            services.AddTransient<Service21>();
            services.AddTransient<Service22>();
            services.AddTransient<Service23>();
            services.AddTransient<Service24>();
            services.AddTransient<Service25>();
            services.AddTransient<Service26>();
            services.AddTransient<Service27>();
            services.AddTransient<Service28>();
            services.AddTransient<Service29>();
        }

        [Benchmark]
        public object BuildServiceProvider()
        {
            var sp = services.BuildServiceProvider();

            return sp;
        }
    }

I also made a slight optimization to CallSiteFactory to use ToArray instead of ToList.
@Dotnet-GitSync-Bot
Copy link
Collaborator

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.

@ghost
Copy link

ghost commented Nov 18, 2020

Tagging subscribers to this area: @eerhardt, @maryamariyan
See info in area-owners.md if you want to be subscribed.

Issue Details
Description:

I also made a slight optimization to CallSiteFactory to use ToArray instead of ToList.

Author: eerhardt
Assignees: -
Labels:

area-Extensions-DependencyInjection

Milestone: -

@davidfowl
Copy link
Member

Nice clean up. We should do this in ASP.NET too...

@eerhardt
Copy link
Member Author

We should do this in ASP.NET too...

Will log an issue (and maybe make a PR if it looks quick).

Copy link
Member

@stephentoub stephentoub left a 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.)

@stephentoub stephentoub merged commit 0fad9d6 into dotnet:master Nov 18, 2020
@@ -24,27 +24,27 @@ internal class CallSiteFactory
public CallSiteFactory(IEnumerable<ServiceDescriptor> descriptors)
{
_stackGuard = new StackGuard();
_descriptors = descriptors.ToList();
_descriptors = descriptors.ToArray();
Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member

Choose a reason for hiding this comment

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

Cool

@eerhardt eerhardt deleted the RemoveGetTypeInfo branch November 18, 2020 23:48
@ghost ghost locked as resolved and limited conversation to collaborators Dec 19, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants