Dependency Injection performance
Hi there, I'm trying to be open minded and refactor my current Api-structure to a service based structured based on Dependency Injection. When doing this I noticed something when running my tests.
The simplest way for me to test while converting was to pass in the IServiceProvider into the constructor of the Api and resolve the services there, thus letting the tests running exactly in the same way as they did before. The old constructor looked like this:
Archives = new Repositories.ArchiveRepository(this, db);
Categories = new Services.CategoryService(db, cacheLevel > 2 ? cache : null);
Media = new Repositories.MediaRepository(this, db, storage, cacheLevel > 2 ? cache : null, imageProcessor);
Pages = new Repositories.PageRepository(this, db, services, cacheLevel > 2 ? cache : null);
PageTypes = new Services.PageTypeService(db, cacheLevel > 1 ? cache : null);
Params = new Services.ParamService(db, cacheLevel > 0 ? cache : null);
Posts = new Repositories.PostRepository(this, db, services, cacheLevel > 2 ? cache : null);
PostTypes = new Services.PostTypeService(db, cacheLevel > 1 ? cache : null);
Sites = new Services.SiteService(db, PageTypes, cacheLevel > 0 ? cache : null);
Tags = new Services.TagService(db, cacheLevel > 2 ? cache : null);
Aliases = new Services.AliasService(db, Sites, cacheLevel > 2 ? cache : null);
Here we have seven services being created by calling their constructor. When running the tests, which includes 466 tests creating the Api object I get the following results:
I then changed the code to resolve the seven services like so (I've removed the unchanged code for clarity):
Aliases = services.GetService<IAliasService>();
Archives = ...
Categories = services.GetService<ICategoryService>();
Media = ...
Pages = ...
PageTypes = services.GetService<IPageTypeService>();
Params = services.GetService<IParamService>();
Posts = ...
PostTypes = services.GetService<IPostTypeService>();
Sites = services.GetService<ISiteService>();
Tags = services.GetService<ITagService>();
When running the exact same tests again I get the following results:
As you can see the time to construct the Api these 466 times has increased with one minute. Now I love dependency injection as much as the next guy (or girl), but the cost is a bit high in terms of performance.
Please note that both versions of the code actually include adding the services and building the service provider just to make sure that the only difference is the constructor code.
Regards
And to clarify the project is built using NetStandard 2.0 and I'm using the .NET CLI 2.1.300-preview1-008174.
Yes. But resolving in the constructor is going to have an overhead of sorts.
Please elaborate! I know that in the real life scenario when everything is refactored all of the services will be passed into the constructor by the DI, but it would be interesting to see how this could effect the performance.
Either they are resolved right before calling the constructor, or within the constructor (as in the test case). Is there some magic going on that will speed it up significant if they are passed in as parameters?
Depends what you mean by performance? Also careful using performance in your tests as a real life indicator. I would benchmark the perf through a console app to understand the difference. Also passing around the IServiceProvider is generally frowned upon. Putting it into perspective as part of a single web request, DI resolution would make up a fraction of the request time.
Yes, the ServiceProvider is only passed in to avoid having to rewrite all 466 test while refactoring :) And regarding the performance implications in a real scenario I understand your point. In a basic application with a couple of middleware and a controller the services would be resolved maybe 4-8 times per request.
Still I was surprised of the difference.
Im sure there are faster containers out there. But design comes into play.
What version of ASP.NET Core are you using? By injecting IServiceProvider and dynamically resolving the services you need, you're preventing the container from doing performance optimizations by compiling code (using expressions or IL emit in 2.1) for resolving the type.
I also think there's a condition (by default) where it won't compile code unless you resolve the type more than 2-3 times, so it would be slow for the first few calls and faster after that.
In 2.1, a ServiceProviderMode was introduced to be able to manually configure this behavior.
That’s really interesting @khellang! Maybe the numbers will be different when the entire code base is refactored. I’ll test more
@tidyui can you provide a repro?
I'm sorry @davidfowl, I ended up throwing away the branch since I couldn't get to the bottom with it and I'm kind of obsessed with performance 😂. I could probably create a repo with the same behavior though to see if it persists in the latest version of .NET Core if it would be interesting.
No problem! We expect the above to be slower, I was just curious on the exact details. We do have our own performance tests but the scale of yours might be different.
Thanks again
Well I created a simple setup of the same scenario for fun and ran a test on simply instantiating the api by constructor 10 000 times and then doing the same but resolving the services. Yes, resolving the services was 9 times slower BUT it only increased from 2 ms to 24 ms so WHO CARES :)
$ dotnet run
Creating 10000 instances of IApi by Constructor and accessing a service.
Total time: 2 ms
Resolving 10000 instances of IApi with ServiceProvider and accessing a service.
Total time: 24 ms
Resolving the services was 9,30311466613377 times slower
There must have been some other hiccup with the old project caused by how the tests were executed by XUnit.
Cheers