pub enum TensorStorage<T: TensorElement> {
InMemory(Arc<RwLock<Vec<T>>>),
MemoryMapped(Arc<RwLock<MemoryMappedStorage<T>>>),
}Expand description
Storage abstraction for tensor data
Variants§
InMemory(Arc<RwLock<Vec<T>>>)
In-memory storage for smaller tensors
MemoryMapped(Arc<RwLock<MemoryMappedStorage<T>>>)
Memory-mapped storage for large tensors
Implementations§
Source§impl<T: TensorElement + Copy> TensorStorage<T>
impl<T: TensorElement + Copy> TensorStorage<T>
Sourcepub fn memory_mapped(data: Vec<T>, file_path: Option<PathBuf>) -> Result<Self>
pub fn memory_mapped(data: Vec<T>, file_path: Option<PathBuf>) -> Result<Self>
Create memory-mapped storage
Sourcepub fn fast_result(data: Vec<T>) -> Self
pub fn fast_result(data: Vec<T>) -> Self
🚀 Phase 7: Create fast result storage (skips alignment copy)
For SIMD operation results where we already have the data in a Vec, uses InMemory storage to avoid the ~10µs alignment copy overhead.
§Performance
- Skips AlignedVec copy (saves ~10µs for 50K elements)
- Uses InMemory storage (has RwLock but we just created it)
- Optimal for result tensors that won’t be immediately used in SIMD ops
Sourcepub fn create_optimal(data: Vec<T>) -> Result<Self>
pub fn create_optimal(data: Vec<T>) -> Result<Self>
Create storage automatically based on size and performance characteristics
Storage Selection Strategy:
- Very large (>1GB): Memory-mapped for virtual memory efficiency
- Large (>10KB, SIMD enabled): SimdOptimized (lock-free reads)
- Medium (>1KB, SIMD enabled): Aligned storage for SIMD alignment
- Small (<1KB): In-memory with RwLock
Sourcepub fn get_slice(&self, start: usize, len: usize) -> Result<Vec<T>>where
T: Copy,
pub fn get_slice(&self, start: usize, len: usize) -> Result<Vec<T>>where
T: Copy,
Get multiple elements
Sourcepub fn set_slice(&self, start: usize, values: &[T]) -> Result<()>where
T: Copy,
pub fn set_slice(&self, start: usize, values: &[T]) -> Result<()>where
T: Copy,
Set multiple elements
Sourcepub fn to_vec(&self) -> Result<Vec<T>>where
T: Copy,
pub fn to_vec(&self) -> Result<Vec<T>>where
T: Copy,
Convert to vector (useful for small tensors or debugging)
Sourcepub fn storage_type(&self) -> &'static str
pub fn storage_type(&self) -> &'static str
Get storage type information
Sourcepub fn memory_usage(&self) -> usize
pub fn memory_usage(&self) -> usize
Get estimated memory usage in bytes
Sourcepub fn with_slice<R, F>(&self, f: F) -> Result<R>
pub fn with_slice<R, F>(&self, f: F) -> Result<R>
Execute a function with immutable access to data slice (zero-copy within scope)
This enables zero-copy SIMD operations by providing direct &[T] access
within the closure scope while the lock is held.
§Arguments
f- Closure that receives&[T]and returnsResult<R>
§Returns
Result from the closure
§Performance
- Zero allocations for in-memory and aligned storage
- Converts memory-mapped storage to Vec (one allocation)
§Examples
storage.with_slice(|data| {
// Direct SIMD access to data
f32::simd_add(&data, &other_data)
})?;Sourcepub fn with_slice_mut<R, F>(&self, f: F) -> Result<R>
pub fn with_slice_mut<R, F>(&self, f: F) -> Result<R>
Execute a function with mutable access to data slice (zero-copy within scope)
This enables zero-copy in-place operations by providing direct &mut [T] access
within the closure scope while the lock is held.
§Arguments
f- Closure that receives&mut [T]and returnsResult<R>
§Returns
Result from the closure
§Performance
- Zero allocations for in-memory storage
- Aligned storage not yet supported (returns error)
- Memory-mapped storage not supported for mutable access (returns error)
§Examples
storage.with_slice_mut(|data| {
// In-place SIMD operation
f32::simd_add_inplace(data, &other_data)
})?;Trait Implementations§
Source§impl<T: TensorElement> Clone for TensorStorage<T>
impl<T: TensorElement> Clone for TensorStorage<T>
Auto Trait Implementations§
impl<T> Freeze for TensorStorage<T>
impl<T> RefUnwindSafe for TensorStorage<T>
impl<T> Send for TensorStorage<T>
impl<T> Sync for TensorStorage<T>
impl<T> Unpin for TensorStorage<T>
impl<T> UnsafeUnpin for TensorStorage<T>
impl<T> UnwindSafe for TensorStorage<T>
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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