This repository was archived by the owner on Nov 2, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 314
[Design] Reduce DI Allocations #289
Closed
+111
−4
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
6a0ad26
Reduce DI Allocations
benaadams 3f44164
Don't clear _createServiceAccessor
benaadams a5746e4
Restore readonly
benaadams 572fc71
Bag back rather than Queue
benaadams 09e07f7
review changes
benaadams 7194bcb
Single dispose, backing disposable, throw errors
benaadams 1b0d07b
Empty correct object
benaadams 9bef883
Use resx for exception, make internal method public
benaadams 5560096
Added Pool Test
benaadams 15b8df8
Merge remote-tracking branch 'aspnet/dev' into Reduce-DI-Allocations
benaadams c64fb60
Make _halfCapacity actually half
benaadams File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
src/Microsoft.Framework.DependencyInjection/Properties/Resources.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 27 additions & 1 deletion
28
src/Microsoft.Framework.DependencyInjection/ServiceLookup/ServiceScopeFactory.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,46 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Concurrent; | ||
|
||
namespace Microsoft.Framework.DependencyInjection.ServiceLookup | ||
{ | ||
internal class ServiceScopeFactory : IServiceScopeFactory | ||
{ | ||
private readonly int _capacity; | ||
private readonly int _halfCapacity; | ||
private readonly ConcurrentQueue<ServiceScope> _scopePool = new ConcurrentQueue<ServiceScope>(); | ||
|
||
private readonly ServiceProvider _provider; | ||
|
||
public ServiceScopeFactory(ServiceProvider provider) | ||
{ | ||
_capacity = 32 * Environment.ProcessorCount; | ||
_halfCapacity = _capacity / 2; | ||
_provider = provider; | ||
} | ||
|
||
public IServiceScope CreateScope() | ||
{ | ||
return new ServiceScope(new ServiceProvider(_provider)); | ||
ServiceScope scope; | ||
// maintain unused buffer of _halfCapacity as partial defense against badly behaving user code | ||
if (_scopePool.Count > _halfCapacity && _scopePool.TryDequeue(out scope)) | ||
{ | ||
scope.Reset(); | ||
return scope; | ||
} | ||
return new ServiceScope(new ServiceProvider(_provider), this); | ||
} | ||
|
||
internal void PoolScope(ServiceScope scope) | ||
{ | ||
// Benign race condition | ||
if (_scopePool.Count < _capacity) | ||
{ | ||
_scopePool.Enqueue(scope); | ||
} | ||
} | ||
|
||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Why internal here? It would also be helpful to capture your explanation of this in comments.
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.
Not sure what to write... Its a method that is explicitly bonded to the pooling of ServiceScopeFactory so if the class were made public rather than internal for whatever reason the exposed api would be for it to function as a standalone class (which is why I left public constructor in); whereas the "internals" only work as a class pair.
Not hugely fussed on visibility though, can make public
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 would leave it internal