Skip to content

Commit

Permalink
Add comments to note optimization changes
Browse files Browse the repository at this point in the history
  • Loading branch information
okaneco committed Apr 19, 2020
1 parent 29e7fff commit fb1798e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
1 change: 1 addition & 0 deletions palette/src/encoding/srgb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ impl LumaStandard for Srgb {

impl TransferFn for Srgb {
fn into_linear<T: Float + FromF64>(x: T) -> T {
// Recip call shows performance benefits in benchmarks for this function
if x <= from_f64(0.04045) {
x * from_f64::<T>(12.92).recip()
} else {
Expand Down
9 changes: 9 additions & 0 deletions palette/src/matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub fn multiply_xyz<Swp: WhitePoint, Dwp: WhitePoint, T: FloatComponent>(
c: &Mat3<T>,
f: &Xyz<Swp, T>,
) -> Xyz<Dwp, T> {
// Input Mat3 is destructured to avoid panic paths
let [c0, c1, c2, c3, c4, c5, c6, c7, c8] = *c;

let x1 = c0 * f.x;
Expand All @@ -42,6 +43,8 @@ pub fn multiply_xyz_to_rgb<S: RgbSpace, T: FloatComponent>(
c: &Mat3<T>,
f: &Xyz<S::WhitePoint, T>,
) -> Rgb<Linear<S>, T> {
// Input Mat3 is destructured to avoid panic paths. red, green, and blue
// can't be extracted like in `multiply_xyz` to get a performance increase
let [c0, c1, c2, c3, c4, c5, c6, c7, c8] = *c;

Rgb {
Expand All @@ -56,6 +59,8 @@ pub fn multiply_rgb_to_xyz<S: RgbSpace, T: FloatComponent>(
c: &Mat3<T>,
f: &Rgb<Linear<S>, T>,
) -> Xyz<S::WhitePoint, T> {
// Input Mat3 is destructured to avoid panic paths. Same problem as
// `multiply_xyz_to_rgb` for extracting x, y, z
let [c0, c1, c2, c3, c4, c5, c6, c7, c8] = *c;

Xyz {
Expand All @@ -68,6 +73,7 @@ pub fn multiply_rgb_to_xyz<S: RgbSpace, T: FloatComponent>(

/// Multiply two 3x3 matrices.
pub fn multiply_3x3<T: Float>(c: &Mat3<T>, f: &Mat3<T>) -> Mat3<T> {
// Input Mat3 are destructured to avoid panic paths
let [c0, c1, c2, c3, c4, c5, c6, c7, c8] = *c;
let [f0, f1, f2, f3, f4, f5, f6, f7, f8] = *f;

Expand All @@ -88,6 +94,8 @@ pub fn multiply_3x3<T: Float>(c: &Mat3<T>, f: &Mat3<T>) -> Mat3<T> {

/// Invert a 3x3 matrix and panic if matrix is not invertible.
pub fn matrix_inverse<T: Float>(a: &Mat3<T>) -> Mat3<T> {
// This function runs fastest with assert and no destructuring. The `det`'s
// location should not be changed until benched that it's faster elsewhere
assert!(a.len() > 8);

let d0 = a[4] * a[8] - a[5] * a[7];
Expand Down Expand Up @@ -125,6 +133,7 @@ pub fn rgb_to_xyz_matrix<S: RgbSpace, T: FloatComponent>() -> Mat3<T> {
let g: Xyz<S::WhitePoint, T> = S::Primaries::green().into_color_unclamped();
let b: Xyz<S::WhitePoint, T> = S::Primaries::blue().into_color_unclamped();

// Destructuring has some performance benefits, don't change unless measured
let [t0, t1, t2, t3, t4, t5, t6, t7, t8] = mat3_from_primaries(r, g, b);

let s_matrix: Rgb<Linear<S>, T> = multiply_xyz_to_rgb(
Expand Down
1 change: 1 addition & 0 deletions palette/src/xyz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ where
Wp: WhitePoint,
{
fn from_color_unclamped(color: Lab<Wp, T>) -> Self {
// Recip call shows performance benefits in benchmarks for this function
let y = (color.l + from_f64(16.0)) * from_f64::<T>(116.0).recip();
let x = y + (color.a * from_f64::<T>(500.0).recip());
let z = y - (color.b * from_f64::<T>(200.0).recip());
Expand Down

0 comments on commit fb1798e

Please sign in to comment.