Skip to main content

Alignment

Trait Alignment 

Source
pub trait Alignment: Sealed { }
Expand description

Marker trait controlling SIMD alignment for math types.

Math types like Vector are generic over A: Alignment, which can be one of two marker types:

  • Aligned: The type may be SIMD-aligned when doing so improves performance.
  • Unaligned: The type is never SIMD-aligned.

The default type aliases (e.g., Vec2<T>, Vec3<T>) are Aligned. Aliases ending in U (e.g., Vec2U<T>, Vec3U<T>) are Unaligned.

§Background

SIMD instructions can significantly improve the performance of math code, but they require stricter memory alignment than scalar code.

For example, Vec3<f32> must be aligned to 16 bytes for it to take advantage of SIMD. This increases its size from 12 bytes to 16 bytes, meaning it has 4 bytes of padding.

In many cases padding is worthwhile for performance, but in other cases such as storing large arrays, minimizing memory usage is more important.

To support both cases, math types like Vector are generic over A: Alignment, allowing SIMD alignment to be enabled or disabled.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§