Skip to content

Collections · API

SlotAllocator

Hands out the same handles as SlotMap, for values kept in your own arrays. Each handle names a slot, and SlotAllocator:SlotOf gives a live handle’s slot, so arrays indexed by slot hold the values. A slot is reused 32,767 times, then retired, so a handle to a freed slot never matches again.

local Slots = Collections.SlotAllocator.new()
local Names = {}
local Handle, Slot = Slots:Allocate()
Names[Slot] = "goblin"
Slots:Free(Handle)
print(Slots:IsAlive(Handle), Slots:SlotOf(Handle)) -- false nil
Member Description
.PackHandle() Builds a handle from a slot and a generation, as Collections.PackHandle does.
.UnpackHandle() Splits a handle into its slot and generation, as Collections.UnpackHandle does.
.new() Creates an empty allocator.
:Allocate() Takes the lowest free slot and returns a handle to it.
:IsAlive() Whether Handle’s slot is still allocated to it.
:SlotOf() Returns a live Handle’s slot, or nil once it is freed.
:Free() Frees Handle’s slot, and returns false if Handle was not live.
:Count() How many handles are live.
:Capacity() How many slots fit before it next grows.
:Clear() Frees every slot at once.
Config Settings for SlotAllocator.new.
function SlotAllocator.PackHandle(Slot: number, Generation: number): number

Builds a handle from a slot and a generation, as Collections.PackHandle does.

function SlotAllocator.UnpackHandle(Handle: number): (number, number)

Splits a handle into its slot and generation, as Collections.UnpackHandle does.

function SlotAllocator.new(Config: Config?): SlotAllocator

Creates an empty allocator.

function SlotAllocator:Allocate(): (number, number)

Takes the lowest free slot and returns a handle to it.

Returns Description
number the handle
number its slot
function SlotAllocator:IsAlive(Handle: number): boolean

Whether Handle’s slot is still allocated to it.

function SlotAllocator:SlotOf(Handle: number): number?

Returns a live Handle’s slot, or nil once it is freed.

function SlotAllocator:Free(Handle: number): boolean

Frees Handle’s slot, and returns false if Handle was not live.

function SlotAllocator:Count(): number

How many handles are live.

function SlotAllocator:Capacity(): number

How many slots fit before it next grows.

function SlotAllocator:Clear()

Frees every slot at once. Every handle given out so far stops matching.

export type Config = {
	InitialCapacity: number?,
	OnGrow: ((Old: number, New: number) -> ())?,
}

Settings for SlotAllocator.new.

Field Type Description
InitialCapacity number? slots to make room for up front, 8 by default, rounded up to a power of two
OnGrow ((Old: number, New: number) -> ())? called with the old and new capacity whenever it grows

© 2026 PRMS Developer