Object Pool

The ObjectPool class provides a simple set of APIs to enable recycling of high-level objects. This can be very useful to avoid putting extra strain on the garbage collector and improve performance in cases where it’s non-trivial to create an object, but is faster to reset and reuse the object. This is especially true when dealing with WoW’s UI elements which can’t be GC’d. This can also be easily paired with classes to allow for recycling of class objects.

Example

Below is an example which demonstrates how to use an ObjectPool.

-- MyClass.lua
local MyModule = select(2, ...).MyModule
local ObjectPool = MyModule:From("LibTSMUtil"):IncludeClassType("ObjectPool")
local MyClass = MyModule:DefineClassType("MyClass")
local pool = ObjectPool.New("MY_CLASS", MyClass)

function MyClass.__static.Acquire()
   local obj = pool:Get()
   obj:_Acquire()
   return obj
end

function MyClass.__private:__init()
   self._value = nil
end

function MyClass:Release()
   self._value = nil
   pool:Recycle(self)
end

function MyClass:SetValue(value)
   self._value = value
end

function MyClass:GetValue()
   return self._value
end

-- Main.lua
local MyModule = select(2, ...).MyModule
local MyClass = MyModule:IncludeClassType("MyClass")

local obj = MyClass.Acquire()
obj:SetValue(42)
print(obj:GetValue()) -- 42
obj:Release()

local obj2 = MyClass.Acquire()
print(obj2:GetValue()) -- nil
obj2:Release()

API

class ObjectPool: Class
staticmethod New(
    name: string,
    createFunc: function | Class,
    extraStackOffset?: number
): ObjectPool

Create a new object pool.

Parameters:
  • name (string) – The name of the object pool for debug purposes

  • createFunc (function | Class) – The function which is called to create a new object

  • extraStackOffset? (number) – The extra stack offset for tracking where objects are being used from or nil to disable stack info

staticmethod EnableLeakDebug()

Enables leak debugging.

staticmethod GetDebugInfo(): table<string, ObjectPoolDebugInfo>

Gets debug information which represents the current state of all the created object pools

Get(self: ObjectPool): <T>

Either returns a recycled instance of the object or creates a new one as applicable.

Recycle(self: ObjectPool, obj: <T>)

Recycles an instance of the object back into the pool.

Parameters:

obj (<T>) – The object to recycle