Class ObjectPool<T>
- Namespace
- Misaki.HighPerformance.Buffer
- Assembly
- Misaki.HighPerformance.dll
public class ObjectPool<T> : IDisposable where T : class
Type Parameters
T
- Inheritance
-
ObjectPool<T>
- Implements
- Inherited Members
Constructors
ObjectPool(Func<T>, Action<T>?, int)
Initializes a new instance of the ObjectPool class with the specified factory function, reset action, and initial size.
public ObjectPool(Func<T> factory, Action<T>? resetAction, int initialSize = 0)
Parameters
factoryFunc<T>The factory function used to create new instances of the pooled object.
resetActionAction<T>The action to invoke when an object is returned to the pool.
initialSizeintThe initial number of objects to pre-create and add to the pool.
Properties
InitialSize
Gets the initial size of the object pool, which indicates how many objects were pre-created and added to the pool upon initialization.
public int InitialSize { get; }
Property Value
Methods
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()
~ObjectPool()
protected ~ObjectPool()
Rent()
Rents an object from the pool. If the pool is empty, a new instance will be created using the factory function.
public T Rent()
Returns
- T
Reset()
Resets the object pool by clearing all objects from the pool and invoking the reset action on each object before clearing.
public void Reset()
Return(T)
Returns an object to the pool. The reset action will be invoked on the object before it is added back to the pool.
public void Return(T obj)
Parameters
objTThe object to return to the pool.
TryRent(out T)
Tries to rent an object from the pool without creating a new instance if the pool is empty.
public bool TryRent(out T obj)
Parameters
objTThe object rented from the pool, or null if the pool is empty.
Returns
- bool
true if an object was successfully rented; otherwise, false.