Skip to main content

Object Pool

Objects Pool's provide an efficient alternative to contineously instantiating and destroying objects. instead objects are reused as per demand from the pool.

CBSL provides 2 general purpose object pools :-

  • ObjectPool - Fixed size pool which creates objects at time of pool initialization
  • LazyObjectPool - Fixed size pool which creates objects on demand (if the pool is empty)

other than this CBSL provides a IObjectPool, it's a simple interface that you can use to implement your own object pool

Initialization#

You can provide an IEnumerable to ObjectPool in constructor but ideally you would want to use the builder func constructor

Object Pool
public ObjectPool(int size, Func<int, T> builder, Action<T> onClaim = null, Action<T> onReclaim = null) { }
Lazy Object Pool
public LazyObjectPool(int size, Func<int, T> builder, Action<T> onClaim = null, Action<T> onReclaim = null) { }

using the func you can define how each item in the pool can be created

Claim & Reclaim Hooks#

As you can see in the above, both constructor's have onClaim & onReclaim Actions as optional parameters where you can define custom login to be run on the item be claim and re-claimed. for e.g you can use these hooks to enable & disable game objects for a game object pool

public ObjectPool(
5,
index => new GameObject(),
go => go.SetActive(true),
go => go.SetActive(false)
) { }