Init
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
namespace QFSW.QC.Pooling
|
||||
{
|
||||
public class ConcurrentPool<T> : IPool<T> where T : class, new()
|
||||
{
|
||||
private readonly ConcurrentBag<T> _objs;
|
||||
|
||||
public ConcurrentPool()
|
||||
{
|
||||
_objs = new ConcurrentBag<T>();
|
||||
}
|
||||
|
||||
public ConcurrentPool(int objCount)
|
||||
{
|
||||
_objs = new ConcurrentBag<T>();
|
||||
for (int i = 0; i < objCount; i++)
|
||||
{
|
||||
_objs.Add(new T());
|
||||
}
|
||||
}
|
||||
|
||||
public T GetObject()
|
||||
{
|
||||
if (_objs.TryTake(out T obj))
|
||||
{
|
||||
return obj;
|
||||
}
|
||||
|
||||
return new T();
|
||||
}
|
||||
|
||||
public void Release(T obj)
|
||||
{
|
||||
_objs.Add(obj);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 226777291e3dca846a892782ceadaec0
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace QFSW.QC.Pooling
|
||||
{
|
||||
public interface IPool<T> where T : class, new()
|
||||
{
|
||||
T GetObject();
|
||||
void Release(T obj);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0fc6995174196304f926b745d10b0319
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,40 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace QFSW.QC.Pooling
|
||||
{
|
||||
public class Pool<T> : IPool<T> where T : class, new()
|
||||
{
|
||||
private readonly Stack<T> _objs;
|
||||
|
||||
public Pool()
|
||||
{
|
||||
_objs = new Stack<T>();
|
||||
}
|
||||
|
||||
public Pool(int objCount)
|
||||
{
|
||||
_objs = new Stack<T>(objCount);
|
||||
for (int i = 0; i < objCount; i++)
|
||||
{
|
||||
_objs.Push(new T());
|
||||
}
|
||||
}
|
||||
|
||||
public T GetObject()
|
||||
{
|
||||
if (_objs.Count > 0)
|
||||
{
|
||||
return _objs.Pop();
|
||||
}
|
||||
else
|
||||
{
|
||||
return new T();
|
||||
}
|
||||
}
|
||||
|
||||
public void Release(T obj)
|
||||
{
|
||||
_objs.Push(obj);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 92c171e15d3b459478e26e2217e49a05
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,36 @@
|
||||
using System.Text;
|
||||
|
||||
namespace QFSW.QC.Pooling
|
||||
{
|
||||
public class ConcurrentStringBuilderPool : StringBuilderPool<ConcurrentPool<StringBuilder>>
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public class StringBuilderPool : StringBuilderPool<Pool<StringBuilder>>
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public class StringBuilderPool<TPool> where TPool : IPool<StringBuilder>, new()
|
||||
{
|
||||
private readonly TPool _pool = new TPool();
|
||||
|
||||
public StringBuilder GetStringBuilder(int minCapacity = 0)
|
||||
{
|
||||
StringBuilder stringBuilder = _pool.GetObject();
|
||||
stringBuilder.Clear();
|
||||
stringBuilder.EnsureCapacity(minCapacity);
|
||||
|
||||
return stringBuilder;
|
||||
}
|
||||
|
||||
public string ReleaseAndToString(StringBuilder stringBuilder)
|
||||
{
|
||||
string result = stringBuilder.ToString();
|
||||
_pool.Release(stringBuilder);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d25ea49651dea404ba9ec41afb2b189f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user