Skip to content

Commit b58c310

Browse files
author
Oren (electricessence)
committed
Updates after resharper inspection.
1 parent 02a0312 commit b58c310

17 files changed

Lines changed: 128 additions & 120 deletions

benchmarking/Open.Disposable.ObjectPools.Benchmarking.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77
<Version>1.0.0</Version>
88
</PropertyGroup>
99

10+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
11+
<LangVersion>latest</LangVersion>
12+
</PropertyGroup>
13+
14+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
15+
<LangVersion>latest</LangVersion>
16+
</PropertyGroup>
17+
1018
<ItemGroup>
1119
<Compile Remove="OldTest.cs" />
1220
</ItemGroup>

benchmarking/Program.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ static void Main(string[] args)
3939
count => () => OptimisticArrayObjectPool.Create<object>((int)count * 2));
4040

4141
// Is ineveitably slower than the above but should be enabled for testing code changes.
42-
//report.AddBenchmark("InterlockedArrayObjectPool",
43-
// count => () => InterlockedArrayObjectPool.Create<object>((int)count * 2));
42+
report.AddBenchmark("InterlockedArrayObjectPool",
43+
count => () => InterlockedArrayObjectPool.Create<object>((int)count * 2));
4444

4545
report.Pretest(200, 200); // Run once through first to scramble/warm-up initial conditions.
4646

4747
Console.SetCursorPosition(0, Console.CursorTop);
4848

49-
const int loopMultiple = 2;
49+
const int loopMultiple = 6;
5050
report.Test(4, 8 * loopMultiple);
5151
report.Test(10, 8 * loopMultiple);
5252
report.Test(50, 12 * loopMultiple);

