Skip to content

Commit

Permalink
Use hint::likely for Vec construction
Browse files Browse the repository at this point in the history
To help ensure instruction ordering on fallible functions, the
`hint::likely` intrinsic is used to hint that the `Vec` construction
is likely to succeed. This is a minor performance improvement that
allows the code-generation to assume construction will succeed on
average.
  • Loading branch information
bitwizeshift committed Sep 28, 2024
1 parent 7aabf47 commit b06bc71
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 135 deletions.
23 changes: 9 additions & 14 deletions alloy/src/math/vec/vec2.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::errors::VecError;
use crate::core::hint;
use crate::math::vec::{Vec2i, Vec2u};

use crate::cmp::{AlmostEq, Near};
Expand Down Expand Up @@ -132,10 +133,8 @@ impl Vec2 {
/// assert!(vec.is_err());
/// ```
pub const fn from_slice(slice: &[f32]) -> Result<&Self, VecError> {
if slice.len() == 2 {
// SAFETY: Vec2 is transparent, and implemented directly in terms of a
// slice of f32s. The representation is the same, and thus valid.
// This is implemented symmetrically to `OsStr`.
if hint::likely(slice.len() == 2) {
// SAFETY: slice is checked to have exactly 2 elements
Ok(unsafe { Self::from_slice_unchecked(slice) })
} else {
Err(VecError::new(2, slice.len()))
Expand Down Expand Up @@ -174,10 +173,8 @@ impl Vec2 {
/// assert!(vec.is_err());
/// ```
pub fn from_mut_slice(slice: &mut [f32]) -> Result<&mut Self, VecError> {
if slice.len() == 2 {
// SAFETY: Vec2 is transparent, and implemented directly in terms of a
// slice of f32s. The representation is the same, and thus valid.
// This is implemented symmetrically to `OsStr`.
if hint::likely(slice.len() == 2) {
// SAFETY: slice is checked to have exactly 2 elements
Ok(unsafe { Self::from_mut_slice_unchecked(slice) })
} else {
Err(VecError::new(2, slice.len()))
Expand Down Expand Up @@ -888,13 +885,11 @@ impl Vector2 {
///
/// * `slice` - the slice to read from
pub const fn from_slice(slice: &[f32]) -> Result<Self, VecError> {
if slice.len() != 2 {
Err(VecError::new(2, slice.len()))
if hint::likely(slice.len() == 2) {
// SAFETY: slice is checked to have exactly 2 elements.
Ok(unsafe { Self::from_slice_unchecked(slice) })
} else {
Ok(Self {
x: slice[0],
y: slice[1],
})
Err(VecError::new(2, slice.len()))
}
}

Expand Down
23 changes: 9 additions & 14 deletions alloy/src/math/vec/vec2i.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::errors::VecError;
use crate::core::hint;
use crate::ops::Dot;

use std::borrow::{Borrow, BorrowMut};
Expand Down Expand Up @@ -125,10 +126,8 @@ impl Vec2i {
/// assert!(vec.is_err());
/// ```
pub const fn from_slice(slice: &[i32]) -> Result<&Self, VecError> {
if slice.len() == 2 {
// SAFETY: Vec2 is transparent, and implemented directly in terms of a
// slice of i32s. The representation is the same, and thus valid.
// This is implemented symmetrically to `OsStr`.
if hint::likely(slice.len() == 2) {
// SAFETY: slice is checked to have exactly 2 elements
Ok(unsafe { Self::from_slice_unchecked(slice) })
} else {
Err(VecError::new(2, slice.len()))
Expand Down Expand Up @@ -167,10 +166,8 @@ impl Vec2i {
/// assert!(vec.is_err());
/// ```
pub fn from_mut_slice(slice: &mut [i32]) -> Result<&mut Self, VecError> {
if slice.len() == 2 {
// SAFETY: Vec2 is transparent, and implemented directly in terms of a
// slice of i32s. The representation is the same, and thus valid.
// This is implemented symmetrically to `OsStr`.
if hint::likely(slice.len() == 2) {
// SAFETY: slice is checked to have exactly 2 elements
Ok(unsafe { Self::from_mut_slice_unchecked(slice) })
} else {
Err(VecError::new(2, slice.len()))
Expand Down Expand Up @@ -698,13 +695,11 @@ impl Vector2i {
///
/// * `slice` - the slice to read from
pub const fn from_slice(slice: &[i32]) -> Result<Self, VecError> {
if slice.len() != 2 {
Err(VecError::new(2, slice.len()))
if hint::likely(slice.len() == 2) {
// SAFETY: slice is checked to have exactly 2 elements
Ok(unsafe { Self::from_slice_unchecked(slice) })
} else {
Ok(Self {
x: slice[0],
y: slice[1],
})
Err(VecError::new(2, slice.len()))
}
}

Expand Down
23 changes: 9 additions & 14 deletions alloy/src/math/vec/vec2u.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::errors::VecError;
use crate::core::hint;
use crate::ops::Dot;

use std::borrow::{Borrow, BorrowMut};
Expand Down Expand Up @@ -125,10 +126,8 @@ impl Vec2u {
/// assert!(vec.is_err());
/// ```
pub const fn from_slice(slice: &[u32]) -> Result<&Self, VecError> {
if slice.len() == 2 {
// SAFETY: Vec2 is transparent, and implemented directly in terms of a
// slice of u32s. The representation is the same, and thus valid.
// This is implemented symmetrically to `OsStr`.
if hint::likely(slice.len() == 2) {
// SAFETY: slice is checked to have exactly 2 elements
Ok(unsafe { Self::from_slice_unchecked(slice) })
} else {
Err(VecError::new(2, slice.len()))
Expand Down Expand Up @@ -167,10 +166,8 @@ impl Vec2u {
/// assert!(vec.is_err());
/// ```
pub fn from_mut_slice(slice: &mut [u32]) -> Result<&mut Self, VecError> {
if slice.len() == 2 {
// SAFETY: Vec2 is transparent, and implemented directly in terms of a
// slice of u32s. The representation is the same, and thus valid.
// This is implemented symmetrically to `OsStr`.
if hint::likely(slice.len() == 2) {
// SAFETY: slice is checked to have exactly 2 elements
Ok(unsafe { Self::from_mut_slice_unchecked(slice) })
} else {
Err(VecError::new(2, slice.len()))
Expand Down Expand Up @@ -668,13 +665,11 @@ impl Vector2u {
///
/// * `slice` - the slice to read from
pub const fn from_slice(slice: &[u32]) -> Result<Self, VecError> {
if slice.len() != 2 {
Err(VecError::new(2, slice.len()))
if hint::likely(slice.len() == 2) {
// SAFETY: slice is checked to have exactly 2 elements
Ok(unsafe { Self::from_slice_unchecked(slice) })
} else {
Ok(Self {
x: slice[0],
y: slice[1],
})
Err(VecError::new(2, slice.len()))
}
}

Expand Down
24 changes: 9 additions & 15 deletions alloy/src/math/vec/vec3.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::errors::VecError;
use crate::core::hint;
use crate::math::vec::{Vec2, Vec3i, Vec3u};

use crate::cmp::{AlmostEq, Near};
Expand Down Expand Up @@ -134,10 +135,8 @@ impl Vec3 {
/// assert!(vec.is_err());
/// ```
pub const fn from_slice(slice: &[f32]) -> Result<&Self, VecError> {
if slice.len() == 3 {
// SAFETY: Vec3 is transparent, and implemented directly in terms of a
// slice of f32s. The representation is the same, and thus valid.
// This is implemented symmetrically to `OsStr`.
if hint::likely(slice.len() == 3) {
// SAFETY: slice is checked to have exactly 3 elements
Ok(unsafe { Self::from_slice_unchecked(slice) })
} else {
Err(VecError::new(3, slice.len()))
Expand Down Expand Up @@ -176,10 +175,8 @@ impl Vec3 {
/// assert!(vec.is_err());
/// ```
pub fn from_mut_slice(slice: &mut [f32]) -> Result<&mut Self, VecError> {
if slice.len() == 3 {
// SAFETY: Vec3 is transparent, and implemented directly in terms of a
// slice of f32s. The representation is the same, and thus valid.
// This is implemented symmetrically to `OsStr`.
if hint::likely(slice.len() == 3) {
// SAFETY: slice is checked to have exactly 3 elements
Ok(unsafe { Self::from_mut_slice_unchecked(slice) })
} else {
Err(VecError::new(3, slice.len()))
Expand Down Expand Up @@ -979,14 +976,11 @@ impl Vector3 {
///
/// * `slice` - the slice to read from
pub const fn from_slice(slice: &[f32]) -> Result<Self, VecError> {
if slice.len() != 3 {
Err(VecError::new(3, slice.len()))
if hint::likely(slice.len() == 3) {
// SAFETY: slice is checked to have exactly 3 elements
Ok(unsafe { Self::from_slice_unchecked(slice) })
} else {
Ok(Self {
x: slice[0],
y: slice[1],
z: slice[3],
})
Err(VecError::new(3, slice.len()))
}
}

Expand Down
24 changes: 9 additions & 15 deletions alloy/src/math/vec/vec3i.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::errors::VecError;
use crate::core::hint;
use crate::math::vec::Vec2i;

use std::borrow::{Borrow, BorrowMut};
Expand Down Expand Up @@ -127,10 +128,8 @@ impl Vec3i {
/// assert!(vec.is_err());
/// ```
pub const fn from_slice(slice: &[i32]) -> Result<&Self, VecError> {
if slice.len() == 3 {
// SAFETY: Vec3 is transparent, and implemented directly in terms of a
// slice of i32s. The representation is the same, and thus valid.
// This is implemented symmetrically to `OsStr`.
if hint::likely(slice.len() == 3) {
// SAFETY: slice is checked to have exactly 3 elements
Ok(unsafe { Self::from_slice_unchecked(slice) })
} else {
Err(VecError::new(3, slice.len()))
Expand Down Expand Up @@ -169,10 +168,8 @@ impl Vec3i {
/// assert!(vec.is_err());
/// ```
pub fn from_mut_slice(slice: &mut [i32]) -> Result<&mut Self, VecError> {
if slice.len() == 3 {
// SAFETY: Vec3 is transparent, and implemented directly in terms of a
// slice of i32s. The representation is the same, and thus valid.
// This is implemented symmetrically to `OsStr`.
if hint::likely(slice.len() == 3) {
// SAFETY: slice is checked to have exactly 3 elements
Ok(unsafe { Self::from_mut_slice_unchecked(slice) })
} else {
Err(VecError::new(3, slice.len()))
Expand Down Expand Up @@ -793,14 +790,11 @@ impl Vector3i {
///
/// * `slice` - the slice to read from
pub const fn from_slice(slice: &[i32]) -> Result<Self, VecError> {
if slice.len() != 3 {
Err(VecError::new(3, slice.len()))
if hint::likely(slice.len() == 3) {
// SAFETY: slice is checked to have exactly 3 elements
Ok(unsafe { Self::from_slice_unchecked(slice) })
} else {
Ok(Self {
x: slice[0],
y: slice[1],
z: slice[3],
})
Err(VecError::new(3, slice.len()))
}
}

Expand Down
24 changes: 9 additions & 15 deletions alloy/src/math/vec/vec3u.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::errors::VecError;
use crate::core::hint;
use crate::math::vec::Vec2u;

use std::borrow::{Borrow, BorrowMut};
Expand Down Expand Up @@ -126,10 +127,8 @@ impl Vec3u {
/// assert!(vec.is_err());
/// ```
pub const fn from_slice(slice: &[u32]) -> Result<&Self, VecError> {
if slice.len() == 3 {
// SAFETY: Vec3 is transparent, and implemented directly in terms of a
// slice of u32s. The representation is the same, and thus valid.
// This is implemented symmetrically to `OsStr`.
if hint::likely(slice.len() == 3) {
// SAFETY: slice is checked to have 3 elements.
Ok(unsafe { Self::from_slice_unchecked(slice) })
} else {
Err(VecError::new(3, slice.len()))
Expand Down Expand Up @@ -168,10 +167,8 @@ impl Vec3u {
/// assert!(vec.is_err());
/// ```
pub fn from_mut_slice(slice: &mut [u32]) -> Result<&mut Self, VecError> {
if slice.len() == 3 {
// SAFETY: Vec3 is transparent, and implemented directly in terms of a
// slice of u32s. The representation is the same, and thus valid.
// This is implemented symmetrically to `OsStr`.
if hint::likely(slice.len() == 3) {
// SAFETY: slice is checked to have 3 elements
Ok(unsafe { Self::from_mut_slice_unchecked(slice) })
} else {
Err(VecError::new(3, slice.len()))
Expand Down Expand Up @@ -753,14 +750,11 @@ impl Vector3u {
///
/// * `slice` - the slice to read from
pub const fn from_slice(slice: &[u32]) -> Result<Self, VecError> {
if slice.len() != 3 {
Err(VecError::new(3, slice.len()))
if hint::likely(slice.len() == 3) {
// SAFETY: slice is checked to have 3 elements.
Ok(unsafe { Self::from_slice_unchecked(slice) })
} else {
Ok(Self {
x: slice[0],
y: slice[1],
z: slice[3],
})
Err(VecError::new(3, slice.len()))
}
}

Expand Down
25 changes: 9 additions & 16 deletions alloy/src/math/vec/vec4.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::errors::VecError;
use crate::core::hint;
use crate::math::vec::{Vec2, Vec3, Vec4i, Vec4u};

use crate::cmp::{AlmostEq, Near};
Expand Down Expand Up @@ -138,10 +139,8 @@ impl Vec4 {
/// assert!(vec.is_err());
/// ```
pub const fn from_slice(slice: &[f32]) -> Result<&Self, VecError> {
if slice.len() == 4 {
// SAFETY: Vec4 is transparent, and implemented directly in terms of a
// slice of f32s. The representation is the same, and thus valid.
// This is implemented symmetrically to `OsStr`.
if hint::likely(slice.len() == 4) {
// SAFETY: slice is checked to have exactly 4 elements
Ok(unsafe { Self::from_slice_unchecked(slice) })
} else {
Err(VecError::new(4, slice.len()))
Expand Down Expand Up @@ -180,10 +179,8 @@ impl Vec4 {
/// assert!(vec.is_err());
/// ```
pub fn from_mut_slice(slice: &mut [f32]) -> Result<&mut Self, VecError> {
if slice.len() == 4 {
// SAFETY: Vec4 is transparent, and implemented directly in terms of a
// slice of f32s. The representation is the same, and thus valid.
// This is implemented symmetrically to `OsStr`.
if hint::likely(slice.len() == 4) {
// SAFETY: slice is checked to have exactly 4 elements
Ok(unsafe { Self::from_mut_slice_unchecked(slice) })
} else {
Err(VecError::new(4, slice.len()))
Expand Down Expand Up @@ -1146,15 +1143,11 @@ impl Vector4 {
///
/// * `slice` - the slice to read from
pub const fn from_slice(slice: &[f32]) -> Result<Self, VecError> {
if slice.len() != 4 {
Err(VecError::new(4, slice.len()))
if hint::likely(slice.len() == 4) {
// SAFETY: slice is checked to have exactly 4 elements
Ok(unsafe { Self::from_slice_unchecked(slice) })
} else {
Ok(Self {
x: slice[0],
y: slice[1],
z: slice[3],
w: slice[4],
})
Err(VecError::new(4, slice.len()))
}
}

Expand Down
Loading

0 comments on commit b06bc71

Please sign in to comment.