Skip to content

Commit

Permalink
Trying to black box parameters properly for iai_callgrind. (#513)
Browse files Browse the repository at this point in the history
  • Loading branch information
bitshifter authored May 26, 2024
1 parent 636add0 commit b6d1d28
Show file tree
Hide file tree
Showing 2 changed files with 197 additions and 90 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ rand_xoshiro = "0.6"
# Set a size_xx feature so that this crate compiles properly with --all-targets --all-features
rkyv = { version = "0.7", default-features = false, features = ["size_32"] }
serde_json = "1.0"
iai = { version = "0.1" }

[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
criterion = { version = "0.5", features = ["html_reports"] }
iai-callgrind = "0.11.0"

[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
wasm-bindgen-test = "0.3.0"
Expand Down
285 changes: 196 additions & 89 deletions benches/iai.rs
Original file line number Diff line number Diff line change
@@ -1,175 +1,282 @@
#![allow(clippy::all)]

use core::hint::black_box;
use iai;
use iai_callgrind::{library_benchmark, library_benchmark_group, main};

use glam::{BVec3A, Mat2, Mat3A, Mat4, Quat, Vec2, Vec3A, Vec4};

#[cfg(feature = "scalar-math")]
use glam::BVec4 as BVec4A;

#[cfg(not(feature = "scalar-math"))]
use glam::BVec4A;

#[inline]
fn mat2() -> Mat2 {
Mat2::from_cols_array(&[1.0, 2.0, 3.0, 4.0])
black_box(Mat2::from_cols_array(&[1.0, 2.0, 3.0, 4.0]))
}

#[inline]
fn mat3a() -> Mat3A {
Mat3A::from_cols_array(&[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0])
black_box(Mat3A::from_cols_array(&[
1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0,
]))
}

#[inline]
fn mat4() -> Mat4 {
Mat4::from_cols_array(&[
black_box(Mat4::from_cols_array(&[
1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0,
])
]))
}

#[inline]
fn quat() -> Quat {
Quat::IDENTITY
black_box(Quat::from_xyzw(0.0, 0.0, 0.0, 1.0))
}

#[inline]
fn vec2() -> Vec2 {
Vec2::new(1.0, 2.0)
black_box(Vec2::new(1.0, 2.0))
}

#[inline]
fn vec3a() -> Vec3A {
Vec3A::new(1.0, 2.0, 3.0)
black_box(Vec3A::new(1.0, 2.0, 3.0))
}

#[inline]
fn bvec3a() -> BVec3A {
black_box(BVec3A::new(true, false, true))
}

#[inline]
fn vec4() -> Vec4 {
Vec4::new(1.0, 2.0, 3.0, 4.0)
black_box(Vec4::new(1.0, 2.0, 3.0, 4.0))
}

#[inline]
fn bvec4a() -> BVec4A {
black_box(BVec4A::new(true, false, true, false))
}

#[library_benchmark]
#[bench::args(mat2())]
fn mat2_determinant(m: Mat2) -> f32 {
black_box(m.determinant())
}

fn iai_bench_mat2_determinant() -> f32 {
black_box(mat2()).determinant()
#[library_benchmark]
#[bench::args(mat2())]
fn mat2_inverse(m: Mat2) -> Mat2 {
black_box(m.inverse())
}

fn iai_bench_mat2_inverse() -> Mat2 {
black_box(mat2()).inverse()
#[library_benchmark]
#[bench::args(mat2())]
fn mat2_transpose(m: Mat2) -> Mat2 {
black_box(m.transpose())
}

fn iai_bench_mat2_transpose() -> Mat2 {
black_box(mat2()).transpose()
#[library_benchmark]
#[bench::args(mat2(), mat2())]
fn mat2_mul_mat2(m1: Mat2, m2: Mat2) -> Mat2 {
black_box(m1 * m2)
}

fn iai_bench_mat2_mul_mat2() -> Mat2 {
black_box(mat2()) * black_box(mat2())
#[library_benchmark]
#[bench::args(mat2(), vec2())]
fn mat2_mul_vec2(m: Mat2, v: Vec2) -> Vec2 {
black_box(m * v)
}

fn iai_bench_mat2_mul_vec2() -> Vec2 {
black_box(mat2()) * black_box(vec2())
#[library_benchmark]
#[bench::args(mat3a())]
fn mat3a_determinant(m: Mat3A) -> f32 {
black_box(m.determinant())
}

fn iai_bench_mat3a_determinant() -> f32 {
black_box(mat3a()).determinant()
#[library_benchmark]
#[bench::args(mat3a())]
fn mat3a_inverse(m: Mat3A) -> Mat3A {
black_box(m.inverse())
}

fn iai_bench_mat3a_inverse() -> Mat3A {
black_box(mat3a()).inverse()
#[library_benchmark]
#[bench::args(mat3a())]
fn mat3a_transpose(m: Mat3A) -> Mat3A {
black_box(m.transpose())
}

fn iai_bench_mat3a_transpose() -> Mat3A {
black_box(mat3a()).transpose()
#[library_benchmark]
#[bench::args(mat3a(), mat3a())]
fn mat3a_mul_mat3a(m1: Mat3A, m2: Mat3A) -> Mat3A {
black_box(m1 * m2)
}

fn iai_bench_mat3a_mul_mat3a() -> Mat3A {
black_box(mat3a()) * black_box(mat3a())
#[library_benchmark]
#[bench::args(mat3a(), vec3a())]
fn mat3a_mul_vec3a(m: Mat3A, v: Vec3A) -> Vec3A {
black_box(m * v)
}

fn iai_bench_mat3a_mul_vec3a() -> Vec3A {
black_box(mat3a()) * black_box(vec3a())
#[library_benchmark]
#[bench::args(mat4())]
fn mat4_determinant(m: Mat4) -> f32 {
black_box(m.determinant())
}

fn iai_bench_mat4_determinant() -> f32 {
black_box(mat4()).determinant()
#[library_benchmark]
#[bench::args(mat4())]
fn mat4_inverse(m: Mat4) -> Mat4 {
black_box(m.inverse())
}

fn iai_bench_mat4_inverse() -> Mat4 {
black_box(mat4()).inverse()
#[library_benchmark]
#[bench::args(mat4())]
fn mat4_transpose(m: Mat4) -> Mat4 {
black_box(m.transpose())
}

fn iai_bench_mat4_transpose() -> Mat4 {
black_box(mat4()).transpose()
#[library_benchmark]
#[bench::args(mat4(), mat4())]
fn mat4_mul_mat4(m1: Mat4, m2: Mat4) -> Mat4 {
black_box(m1 * m2)
}

fn iai_bench_mat4_mul_mat4() -> Mat4 {
black_box(mat4()) * black_box(mat4())
#[library_benchmark]
#[bench::args(mat4(), vec4())]
fn mat4_mul_vec4(m: Mat4, v: Vec4) -> Vec4 {
black_box(m * v)
}

fn iai_bench_mat4_mul_vec4() -> Vec4 {
black_box(mat4()) * black_box(vec4())
#[library_benchmark]
#[bench::args(quat(), quat())]
fn quat_mul_quat(q1: Quat, q2: Quat) -> Quat {
black_box(q1 * q2)
}

fn iai_bench_quat_mul_quat() -> Quat {
black_box(quat()) * black_box(quat())
#[library_benchmark]
#[bench::args(quat(), vec3a())]
fn quat_mul_vec3a(q: Quat, v: Vec3A) -> Vec3A {
black_box(q * v)
}

fn iai_bench_quat_mul_vec3a() -> Vec3A {
black_box(quat()) * black_box(vec3a())
#[library_benchmark]
#[bench::args(vec3a(), vec3a())]
fn vec3a_dot(v1: Vec3A, v2: Vec3A) -> f32 {
black_box(v1.dot(v2))
}

fn iai_bench_vec3a_dot() -> f32 {
black_box(vec3a()).dot(black_box(vec3a()))
#[library_benchmark]
#[bench::args(vec3a(), vec3a())]
fn vec3a_cross(v1: Vec3A, v2: Vec3A) -> Vec3A {
black_box(v1.cross(v2))
}

fn iai_bench_vec3a_cross() -> Vec3A {
black_box(vec3a()).cross(black_box(vec3a()))
#[library_benchmark]
#[bench::args(vec3a())]
fn vec3a_length(v: Vec3A) -> f32 {
black_box(v.length())
}

fn iai_bench_vec3a_length() -> f32 {
black_box(vec3a()).length()
#[library_benchmark]
#[bench::args(vec3a())]
fn vec3a_normalize(v: Vec3A) -> Vec3A {
black_box(v.normalize())
}

fn iai_bench_vec3a_normalize() -> Vec3A {
black_box(vec3a()).normalize()
#[library_benchmark]
#[bench::args(bvec3a(), vec3a(), vec3a())]
fn vec3a_select(b: BVec3A, v1: Vec3A, v2: Vec3A) -> Vec3A {
black_box(Vec3A::select(b, v1, v2))
}

fn iai_bench_vec3a_select() -> Vec3A {
Vec3A::select(black_box(BVec3A::TRUE), Vec3A::ONE, Vec3A::ZERO)
#[library_benchmark]
#[bench::args(vec4(), vec4())]
fn vec4_dot(v1: Vec4, v2: Vec4) -> f32 {
black_box(v1.dot(v2))
}

fn iai_bench_vec4_dot() -> f32 {
black_box(vec4()).dot(black_box(vec4()))
#[library_benchmark]
#[bench::args(vec4())]
fn vec4_length(v: Vec4) -> f32 {
black_box(v.length())
}

fn iai_bench_vec4_length() -> f32 {
black_box(vec4()).length()
#[library_benchmark]
#[bench::args(vec4())]
fn vec4_normalize(v: Vec4) -> Vec4 {
black_box(v.normalize())
}

fn iai_bench_vec4_normalize() -> Vec4 {
black_box(vec4()).normalize()
#[library_benchmark]
#[bench::args(bvec4a(), vec4(), vec4())]
fn vec4_select(b: BVec4A, v1: Vec4, v2: Vec4) -> Vec4 {
black_box(Vec4::select(b, v1, v2))
}

// fn iai_bench_vec4_select() -> Vec4 {
// Vec4::select(black_box(BVec4A::TRUE), Vec4::ONE, Vec4::ZERO)
// }
library_benchmark_group!(
name = bench_mat2;
benchmarks =
mat2_determinant,
mat2_inverse,
mat2_mul_mat2,
mat2_mul_vec2,
mat2_transpose,
);

library_benchmark_group!(
name = bench_mat3a;
benchmarks =
mat3a_determinant,
mat3a_inverse,
mat3a_mul_mat3a,
mat3a_mul_vec3a,
mat3a_transpose,
);

library_benchmark_group!(
name = bench_mat4;
benchmarks =
mat4_determinant,
mat4_inverse,
mat4_mul_mat4,
mat4_mul_vec4,
mat4_transpose,
);

library_benchmark_group!(
name = bench_quat;
benchmarks =
quat_mul_quat,
quat_mul_vec3a,
);

library_benchmark_group!(
name = bench_vec3a;
benchmarks =
vec3a_dot,
vec3a_cross,
vec3a_length,
vec3a_normalize,
vec3a_select,
);

library_benchmark_group!(
name = bench_vec4;
benchmarks =
vec4_dot,
vec4_length,
vec4_normalize,
vec4_select,
);

iai::main!(
iai_bench_mat2_determinant,
iai_bench_mat2_inverse,
iai_bench_mat2_mul_mat2,
iai_bench_mat2_mul_vec2,
iai_bench_mat2_transpose,
iai_bench_mat3a_determinant,
iai_bench_mat3a_inverse,
iai_bench_mat3a_mul_mat3a,
iai_bench_mat3a_mul_vec3a,
iai_bench_mat3a_transpose,
iai_bench_mat4_determinant,
iai_bench_mat4_inverse,
iai_bench_mat4_mul_mat4,
iai_bench_mat4_mul_vec4,
iai_bench_mat4_transpose,
iai_bench_quat_mul_quat,
iai_bench_quat_mul_vec3a,
iai_bench_vec3a_dot,
iai_bench_vec3a_cross,
iai_bench_vec3a_length,
iai_bench_vec3a_normalize,
iai_bench_vec3a_select,
iai_bench_vec4_dot,
iai_bench_vec4_length,
iai_bench_vec4_normalize,
// iai_bench_vec4_select,
main!(
library_benchmark_groups = bench_mat2,
bench_mat3a,
bench_mat4,
bench_quat,
bench_vec3a,
bench_vec4
);

0 comments on commit b6d1d28

Please sign in to comment.