source/Array/InterlockedArrayObjectPool.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ public InterlockedArrayObjectPool(Func<T> factory, int capacity = DEFAULT_CAPACI
2727
protected ReferenceContainer<T>[] Pool;
2828

2929
// Sets a limit on what has been stored yet to prevent over searching the array unnecessarily..
30-
protected int MaxStored = 0;
30+
protected int MaxStored;
3131
protected const int MaxStoredIncrement = 5; // Instead of every one.
3232

3333
public override int Count
34-
=> Pool.Count(e => e.Value != null) + PocketCount;
34+
=> Pool.Count(e => e.Value != null) + PocketCount;
3535

3636
protected virtual bool Store(ReferenceContainer<T>[] p, T item, int index)
3737
=> p[index].TrySave(item);
@@ -41,7 +41,7 @@ protected override bool Receive(T item)
4141
var elements = Pool;
4242
var len = elements?.Length ?? 0;
4343

44-
for (int i = 0; i < len; i++)
44+
for (var i = 0; i < len; i++)
4545
{
4646
if (Store(elements, item, i))
4747
{
@@ -62,7 +62,7 @@ protected override T TryRelease()
6262
if (elements == null) return null;
6363

6464
var len = elements.Length;
65-
for (int i = 0; i < MaxStored && i < len; i++)
65+
for (var i = 0; i < MaxStored && i < len; i++)
6666
{
6767
var item = elements[i].TryRetrieve();
6868
if (item != null) return item;

source/Collection/CollectionWrapperObjectPool.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,14 @@ protected override T TryRelease()
4141

4242
var p = Pool;
4343
var item = p?.FirstOrDefault();
44-
if (item != null)
45-
{
46-
/* Removing the first item is typically horribly inefficient but we can't make assumptions about the implementation here.
44+
if (item == null) return null;
45+
/* Removing the first item is typically horribly inefficient but we can't make assumptions about the implementation here.
4746
* It's a trade off between potentially iterating the entire collection before removing the last item, or relying on the underlying implementation.
4847
* This implementation is in place for reference more than practice. Sub classes should override. */
4948

50-
bool wasRemoved = false;
51-
lock (p) wasRemoved = p.Remove(item);
52-
if (!wasRemoved) goto retry;
53-
}
49+
bool wasRemoved;
50+
lock (p) wasRemoved = p.Remove(item);
51+
if (!wasRemoved) goto retry;
5452

5553
return item;
5654
}

source/Collection/ConcurrentQueueObjectPool.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ protected override T TryRelease()
3535
{
3636
var p = Pool;
3737
if (p == null) return null;
38-
p.TryDequeue(out T item);
38+
p.TryDequeue(out var item);
3939
return item;
4040
}
4141

source/Collection/ConcurrentStackObjectPool.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ protected override T TryRelease()
3030
{
3131
var p = Pool;
3232
if (p == null) return null;
33-
p.TryPop(out T item);
33+
p.TryPop(out var item);
3434
return item;
3535
}
3636

source/IObjectPool.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ public interface IObjectPool<T> : IDisposable
2121
/// WARNING: The item is considered 'dead' but resurrectable so be sure not to hold on to the item's reference.
2222
/// </summary>
2323
/// <param name="item">The item to give up to the pool.</param>
24-
/// <param name="recycler">An optional action exectue on the item only if it's possible to return to the pool.</param>
2524
void Give(T item);
2625

2726
/// <summary>
@@ -52,26 +51,28 @@ public interface IObjectPool<T> : IDisposable
5251
int Count { get; }
5352
}
5453

55-
public static partial class ObjectPoolExtensions
54+
public static class ObjectPoolExtensions
5655
{
5756
/// <summary>
5857
/// Receives items and iteratively adds them to the pool.
5958
/// WARNING: These items are considered 'dead' but resurrectable so be sure not to hold on to their reference.
6059
/// </summary>
60+
/// <param name="target">The pool to give to.</param>
6161
/// <param name="items">The items to give up to the pool.</param>
6262
public static void Give<T>(this IObjectPool<T> target, IEnumerable<T> items)
6363
where T : class
6464
{
65-
if (items != null)
66-
foreach (var i in items)
67-
target.Give(i);
65+
if (items == null) return;
66+
foreach (var i in items)
67+
target.Give(i);
6868
}
6969

7070
/// <summary>
7171
/// Receives items and iteratively adds them to the pool.
7272
/// WARNING: These items are considered 'dead' but resurrectable so be sure not to hold on to their reference.
7373
/// </summary>
74-
/// <param name="item2">The first item to give up to the pool.</param>
74+
/// <param name="target">The pool to give to.</param>
75+
/// <param name="item1">The first item to give up to the pool.</param>
7576
/// <param name="item2">The second item to give up to the pool.</param>
7677
/// <param name="items">The remaining items to give up to the pool.</param>
7778
public static void Give<T>(this IObjectPool<T> target, T item1, T item2, params T[] items)

source/IRecycler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace Open.Disposable
55
{
6-
interface IRecycler<T> : IDisposable
6+
interface IRecycler<in T> : IDisposable
77
where T : class
88
{
99
bool Recycle(T item);

source/ObjectPoolAutoTrimmer.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using Open.Threading.Tasks;
22
using System;
3+
using System.Diagnostics.CodeAnalysis;
34

45
namespace Open.Disposable
56
{
7+
[SuppressMessage("ReSharper", "FieldCanBeMadeReadOnly.Local")]
68
public class ObjectPoolAutoTrimmer : DisposableBase
79
{
810
ITrimmableObjectPool _pool;
@@ -15,11 +17,13 @@ public class ObjectPoolAutoTrimmer : DisposableBase
1517
/// <summary>
1618
/// Max size that trimming will allow.
1719
/// </summary>
20+
// ReSharper disable once NotAccessedField.Global
1821
public readonly ushort TrimmedSize;
1922

2023
/// <summary>
2124
/// Time to wait/defer trimming. Default is 500 milliSeconds.
2225
/// </summary>
26+
// ReSharper disable once NotAccessedField.Global
2327
public readonly TimeSpan TrimDelay;
2428

2529
/// <summary>

source/ObjectPoolBase.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@ protected ObjectPoolBase(Func<T> factory, Action<T> recycler, Action<T> disposer
2020
protected int MaxSize;
2121
public int Capacity => MaxSize;
2222

23+
/// <inheritdoc />
2324
/// <summary>
2425
/// Total number of items in the pool.
2526
/// </summary>
2627
public abstract int Count { get; }
27-
protected virtual int PocketCount => Pocket.Value == null ? 0 : 1;
28+
protected int PocketCount => Pocket.Value == null ? 0 : 1;
2829

29-
protected Action<T> Recycler; // Before entering the pool.
30-
protected Action<T> OnDiscarded; // When not able to be used.
30+
protected readonly Action<T> Recycler; // Before entering the pool.
31+
protected readonly Action<T> OnDiscarded; // When not able to be used.
3132

3233

3334
// Read-only because if Take() is called after disposal, this still facilitates returing an object.
@@ -36,14 +37,15 @@ protected ObjectPoolBase(Func<T> factory, Action<T> recycler, Action<T> disposer
3637

3738
public T Generate() => Factory();
3839

40+
// ReSharper disable once UnassignedField.Global
3941
protected ReferenceContainer<T> Pocket; // Default struct constructs itself.
4042

4143
#region Receive (.Give(T item))
4244
protected virtual bool CanReceive => true; // A default of true is acceptable, enabling the Recieve method to do the actual deciding.
4345

4446
protected bool PrepareToReceive(T item)
4547
{
46-
if (item!=null && CanReceive)
48+
if (item != null && CanReceive)
4749
{
4850
var r = Recycler;
4951
if (r != null)
@@ -97,7 +99,7 @@ public bool TryTake(out T item)
9799
protected virtual bool SaveToPocket(T item)
98100
=> Pocket.TrySave(item);
99101

100-
protected virtual T TakeFromPocket()
102+
protected T TakeFromPocket()
101103
=> Pocket.TryRetrieve();
102104

103105

@@ -122,11 +124,9 @@ protected override void OnBeforeDispose()
122124

123125
protected override void OnDispose(bool calledExplicitly)
124126
{
125-
if (calledExplicitly && OnDiscarded != null)
126-
{
127-
T d;
128-
while ((d = TryRelease()) != null) OnDiscarded(d);
129-
}
127+
if (!calledExplicitly || OnDiscarded == null) return;
128+
T d;
129+
while ((d = TryRelease()) != null) OnDiscarded(d);
130130
}
131131
}
132132
}

0 commit comments

Comments
 (0)