-
Notifications
You must be signed in to change notification settings - Fork 763
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Replace List with ConcurrentDictionary in AsyncState * Remove unused namespace in FeaturesPooledPolicy.cs * Fixes in tests * Replace ConcurrentDictionary with custom Features class * Remove unused namespace in AsyncState.cs --------- Co-authored-by: Martin Obratil <maobrati@microsoft.com>
- Loading branch information
Showing
5 changed files
with
66 additions
and
67 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections.Generic; | ||
|
||
namespace Microsoft.Extensions.AsyncState; | ||
|
||
internal sealed class Features | ||
{ | ||
private readonly List<object?> _items = []; | ||
|
||
public object? Get(int index) | ||
{ | ||
return _items.Count <= index ? null : _items[index]; | ||
} | ||
|
||
public void Set(int index, object? value) | ||
{ | ||
if (_items.Count <= index) | ||
{ | ||
lock (_items) | ||
{ | ||
var count = index + 1; | ||
|
||
#if NET6_0_OR_GREATER | ||
_items.EnsureCapacity(count); | ||
#endif | ||
|
||
var difference = count - _items.Count; | ||
|
||
for (int i = 0; i < difference; i++) | ||
{ | ||
_items.Add(null); | ||
} | ||
} | ||
} | ||
|
||
_items[index] = value; | ||
} | ||
|
||
public void Clear() | ||
{ | ||
_items.Clear(); | ||
} | ||
} |
15 changes: 5 additions & 10 deletions
15
src/Libraries/Microsoft.Extensions.AsyncState/FeaturesPooledPolicy.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 |
---|---|---|
@@ -1,27 +1,22 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections.Generic; | ||
using Microsoft.Extensions.ObjectPool; | ||
|
||
namespace Microsoft.Extensions.AsyncState; | ||
|
||
internal sealed class FeaturesPooledPolicy : IPooledObjectPolicy<List<object?>> | ||
internal sealed class FeaturesPooledPolicy : IPooledObjectPolicy<Features> | ||
{ | ||
/// <inheritdoc/> | ||
public List<object?> Create() | ||
public Features Create() | ||
{ | ||
return []; | ||
return new Features(); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public bool Return(List<object?> obj) | ||
public bool Return(Features obj) | ||
{ | ||
for (int i = 0; i < obj.Count; i++) | ||
{ | ||
obj[i] = null; | ||
} | ||
|
||
obj.Clear(); | ||
return true; | ||
} | ||
} |
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