Pools
Pool and reuse collections and other object types to minimize memory allocation/garbage collection.
Install / Use
/learn @beatthat/PoolsREADME
<a name="readme"></a>Pool and reuse collections and other object types to minimize memory allocation/garbage collection.
Install
From your unity project folder:
npm init
npm install beatthat/pools --save
The package and all its dependencies will be installed under Assets/Plugins/packages/beatthat.
In case it helps, a quick video of the above: https://youtu.be/Uss_yOiLNw8
USAGE
Use a List in a using block
The list type returned is a Disposable whose dispose method returns the list to the pool so...
using(var list = ListPool<string>.Get() {
// use the list
}
Use a pooled list and return it manually when you're done
Usually you should wrap the list's use in a using block as above, but you can't do that if, say, you're going to do something asynchronous with the list.
var list = ListPool<string>.Get();
SomeAsyncMethod(list, () => {
// have to dispose list manually
// because its use extends
// into async callback
list.Dispose()
})
Use a pooled copy of some other collection
If you're using a pooled list to work with a copy of some other collection already in hand, you can pass the collection you want to copy into Get and the returned list will already have the contents copied for you.
IDictionary<string, string> d = SomeDictionary();
using(var list = ListPool<string>.Get(d.Values) {
// list has all the values from d
}
Pools for Dictionary and StringBuilder work similarly to list
IDictionary<string, string> d = SomeDictionary();
using(var pooledDict = DictionaryPool<string, string>.Get(d) {
// list has all the values from d
}
using(var sb = PooledStringBuilder.Get() {
// sb.stringBuilder holds the StringBuilder
// in an IDisposable wrapper
}
ArrayPool requires a size or a collection/array to copy from
using(var a = ArrayPool<string>.Get(3) {
// array will have 3 elements
// actual array is in a.array
// because array itself can't be extended to implement IDisposable
}
IDictionary<string, string> d = SomeDictionary();
using(var a = ArrayPool<string>.Get(d.Values) {
// array will have all the values of d as elements
// actual array is in a.array
// because array itself can't be extended to implement IDisposable
}
Use StaticObjectPool<T> to pool other types
You can use StaticObjectPool<T> to pool other c# types as long as they have a no-arg constructor
class Foo {}
class Bar {
void SomeMethod()
{
var foo = StaticObjectPool<Foo>.Get();
// do stuff with Foo
StaticObjectPool<Foo>.Return(foo);
}
}
Related Skills
node-connect
347.0kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
107.8kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
347.0kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
347.0kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
