GroveGames.ObjectPool
0.2.2
See the version list below for details.
dotnet add package GroveGames.ObjectPool --version 0.2.2
NuGet\Install-Package GroveGames.ObjectPool -Version 0.2.2
<PackageReference Include="GroveGames.ObjectPool" Version="0.2.2" />
<PackageVersion Include="GroveGames.ObjectPool" Version="0.2.2" />
<PackageReference Include="GroveGames.ObjectPool" />
paket add GroveGames.ObjectPool --version 0.2.2
#r "nuget: GroveGames.ObjectPool, 0.2.2"
#:package GroveGames.ObjectPool@0.2.2
#addin nuget:?package=GroveGames.ObjectPool&version=0.2.2
#tool nuget:?package=GroveGames.ObjectPool&version=0.2.2
GroveGames.ObjectPool
A high-performance object pooling library for .NET, Unity, and Godot with Native AOT support. Built for game development scenarios where allocation-free code is critical.
Features
- Generic Object Pooling: Pool any reference type with customizable factory, rent, and return callbacks
- Collection Pools: Specialized pools for List, Dictionary, Queue, Stack, HashSet, LinkedList, and Array
- Thread-Safe Variants: Concurrent versions of all pools for multi-threaded scenarios
- Multi-Type Pooling: Pool multiple derived types with a single pool using frozen dictionaries
- Rental Pattern: Ref struct-based rentals for automatic return via
usingstatements - Native AOT Compatible: Fully supports ahead-of-time compilation for maximum performance
- Unity Integration: GameObjectPool and ComponentPool with automatic activation management
- Godot Integration: Available as a Godot addon
.NET
Install via NuGet:
dotnet add package GroveGames.ObjectPool
Basic Object Pooling
using GroveGames.ObjectPool;
var pool = new ObjectPool<MyObject>(
factory: () => new MyObject(),
onRent: obj => obj.Initialize(),
onReturn: obj => obj.Reset(),
initialSize: 10,
maxSize: 100
);
var obj = pool.Rent();
obj.DoWork();
pool.Return(obj);
pool.Dispose();
Using the Rental Pattern
The rental pattern provides automatic return via using statements with zero-allocation ref structs:
using GroveGames.ObjectPool;
var pool = new ObjectPool<MyObject>(
() => new MyObject(),
null,
obj => obj.Reset(),
0,
100
);
using (pool.Rent(out var obj))
{
obj.DoWork();
}
Collection Pools
Pool collections to avoid repeated allocations:
using GroveGames.ObjectPool;
var listPool = new ListPool<int>(initialSize: 5, maxSize: 50);
var list = listPool.Rent();
list.Add(1);
list.Add(2);
list.Add(3);
listPool.Return(list);
using (listPool.Rent(out var items))
{
items.Add(42);
ProcessItems(items);
}
Available collection pools:
ListPool<T>- PoolsList<T>instancesDictionaryPool<TKey, TValue>- PoolsDictionary<TKey, TValue>instancesQueuePool<T>- PoolsQueue<T>instancesStackPool<T>- PoolsStack<T>instancesHashSetPool<T>- PoolsHashSet<T>instancesLinkedListPool<T>- PoolsLinkedList<T>instancesArrayPool<T>- Pools array instances
Thread-Safe Pools
Use concurrent pools for multi-threaded scenarios:
using GroveGames.ObjectPool.Concurrent;
var concurrentPool = new ConcurrentObjectPool<MyObject>(
() => new MyObject(),
null,
obj => obj.Reset(),
10,
100
);
var concurrentListPool = new ConcurrentListPool<int>(5, 50);
var concurrentDictPool = new ConcurrentDictionaryPool<string, int>(5, 50);
Multi-Type Object Pool
Pool multiple derived types with a single pool interface:
using GroveGames.ObjectPool;
public abstract class Enemy { }
public class Zombie : Enemy { }
public class Skeleton : Enemy { }
public class Ghost : Enemy { }
var pools = new MultiTypeObjectPoolBuilder<Enemy>()
.AddPool(() => new Zombie(), null, null, 5, 20)
.AddPool(() => new Skeleton(), null, null, 5, 20)
.AddPool(() => new Ghost(), null, null, 3, 10)
.Build();
var multiPool = new MultiTypeObjectPool<Enemy>(pools);
var zombie = multiPool.Rent<Zombie>();
var skeleton = multiPool.Rent<Skeleton>();
multiPool.Return(zombie);
multiPool.Return(skeleton);
Keyed Object Pool
Keep one pool per key, created the first time a key is used:
using GroveGames.ObjectPool;
var bullets = new KeyedObjectPool<string, Bullet>(
kind => new ObjectPool<Bullet>(() => new Bullet(kind), null, bullet => bullet.Reset(), 5, 50)
);
var bullet = bullets.Rent("plasma");
bullets.Return("plasma", bullet);
bullets.Warm("laser");
IndexedObjectPool<T> takes non-negative int keys and stores its pools in an array, so a lookup is a bounds check and an array read instead of a dictionary lookup. It suits dense indices, such as a table of prefabs or an enum whose values start at zero:
public enum EffectKind
{
Explosion,
Smoke,
Spark
}
var effects = new IndexedObjectPool<Effect>(
index => new ObjectPool<Effect>(() => new Effect((EffectKind)index), null, null, 0, 20)
);
var smoke = effects.Rent((int)EffectKind.Smoke);
effects.Return((int)EffectKind.Smoke, smoke);
The array grows to the highest index used, so keep indices dense: an enum value of 1000 allocates 1001 slots.
Core Components
IObjectPool<T>: Core pooling interface with Rent, Return, Clear, and DisposeObjectPool<T>: Main implementation with customizable callbacksObjectRental<T>: Ref struct for automatic return via using statementsListPool<T>,DictionaryPool<TKey, TValue>, etc.: Specialized collection poolsMultiTypeObjectPool<T>: Polymorphic pooling with frozen dictionary lookupMultiTypeObjectPoolBuilder<T>: Fluent builder for multi-type poolsIKeyedObjectPool<TKey, TValue>: Interface for pools that keep one pool per keyKeyedObjectPool<TKey, TValue>: Creates a pool per key on first use through a factoryIndexedObjectPool<T>: Array-backed keyed pool with non-negativeintkeys
Concurrent Components
ConcurrentObjectPool<T>: Thread-safe object poolConcurrentListPool<T>,ConcurrentDictionaryPool<TKey, TValue>, etc.: Thread-safe collection poolsConcurrentMultiTypeObjectPool<T>: Thread-safe polymorphic poolingConcurrentKeyedObjectPool<TKey, TValue>: Thread-safe keyed poolConcurrentIndexedObjectPool<T>: Thread-safe array-backed indexed pool with lock-free lookups
Unity
There are two installation steps required to use it in Unity.
Install
GroveGames.ObjectPoolfrom NuGet using NuGetForUnity. Open Window from NuGet → Manage NuGet Packages, search "GroveGames.ObjectPool" and press Install.Install the
GroveGames.ObjectPool.Unitypackage by referencing the git URL:https://github.com/grovegs/ObjectPool.git?path=src/GroveGames.ObjectPool.Unity/Packages/com.grovegames.objectpoolCreate a
csc.rspfile in yourAssets/directory with the following content to enable C# 10 features:-langversion:10 -nullable:enable
With the Unity package, GameObjectPool and ComponentPool become available for pooling Unity objects with automatic activation/deactivation management.
GameObjectPool
using GroveGames.ObjectPool.Unity;
using UnityEngine;
public class BulletSpawner : MonoBehaviour
{
[SerializeField] private GameObject _bulletPrefab;
[SerializeField] private int _initialSize = 10;
[SerializeField] private int _maxSize = 100;
private GameObjectPool _pool;
private void Awake()
{
_pool = new GameObjectPool(_bulletPrefab, transform, _initialSize, _maxSize);
}
public GameObject SpawnBullet()
{
return _pool.Rent();
}
public void DespawnBullet(GameObject bullet)
{
_pool.Return(bullet);
}
private void OnDestroy()
{
_pool.Dispose();
}
}
GameObjectPool with Rental Pattern
using GroveGames.ObjectPool.Unity;
using UnityEngine;
public class EffectSpawner : MonoBehaviour
{
[SerializeField] private GameObject _effectPrefab;
private GameObjectPool _pool;
private void Awake()
{
_pool = new GameObjectPool(_effectPrefab, transform, 5, 20);
}
public void SpawnEffect(Vector3 position)
{
using (_pool.Rent(out var effect))
{
effect.transform.position = position;
}
}
}
ComponentPool
Pool specific components for type-safe access:
using GroveGames.ObjectPool.Unity;
using UnityEngine;
public class EnemySpawner : MonoBehaviour
{
[SerializeField] private Enemy _enemyPrefab;
[SerializeField] private int _maxEnemies = 50;
private ComponentPool<Enemy> _pool;
private void Awake()
{
_pool = new ComponentPool<Enemy>(_enemyPrefab, transform, 0, _maxEnemies);
}
public Enemy SpawnEnemy(Vector3 position)
{
var enemy = _pool.Rent();
enemy.transform.position = position;
enemy.Initialize();
return enemy;
}
public void DespawnEnemy(Enemy enemy)
{
_pool.Return(enemy);
}
private void OnDestroy()
{
_pool.Dispose();
}
}
ComponentPool with Rental Pattern
using GroveGames.ObjectPool.Unity;
using UnityEngine;
public class ProjectileSystem : MonoBehaviour
{
[SerializeField] private Rigidbody _projectilePrefab;
private ComponentPool<Rigidbody> _pool;
private void Awake()
{
_pool = new ComponentPool<Rigidbody>(_projectilePrefab, transform, 10, 100);
}
public void FireProjectile(Vector3 position, Vector3 velocity)
{
using (_pool.Rent(out var projectile))
{
projectile.transform.position = position;
projectile.linearVelocity = velocity;
}
}
}
KeyedComponentPool
Pool several prefabs of the same component type, one pool per key:
using GroveGames.ObjectPool.Unity;
using UnityEngine;
public class ShotSpawner : MonoBehaviour
{
private KeyedComponentPool<Shot, Shot> _shots;
private void Awake()
{
_shots = new KeyedComponentPool<Shot, Shot>(static prefab => prefab, transform, 0, 32);
}
public Shot Spawn(Shot prefab)
{
return _shots.Rent(prefab);
}
public void Despawn(Shot prefab, Shot shot)
{
_shots.Return(prefab, shot);
}
private void OnDestroy()
{
_shots.Dispose();
}
}
Keys do not have to be prefabs. Any key works as long as the provider returns the prefab for it:
var effects = new KeyedGameObjectPool<string>(address => LoadPrefab(address), effectsRoot, 2, 16);
var explosion = effects.Rent("Effects/Explosion");
effects.Return("Effects/Explosion", explosion);
IndexedComponentPool
Use an index, such as an enum value, when prefabs live in a table:
using GroveGames.ObjectPool.Unity;
using UnityEngine;
public enum EffectKind
{
Explosion,
Smoke
}
public class EffectPlayer : MonoBehaviour
{
[SerializeField] private ParticleSystem[] _effectPrefabs;
private IndexedComponentPool<ParticleSystem> _effects;
private void Awake()
{
_effects = new IndexedComponentPool<ParticleSystem>(index => _effectPrefabs[index], transform, 2, 16);
}
public ParticleSystem Play(EffectKind kind)
{
return _effects.Rent((int)kind);
}
public void Stop(EffectKind kind, ParticleSystem effect)
{
_effects.Return((int)kind, effect);
}
private void OnDestroy()
{
_effects.Dispose();
}
}
Unity Components
GameObjectPool: Pools GameObjects with automatic activation/deactivationComponentPool<T>: Pools Components with automatic activation/deactivation
Unity pools own their instances: Warm creates them inactive and never beyond maxSize, Return moves an item back under the pool parent and destroys it when the pool is full, and Clear/Dispose destroy the pooled GameObjects.
KeyedGameObjectPool<TKey>: OneGameObjectPoolper key, created on first useKeyedComponentPool<TKey, T>: OneComponentPool<T>per key, created on first useIndexedGameObjectPool: OneGameObjectPoolper index, stored in an arrayIndexedComponentPool<T>: OneComponentPool<T>per index, stored in an array
Godot
Install via NuGet:
dotnet add package GroveGames.ObjectPool.Godot
Download the Godot addon from the latest release and extract it to your project's addons folder. Enable the addon in Project Settings → Plugins.
res://
├── addons/
│ └── GroveGames.ObjectPool/
│ ├── plugin.cfg
│ └── ...
└── ...
Architecture
Performance Optimizations
- Ref Struct Rentals: Zero-allocation automatic return pattern
- Frozen Dictionaries: O(1) type lookup for multi-type pools
- Queue-Based Storage: Efficient FIFO pooling behavior
- Native AOT: Full compatibility with ahead-of-time compilation
- No Boxing: Generic constraints ensure no boxing allocations
Testing
Run tests:
dotnet test
Contributing
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch
- Write tests for new functionality
- Submit a pull request
License
This project is licensed under the MIT License - see the LICENSE file for details.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 is compatible. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- System.Collections.Immutable (>= 10.0.2)
-
net10.0
- No dependencies.
-
net8.0
- No dependencies.
-
net9.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on GroveGames.ObjectPool:
| Package | Downloads |
|---|---|
|
GroveGames.ObjectPool.Godot
High-performance object pooling library for .NET, Unity, and Godot with Native AOT support |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.2.6 | 119 | 10/23/2026 |
| 0.2.5 | 109 | 10/13/2026 |
| 0.2.4 | 103 | 10/12/2026 |
| 0.2.3 | 102 | 10/8/2026 |
| 0.2.2 | 112 | 10/7/2026 |
| 0.2.1 | 106 | 10/7/2026 |
| 0.2.0 | 108 | 10/7/2026 |
| 0.1.3 | 249 | 2/10/2026 |
| 0.1.2 | 143 | 2/10/2026 |
| 0.1.1 | 142 | 2/10/2026 |
| 0.1.0 | 154 | 2/10/2026 |
| 0.0.15 | 149 | 1/17/2026 |
| 0.0.14 | 157 | 1/16/2026 |
| 0.0.13 | 140 | 1/15/2026 |
| 0.0.12 | 145 | 1/15/2026 |
| 0.0.11 | 150 | 1/15/2026 |
| 0.0.10 | 271 | 10/11/2025 |
| 0.0.6 | 210 | 10/10/2025 |
| 0.0.5 | 205 | 10/10/2025 |
| 0.0.4 | 272 | 9/23/2025 |