An implementation of user-managed object pools in Julia.
This package is designed for use cases where some inner functions would need to allocate a large number of arrays which would not escape an outer function f
. An object pool allows the user to obtain new objects with the new!
, then allow them to be recycled with recycle!
.
Consider this (contrived) example:
function f(pool, x::AbstractVector{T}) where T
recycle!(pool) # reuse all arrays
S = float(T)
l = length(x)
y = new!(pool, Vector{S}, l) # taken from pool
z = new!(pool, Matrix{S}, l, l) # taken from pool
@. y = abs2(x)
z .= x .+ permutedims(x)
z * y # the only allocated array
end
pool = ArrayPool()
will save the relevant interim arrays and avoid allocation when necessary. If the function is called with various types, these will eventually accumulate too in pool
.