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

Dispose services in reverse creation order #505

Merged
merged 2 commits into from
Mar 17, 2017
Merged

Conversation

pakrym
Copy link
Contributor

@pakrym pakrym commented Mar 15, 2017

resolvedExpression);
resolvedVariable);

Expression captureDisposableExpression = Expression.Invoke(
Copy link
Member

@davidfowl davidfowl Mar 16, 2017

Choose a reason for hiding this comment

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

Errr? Give me the summary of this fix? You're now capturing all disposable objects in the same list? It's no longer transient disposables and this point right? We originally removed this because of memory concerns.

Copy link
Member

Choose a reason for hiding this comment

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

We never tested the actual performance in terms of op/s, just memory allocations. If you look closely at your original PR you'll notice that the memory allocations measured are mostly related ServiceProvider.CreateScope()/ctor, and there are no measurements related to the actual capturing disposables of any kind.

If you're really concerned memory you could store some sort of ordering metadata with the captured services (a la Autofac) or pool the Dictionary/List like @benaadams suggested in the original PR. Both sound kinda complicated and might even hurt perf in some scenarios.

It might be a good idea to add a BenchmarkDotNet scenario that covers capturing disposables.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, we need measurements. Also ConcurrentBag did get way less allocatey in .NET Core 2.0 so maybe it's worth a remeasure.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This change would only add scoped and singletons to that list, there aren't many singletons and scope objects are short-lived so I am not super worried about this.

Copy link
Member

Choose a reason for hiding this comment

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

I still want measurements.

Copy link
Member

Choose a reason for hiding this comment

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

serviceCollection.AddSingleton<IFakeMultipleService, FakeDisposableCallbackInnerService>();
serviceCollection.AddSingleton<IFakeMultipleService, FakeDisposableCallbackInnerService>();
serviceCollection.AddSingleton<IFakeMultipleService, FakeDisposableCallbackInnerService>();
serviceCollection.AddSingleton<IFakeService, FakeDisposableCallbackInnerService>();
Copy link
Member

Choose a reason for hiding this comment

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

Nit: might make sense to have a test that mixes service lifetimes. Could be relevant if we go back to tracking disposables using multiple collections.


namespace Microsoft.Extensions.DependencyInjection.Specification.Fakes
{
public class FakeDisposeCallback
Copy link
Member

Choose a reason for hiding this comment

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

There's already 1000 .cs files in Specification.Tests/Fakes, just put all the public classes in their own file.

Expression.Block(
assignExpression,
addValueExpression,
captureDisposableExpression)),
Copy link
Member

Choose a reason for hiding this comment

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

Suuuper nit: match ordering with runtime resolver. I know it doesn't really matter.

Copy link
Member

@halter73 halter73 left a comment

Choose a reason for hiding this comment

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

A benchmark would be nice to alleviate @davidfowl's concerns.

Eliminate redundant CaptureDisposable call from expression trees
Split fakes into separate files
@pakrym pakrym merged commit 0893e6d into dev Mar 17, 2017
@pakrym pakrym mentioned this pull request Mar 17, 2017
@natemcmaster natemcmaster deleted the pakrym/dispose-order branch November 2, 2018 16:24
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.

4 participants