|
5 | 5 |
|
6 | 6 | namespace Open.Disposable |
7 | 7 | { |
8 | | - /// <summary> |
9 | | - /// An extremely fast ObjectPool when the capacity is in the low 100s. |
10 | | - /// </summary> |
11 | | - /// <typeparam name="T"></typeparam> |
12 | | - public class InterlockedArrayObjectPool<T> : ObjectPoolBase<T> |
13 | | - where T : class |
14 | | - { |
15 | | - |
16 | | - public InterlockedArrayObjectPool(Func<T> factory, Action<T> recycler, int capacity = DEFAULT_CAPACITY) |
17 | | - : base(factory, recycler, capacity) |
18 | | - { |
19 | | - Pool = new ReferenceContainer<T>[capacity - 1]; |
20 | | - } |
21 | | - |
22 | | - public InterlockedArrayObjectPool(Func<T> factory, int capacity = DEFAULT_CAPACITY) |
23 | | - : this(factory, null, capacity) |
24 | | - { } |
25 | | - |
26 | | - protected ReferenceContainer<T>[] Pool; |
27 | | - |
28 | | - // Sets a limit on what has been stored yet to prevent over searching the array unnecessarily.. |
29 | | - protected int MaxStored = 0; |
30 | | - protected const int MaxStoredIncrement = 5; // Instead of every one. |
31 | | - |
32 | | - protected override bool Receive(T item) |
33 | | - { |
34 | | - var elements = Pool; |
35 | | - var len = elements?.Length ?? 0; |
36 | | - |
37 | | - for (int i = 0; i < len; i++) |
38 | | - { |
39 | | - if (elements[i].TrySave(item)) |
40 | | - { |
41 | | - var m = MaxStored; |
42 | | - if (i >= m) Interlocked.CompareExchange(ref MaxStored, m + MaxStoredIncrement, m); |
43 | | - |
44 | | - return true; |
45 | | - } |
46 | | - } |
47 | | - |
48 | | - return false; |
49 | | - } |
50 | | - |
51 | | - protected override T TryRelease() |
52 | | - { |
53 | | - // We missed getting the first item or it wasn't there. |
54 | | - var elements = Pool; |
55 | | - if (elements == null) return null; |
56 | | - |
57 | | - var len = elements.Length; |
58 | | - for (int i = 0; i < MaxStored && i < len; i++) |
59 | | - { |
60 | | - var item = elements[i].TryRetrieve(); |
61 | | - if (item != null) return item; |
62 | | - } |
63 | | - |
64 | | - return null; |
65 | | - } |
66 | | - |
67 | | - protected override void OnDispose(bool calledExplicitly) |
68 | | - { |
69 | | - Pool = null; |
70 | | - MaxStored = 0; |
71 | | - } |
72 | | - |
73 | | - } |
74 | | - |
75 | | - public static class InterlockedArrayObjectPool |
76 | | - { |
77 | | - public static InterlockedArrayObjectPool<T> Create<T>(Func<T> factory, int capacity = Constants.DEFAULT_CAPACITY) |
78 | | - where T : class |
79 | | - { |
80 | | - return new InterlockedArrayObjectPool<T>(factory, capacity); |
81 | | - } |
82 | | - |
83 | | - public static InterlockedArrayObjectPool<T> Create<T>(int capacity = Constants.DEFAULT_CAPACITY) |
84 | | - where T : class, new() |
85 | | - { |
86 | | - return Create(() => new T(), capacity); |
87 | | - } |
88 | | - |
89 | | - public static InterlockedArrayObjectPool<T> Create<T>(Func<T> factory, bool autoRecycle, int capacity = Constants.DEFAULT_CAPACITY) |
90 | | - where T : class, IRecyclable |
91 | | - { |
92 | | - Action<T> recycler = null; |
93 | | - if (autoRecycle) recycler = Recycler.Recycle; |
94 | | - return new InterlockedArrayObjectPool<T>(factory, recycler, capacity); |
95 | | - } |
96 | | - |
97 | | - public static InterlockedArrayObjectPool<T> Create<T>(bool autoRecycle, int capacity = Constants.DEFAULT_CAPACITY) |
98 | | - where T : class, IRecyclable, new() |
99 | | - { |
100 | | - return Create(() => new T(), autoRecycle, capacity); |
101 | | - } |
102 | | - |
103 | | - } |
| 8 | + /// <summary> |
| 9 | + /// An extremely fast ObjectPool when the capacity is in the low 100s. |
| 10 | + /// </summary> |
| 11 | + /// <typeparam name="T"></typeparam> |
| 12 | + public class InterlockedArrayObjectPool<T> : ObjectPoolBase<T> |
| 13 | + where T : class |
| 14 | + { |
| 15 | + |
| 16 | + public InterlockedArrayObjectPool(Func<T> factory, Action<T> recycler, int capacity = DEFAULT_CAPACITY) |
| 17 | + : base(factory, recycler, capacity) |
| 18 | + { |
| 19 | + Pool = new ReferenceContainer<T>[capacity - 1]; |
| 20 | + } |
| 21 | + |
| 22 | + public InterlockedArrayObjectPool(Func<T> factory, int capacity = DEFAULT_CAPACITY) |
| 23 | + : this(factory, null, capacity) |
| 24 | + { } |
| 25 | + |
| 26 | + protected ReferenceContainer<T>[] Pool; |
| 27 | + |
| 28 | + // Sets a limit on what has been stored yet to prevent over searching the array unnecessarily.. |
| 29 | + protected int MaxStored = 0; |
| 30 | + protected const int MaxStoredIncrement = 5; // Instead of every one. |
| 31 | + |
| 32 | + protected override bool Receive(T item) |
| 33 | + { |
| 34 | + var elements = Pool; |
| 35 | + var len = elements?.Length ?? 0; |
| 36 | + |
| 37 | + for (int i = 0; i < len; i++) |
| 38 | + { |
| 39 | + if (elements[i].TrySave(item)) |
| 40 | + { |
| 41 | + var m = MaxStored; |
| 42 | + if (i >= m) Interlocked.CompareExchange(ref MaxStored, m + MaxStoredIncrement, m); |
| 43 | + |
| 44 | + return true; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + return false; |
| 49 | + } |
| 50 | + |
| 51 | + protected override T TryRelease() |
| 52 | + { |
| 53 | + // We missed getting the first item or it wasn't there. |
| 54 | + var elements = Pool; |
| 55 | + if (elements == null) return null; |
| 56 | + |
| 57 | + var len = elements.Length; |
| 58 | + for (int i = 0; i < MaxStored && i < len; i++) |
| 59 | + { |
| 60 | + var item = elements[i].TryRetrieve(); |
| 61 | + if (item != null) return item; |
| 62 | + } |
| 63 | + |
| 64 | + return null; |
| 65 | + } |
| 66 | + |
| 67 | + protected override void OnDispose(bool calledExplicitly) |
| 68 | + { |
| 69 | + Pool = null; |
| 70 | + MaxStored = 0; |
| 71 | + } |
| 72 | + |
| 73 | + } |
| 74 | + |
| 75 | + public static class InterlockedArrayObjectPool |
| 76 | + { |
| 77 | + public static InterlockedArrayObjectPool<T> Create<T>(Func<T> factory, int capacity = Constants.DEFAULT_CAPACITY) |
| 78 | + where T : class |
| 79 | + { |
| 80 | + return new InterlockedArrayObjectPool<T>(factory, capacity); |
| 81 | + } |
| 82 | + |
| 83 | + public static InterlockedArrayObjectPool<T> Create<T>(int capacity = Constants.DEFAULT_CAPACITY) |
| 84 | + where T : class, new() |
| 85 | + { |
| 86 | + return Create(() => new T(), capacity); |
| 87 | + } |
| 88 | + |
| 89 | + public static InterlockedArrayObjectPool<T> Create<T>(Func<T> factory, bool autoRecycle, int capacity = Constants.DEFAULT_CAPACITY) |
| 90 | + where T : class, IRecyclable |
| 91 | + { |
| 92 | + Action<T> recycler = null; |
| 93 | + if (autoRecycle) recycler = Recycler.Recycle; |
| 94 | + return new InterlockedArrayObjectPool<T>(factory, recycler, capacity); |
| 95 | + } |
| 96 | + |
| 97 | + public static InterlockedArrayObjectPool<T> Create<T>(bool autoRecycle, int capacity = Constants.DEFAULT_CAPACITY) |
| 98 | + where T : class, IRecyclable, new() |
| 99 | + { |
| 100 | + return Create(() => new T(), autoRecycle, capacity); |
| 101 | + } |
| 102 | + |
| 103 | + } |
104 | 104 | } |
0 commit comments