Struct rten::TensorPool
source · pub struct TensorPool { /* private fields */ }Expand description
A pool which enables reuse of data buffers from tensors and other containers.
Reusing buffers for operator outputs, as opposed to allocating a fresh buffer from the global allocator and freeing it when no longer needed, can provide a significant performance improvement.
TensorPool implements the Alloc trait, enabling tensors to be allocated
from the pool using the various Tensor::*_in methods, eg.
[Tensor::zeros_in]. Allocation requests will be satisfied from the pool if
there is a suitable buffer available, or it will fall back to the global
allocator otherwise.
When a tensor is no longer needed, it’s buffer can be added to the pool
using pool.add(tensor.extract_buffer()), making it available for future
allocations. A more convenient method is to wrap the tensor in a [PoolRef]
smart pointer which will auto-return the tensor to the pool when dropped. A
tensor can be wrapped using tensor.auto_return(pool). The [PoolRef] smart
pointer can also be used with other container types, by implementing the
[ExtractBuffer] trait for them.
Implementations§
source§impl TensorPool
impl TensorPool
sourcepub fn new() -> TensorPool
pub fn new() -> TensorPool
Return a new, empty pool.
This is a cheap operation that does not allocate, so it can be used to create a temporary pool to pass to a function that requires one, if the caller does not have a pool otherwise available.
sourcepub fn alloc<T>(&self, capacity: usize) -> Vec<T>
pub fn alloc<T>(&self, capacity: usize) -> Vec<T>
Allocate an empty vec with a given capacity from the pool.
sourcepub fn add<T>(&self, vec: Vec<T>)
pub fn add<T>(&self, vec: Vec<T>)
Add a data buffer to the pool.
The buffer will be cleared using Vec::clear and then made available to fulfill future allocation requests.
sourcepub fn alloc_count(&self) -> usize
pub fn alloc_count(&self) -> usize
Return the total number of allocation requests.
Trait Implementations§
source§impl Alloc for TensorPool
impl Alloc for TensorPool
Auto Trait Implementations§
impl !Freeze for TensorPool
impl !RefUnwindSafe for TensorPool
impl !Send for TensorPool
impl !Sync for TensorPool
impl Unpin for TensorPool
impl UnwindSafe for TensorPool
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more