pub struct SimdStorage<T> { /* private fields */ }Expand description
SIMD-optimized storage with Copy-on-Write semantics (Phase 5)
This storage variant eliminates lock overhead for read operations while the tensor has never been written to, which is the dominant case for SIMD workloads:
- The buffer handed to
SimdStorage::newis never mutated in place, soSimdStorage::try_as_slicecan hand out a plain&[T]with no guard at all (that is the “lock-free read” this variant exists for). - The first write copies that buffer once into a private copy-on-write buffer
guarded by an
RwLock; every later write mutates it in place, so afor i in 0..n { t.set(i, v) }loop is O(n), not O(n²). - Once the copy exists, readers go through the same
RwLock, exactly likeTensorStorage::Aligned. Slices handed out earlier stay valid because the original buffer is kept alive and untouched for the storage’s lifetime.
This is what makes set/set_slice/with_slice_mut work on tensors of every
size instead of failing above the 10 KB SimdOptimized threshold.
Implementations§
Source§impl<T> SimdStorage<T>
impl<T> SimdStorage<T>
Sourcepub fn new(data: AlignedVec<T>) -> Self
pub fn new(data: AlignedVec<T>) -> Self
Create new SIMD storage from data
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Get the length of the storage
Mutation never changes the element count, so this stays lock-free.
Sourcepub fn is_mutated(&self) -> bool
pub fn is_mutated(&self) -> bool
Whether this storage has been written to (and therefore keeps its authoritative data in the guarded copy-on-write buffer).
Sourcepub fn try_as_slice(&self) -> Option<&[T]>
pub fn try_as_slice(&self) -> Option<&[T]>
Get the immutable, lock-free slice.
Returns None once the storage has been written to: after that the
authoritative data lives behind a lock and cannot be exposed as an
unguarded borrow. Callers should fall back to
SimdStorage::with_slice.
Mark as shared (for Clone)
Check if shared
Source§impl<T: Copy> SimdStorage<T>
impl<T: Copy> SimdStorage<T>
Sourcepub fn with_slice<R>(&self, f: impl FnOnce(&[T]) -> R) -> R
pub fn with_slice<R>(&self, f: impl FnOnce(&[T]) -> R) -> R
Read the storage contents, lock-free while it has never been written to.
Sourcepub fn with_slice_mut<R>(&self, f: impl FnOnce(&mut [T]) -> R) -> Result<R>
pub fn with_slice_mut<R>(&self, f: impl FnOnce(&mut [T]) -> R) -> Result<R>
Mutate the storage contents, promoting to the copy-on-write buffer on the first call.
Trait Implementations§
Auto Trait Implementations§
impl<T> !Freeze for SimdStorage<T>
impl<T> RefUnwindSafe for SimdStorage<T>where
T: RefUnwindSafe,
impl<T> Send for SimdStorage<T>where
T: Send,
impl<T> Sync for SimdStorage<T>
impl<T> Unpin for SimdStorage<T>
impl<T> UnsafeUnpin for SimdStorage<T>
impl<T> UnwindSafe for SimdStorage<T>where
T: RefUnwindSafe,
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
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