A lightweight object pool

Constructor

@:value({ maxNumObjects : -1 })new (factory:Void ‑> T, ?dispose:T ‑> Void, maxNumObjects:Int = -1)

Parameters:

factory

a function responsible for creating new objects.

dispose

a function responsible for disposing objects (optional).
Invoked when the user tries to return an object to a full pool.

maxNumObjects

the maximum allowed number of pooled object.
If omitted, there is no upper limit.

Variables

@:value(GrowthRate.MILD)growthRate:Int = GrowthRate.MILD

The growth rate of the pool.

See:

read onlymaxSize:Int

The maximum allowed number of pooled objects.

@:value(0)read onlysize:Int = 0

The current number of pooled objects.

Methods

free ():Void

Destroys this object by explicitly nullifying all objects for GC'ing used resources. Improves GC efficiency/performance (optional).

inlineget ():T

Gets an object from the pool; the method either creates a new object if the pool is empty (no object has been returned yet) or returns an existing object from the pool. To minimize object allocation, return objects back to the pool as soon as their life cycle ends.

iterator ():Iterator<T>

preallocate (numObjects:Int):Void

Fills the pool in advance with numObjects objects.

inlineput (obj:T):Void

Puts obj into the pool, incrementing this.size.

Discards obj if the pool is full by passing it to the dispose function (this.size == this.maxSize).