Skip to content

Commit 2204865

Browse files
committed
Cleanup pooling classes [skip ci]
1 parent 43399be commit 2204865

4 files changed

Lines changed: 16 additions & 58 deletions

File tree

src/Pooling/BaseArrayPoolProvider.cs

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
43

54
namespace Oxide.Pooling
65
{
7-
public class BaseArrayPoolProvider<T> : IArrayPoolProvider<T>
6+
internal class BaseArrayPoolProvider<T> : IArrayPoolProvider<T>
87
{
98
private readonly int maxSetCapacity;
109
private readonly int maxArrayLength;
1110

1211
private readonly T[] empty;
13-
private readonly ICollection<ICollection<T[]>> pooledArrays;
12+
private readonly Stack<T[]>[] pooledArrays;
1413

1514
public BaseArrayPoolProvider()
1615
{
@@ -19,29 +18,11 @@ public BaseArrayPoolProvider()
1918
// ReSharper disable once VirtualMemberCallInConstructor
2019
empty = InstantiateArray(0);
2120

22-
if (maxArrayLength > 7)
23-
{
24-
pooledArrays = new HashSet<ICollection<T[]>>();
25-
}
26-
else
27-
{
28-
pooledArrays = new List<ICollection<T[]>>(maxArrayLength - 1);
29-
}
21+
pooledArrays = new Stack<T[]>[maxArrayLength];
3022

31-
for (int i = 0; i < maxArrayLength; i++)
23+
for (int i = 0; i < pooledArrays.Length; i++)
3224
{
33-
ICollection<T[]> pool;
34-
35-
if (maxSetCapacity > 6)
36-
{
37-
pool = new HashSet<T[]>();
38-
}
39-
else
40-
{
41-
pool = new List<T[]>(maxSetCapacity);
42-
}
43-
44-
pooledArrays.Add(pool);
25+
pooledArrays[i] = new Stack<T[]>(maxSetCapacity);
4526
}
4627
}
4728

@@ -65,17 +46,11 @@ public T[] Take(int length)
6546
}
6647

6748
T[] item;
68-
69-
ICollection<T[]> pooled = pooledArrays.ElementAt(length - 1);
49+
Stack<T[]> pooled = pooledArrays[length - 1];
7050

7151
lock (pooled)
7252
{
73-
if (pooled.Count != 0)
74-
{
75-
item = pooled.ElementAt(0);
76-
pooled.Remove(item);
77-
}
78-
else
53+
if (!pooled.TryPop(out item))
7954
{
8055
item = InstantiateArray(length);
8156
}
@@ -102,13 +77,13 @@ public void Return(object item)
10277
return;
10378
}
10479

105-
ICollection<T[]> pooled = pooledArrays.ElementAt(array.Length - 1);
80+
Stack<T[]> pooled = pooledArrays[array.Length - 1];
10681

10782
lock (pooled)
10883
{
10984
if (pooled.Count < maxSetCapacity)
11085
{
111-
pooled.Add(array);
86+
pooled.Push(array);
11287
}
11388
}
11489
}

src/Pooling/BasePoolProvider.cs

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,24 @@
11
using System.Collections.Generic;
2-
using System.Linq;
32

43
namespace Oxide.Pooling
54
{
6-
public abstract class BasePoolProvider<T> : IPoolProvider<T>
5+
internal abstract class BasePoolProvider<T> : IPoolProvider<T>
76
{
87
private int MaxPoolSize { get; }
9-
private readonly ICollection<T> pooledData;
8+
private readonly Stack<T> pooledData;
109

1110
protected BasePoolProvider()
1211
{
1312
MaxPoolSize = 50; // TODO: Set based on configuration
14-
15-
if (MaxPoolSize > 6)
16-
{
17-
pooledData = new HashSet<T>();
18-
}
19-
else
20-
{
21-
pooledData = new List<T>(MaxPoolSize);
22-
}
13+
pooledData = new Stack<T>(MaxPoolSize);
2314
}
2415

2516
public T Take()
2617
{
2718
T item;
2819
lock (pooledData)
2920
{
30-
if (pooledData.Count > 0)
31-
{
32-
item = pooledData.ElementAt(0);
33-
pooledData.Remove(item);
34-
35-
}
36-
else
21+
if (!pooledData.TryPop(out item))
3722
{
3823
item = InstantiateItem();
3924
}
@@ -51,7 +36,7 @@ public void Return(object item)
5136
{
5237
if (pooledData.Count < MaxPoolSize)
5338
{
54-
pooledData.Add(typed);
39+
pooledData.Push(typed);
5540
}
5641
}
5742
}

src/Pooling/CorePoolFactory.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics;
4-
using System.Security.Permissions;
5-
using System.Text;
64

75
namespace Oxide.Pooling
86
{
9-
public class CorePoolFactory : IPoolFactory
7+
internal class CorePoolFactory : IPoolFactory
108
{
119
private readonly Type arrayType = typeof(IArrayPoolProvider<>);
1210
private readonly Type itemType = typeof(IPoolProvider<>);

src/Pooling/StringPoolProvider.cs

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

33
namespace Oxide.Pooling
44
{
5-
public sealed class StringPoolProvider : BasePoolProvider<StringBuilder>
5+
internal sealed class StringPoolProvider : BasePoolProvider<StringBuilder>
66
{
77
protected override void OnTake(StringBuilder item) => OnReturn(item);
88

0 commit comments

Comments
 (0)