Skip to content

Commit 1e02410

Browse files
committed
Implement pooling providers within Core
1 parent 2204865 commit 1e02410

5 files changed

Lines changed: 92 additions & 118 deletions

File tree

src/ArrayPool.cs

Lines changed: 8 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,21 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using Oxide.Pooling;
23

34
namespace Oxide.Core
45
{
56
public static class ArrayPool
67
{
7-
private const int MaxArrayLength = 50;
8-
private const int InitialPoolAmount = 64;
9-
private const int MaxPoolAmount = 256;
10-
11-
private static List<Queue<object[]>> _pooledArrays = new List<Queue<object[]>>();
8+
private static readonly IArrayPoolProvider<object> pool;
129

1310
static ArrayPool()
1411
{
15-
for (int i = 0; i < MaxArrayLength; i++)
16-
{
17-
_pooledArrays.Add(new Queue<object[]>());
18-
SetupArrays(i + 1);
19-
}
20-
}
21-
22-
public static object[] Get(int length)
23-
{
24-
if (length == 0 || length > MaxArrayLength)
25-
{
26-
return new object[length];
27-
}
28-
29-
Queue<object[]> arrays = _pooledArrays[length - 1];
30-
lock (arrays)
31-
{
32-
if (arrays.Count == 0)
33-
{
34-
SetupArrays(length);
35-
}
36-
return arrays.Dequeue();
37-
}
12+
pool = Interface.Oxide.PoolFactory.GetArrayProvider<object>();
3813
}
3914

40-
public static void Free(object[] array)
41-
{
42-
if (array == null || array.Length == 0 || array.Length > MaxArrayLength)
43-
{
44-
return;
45-
}
15+
[Obsolete("Use Interface.Oxide.PoolFactory")]
16+
public static object[] Get(int length) => pool.Take(length);
4617

47-
// Cleanup array
48-
for (int i = 0; i < array.Length; i++)
49-
{
50-
array[i] = null;
51-
}
52-
Queue<object[]> arrays = _pooledArrays[array.Length - 1];
53-
lock (arrays)
54-
{
55-
if (arrays.Count > MaxPoolAmount)
56-
{
57-
for (int i = 0; i < InitialPoolAmount; i++)
58-
{
59-
arrays.Dequeue();
60-
}
61-
return;
62-
}
63-
64-
arrays.Enqueue(array);
65-
}
66-
}
67-
68-
private static void SetupArrays(int length)
69-
{
70-
Queue<object[]> arrays = _pooledArrays[length - 1];
71-
for (int i = 0; i < InitialPoolAmount; i++)
72-
{
73-
arrays.Enqueue(new object[length]);
74-
}
75-
}
18+
[Obsolete("Use Interface.Oxide.PoolFactory")]
19+
public static void Free(object[] array) => pool.Return(array);
7620
}
7721
}

0 commit comments

Comments
 (0)