nupp.mem.pool
A pool of record instances of one declared type.
An instance leaves the pool stamped with the declaration and carrying no fields, and comes back cleared, so a hot path that would otherwise build a table per event reuses the ones it already built.
local pool = nupp.mem.pool
local record Damage
amount: integer
source: string
end
local damage = pool.new(Damage, 64)
local hit = damage:acquire()
hit.amount = 12
hit.source = "trap"
damage:release(hit) -- cleared, and kept for the next acquirecapacity is how many released instances the pool keeps; it is not a limit on how many are out at once. An acquire past the free list allocates, and a release onto a full free list lets the collector have the instance. Within capacity, once the free list is warm, neither operation allocates.
Nothing here reaches C storage: the pool is ordinary tables, so it is available on every backend. Struct rows are nupp.mem.arena.
Module contents
Constructors
| Constructor | Description |
|---|---|
new | Creates a pool of witness instances with capacity of them stamped and waiting. |
Types
| Type | Kind | Description |
|---|---|---|
Pool | record | A pool of instances stamped with one record declaration. |
Constructors#
pool.newconstructor#
Creates a pool of witness instances with capacity of them stamped and waiting.
Type parameters
| Name | Description |
|---|---|
T |
Arguments
| Name | Type | Description |
|---|---|---|
witness | Type<T> | the record declaration, whose metatable stamps every instance |
capacity | integer? | how many released instances to retain, 32 when omitted |
Returns
| Type | Description |
|---|---|
pool.Pool<T> | the pool, warmed to capacity |
Raises
when capacity is negative
Types#
Poolrecord#
record pool.Pool<T is table>
readonly capacity: integer
acquire: function(exclusive self: Pool<T>): T
release: function(exclusive self: Pool<T>, value: T): nil
reserve: function(exclusive self: Pool<T>, count: integer): nil
live: function(self: Pool<T>): integer
free: function(self: Pool<T>): integer
reset: function(exclusive self: Pool<T>): nil
endA pool of instances stamped with one record declaration.
Type parameters
| Name | Description |
|---|---|
T |
Methods
acquire#
Hands out an instance, from the free list or freshly stamped.
Arguments
| Name | Type | Description |
|---|---|---|
exclusive self | Pool<T> |
Returns
| Type | Description |
|---|---|
T |
release#
Clears an instance and returns it to the free list.
Arguments
| Name | Type | Description |
|---|---|---|
exclusive self | Pool<T> | |
value | T |
Returns
| Type | Description |
|---|---|
nil |
Raises
when nothing is live, or value was not stamped by the pool's declaration
reserve#
Stamps free instances until at least count are waiting.
Arguments
| Name | Type | Description |
|---|---|---|
exclusive self | Pool<T> | |
count | integer |
Returns
| Type | Description |
|---|---|
nil |
Raises
when count is negative
live#
live: function(self: Pool<T>): integerHow many instances are acquired and not yet released.
Arguments
| Name | Type | Description |
|---|---|---|
self | Pool<T> |
Returns
| Type | Description |
|---|---|
integer |
free#
free: function(self: Pool<T>): integerHow many instances are waiting on the free list.
Arguments
| Name | Type | Description |
|---|---|---|
self | Pool<T> |
Returns
| Type | Description |
|---|---|
integer |
reset#
Drops the free list.
Arguments
| Name | Type | Description |
|---|---|---|
exclusive self | Pool<T> |
Returns
| Type | Description |
|---|---|
nil |
Raises
when any instance is live