Skip to main content

SimdStorage

Struct SimdStorage 

Source
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::new is never mutated in place, so SimdStorage::try_as_slice can 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 a for 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 like TensorStorage::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>

Source

pub fn new(data: AlignedVec<T>) -> Self

Create new SIMD storage from data

Source

pub fn len(&self) -> usize

Get the length of the storage

Mutation never changes the element count, so this stays lock-free.

Source

pub fn is_empty(&self) -> bool

Check if storage is empty

Source

pub fn capacity(&self) -> usize

Get the capacity

Source

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).

Source

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.

Source

pub fn mark_shared(&self)

Mark as shared (for Clone)

Source

pub fn is_shared(&self) -> bool

Check if shared

Source§

impl<T: Copy> SimdStorage<T>

Source

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.

Source

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.

Source

pub fn to_vec(&self) -> Vec<T>

Convert to Vec (copying data)

Trait Implementations§

Source§

impl<T> Debug for SimdStorage<T>

Available on crate feature simd only.
Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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>
where T: Sync + Send,

§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V