Skip to main content

Module tensor_pool

Module tensor_pool 

Source
Expand description

Tensor memory pool for reducing allocation pressure in hot loops. Thread-local tensor buffer pool for reusing allocations.

In tight loops (e.g., 50-layer NN forward pass), the same tensor sizes are allocated and freed every iteration. This pool caches freed buffers and returns them on the next allocation of the same size, avoiding repeated malloc/free cycles.

§Determinism

Pool reuse does NOT affect computed values — only memory addresses change. The buffer contents are always overwritten before use. Snap hashes are computed from data, not addresses, so they remain identical.

§Usage

// Get a buffer (may be recycled or freshly allocated)
let mut buf = tensor_pool::acquire(1000);
// ... fill buf with data ...
// Buffer is returned to pool when dropped (via TensorPool::recycle)

Functions§

acquire
Acquire a zeroed buffer of size f64 elements from the thread-local pool.
pool_size
Returns the current number of cached buffers in the pool (for diagnostics).
recycle
Return a buffer to the thread-local pool for future reuse.