-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pool the underlying list and dictionary in scopes. (#50463)
- This change pools a set of scopes assuming they are short lived. - One breaking change is that after disposal, pooled scopes will throw if services are accessed afterwards on the scope. - Modified test to throw after dispose
- Loading branch information
Showing
7 changed files
with
181 additions
and
42 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
src/libraries/Microsoft.Extensions.DependencyInjection/src/ScopePool.cs
This file contains 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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Threading; | ||
using Microsoft.Extensions.DependencyInjection.ServiceLookup; | ||
|
||
namespace Microsoft.Extensions.DependencyInjection | ||
{ | ||
internal class ScopePool | ||
{ | ||
// Modest number to re-use. We only really care about reuse for short lived scopes | ||
private const int MaxQueueSize = 128; | ||
|
||
private int _count; | ||
private readonly ConcurrentQueue<State> _queue = new(); | ||
|
||
public State Rent() | ||
{ | ||
if (_queue.TryDequeue(out State state)) | ||
{ | ||
Interlocked.Decrement(ref _count); | ||
return state; | ||
} | ||
return new State(this); | ||
} | ||
|
||
public bool Return(State state) | ||
{ | ||
if (Interlocked.Increment(ref _count) > MaxQueueSize) | ||
{ | ||
Interlocked.Decrement(ref _count); | ||
return false; | ||
} | ||
|
||
state.Clear(); | ||
_queue.Enqueue(state); | ||
return true; | ||
} | ||
|
||
public class State | ||
{ | ||
private readonly ScopePool _pool; | ||
|
||
public IDictionary<ServiceCacheKey, object> ResolvedServices { get; } | ||
public List<object> Disposables { get; set; } | ||
|
||
public State(ScopePool pool = null) | ||
{ | ||
_pool = pool; | ||
// When pool is null, we're in the global scope which doesn't need pooling. | ||
// To reduce lock contention for singletons upon resolve we use a concurrent dictionary. | ||
ResolvedServices = pool == null ? new ConcurrentDictionary<ServiceCacheKey, object>() : new Dictionary<ServiceCacheKey, object>(); | ||
} | ||
|
||
internal void Clear() | ||
{ | ||
// This should only get called from the pool | ||
Debug.Assert(_pool != null); | ||
// REVIEW: Should we trim excess here as well? | ||
ResolvedServices.Clear(); | ||
Disposables?.Clear(); | ||
} | ||
|
||
public bool Return() | ||
{ | ||
return _pool?.Return(this) ?? false; | ||
} | ||
} | ||
} | ||
} |
This file contains 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 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 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 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 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
Oops, something went wrong.