class RemiLib::ArrayPool(T)

Overview

The ArrayPool class provides functionality to allow an array of a desired size to be 'rented' out so that buffers of the same size don't need to be constantly re-created.

These are not thread safe. If you need to use array pools with multiple threads, create one ArrayPool instance directly per thread.

Defined in:

remilib/arraypool.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(defaultVal : T) #

Creates a new ArrayPool instance.

defaultVal is the default value assigned to each index when creating NEW arrays. Existing arrays are not re-initialized with defaultVal between rentings.


[View source]

Instance Method Detail

def clear : Nil #

Removes all arrays from the pool and marks them as not rented.


[View source]
def rent(size) : Array(T) #

Requests an Array(T) of size elements from the pool. If an array of that size already exists and is not yet rented, it is returned and set as 'rented'. Otherwise a new array of that size is created, set as 'rented', and returned.

IMPORTANT: No effort is made to clear out the contents of old arrays before returning one.


[View source]
def return(array) #

Lets the array pool know that you are done using array so that it can mark it as not rented. After calling this, you should not use array anymore without first re-renting it with ArrayPool.rent.


[View source]
def withRentedArray(size, &) #

Rents out an array of size elements, then yields it to the block. The block is wrapped in a begin..ensure that will automatically return the array when the block exits.


[View source]