Skip to content

Commit

Permalink
Merge pull request #195 from Walther/2024-02-03
Browse files Browse the repository at this point in the history
2024-02-03 improvements
  • Loading branch information
Walther authored Feb 4, 2024
2 parents 8b97384 + 9a51cdb commit e4df3c6
Show file tree
Hide file tree
Showing 11 changed files with 353 additions and 67 deletions.
8 changes: 8 additions & 0 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,11 @@ all-scenes:
profile *ARGS:
cargo build --profile profiling; \
samply record -- ./target/profiling/clovers-cli {{ARGS}}

# Run all tests
test:
cargo nextest run --cargo-quiet

# Run all benchmarks
bench:
cargo bench --quiet
19 changes: 18 additions & 1 deletion clovers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,25 @@ stl_io = { version = "0.7.0", optional = true }
tracing = { version = "0.1.40", optional = true }

[dev-dependencies]
criterion = "0.4.0"
divan = "0.1.11"
proptest = "1"

[[bench]]
name = "random"
harness = false

[[bench]]
name = "interval"
harness = false

[[bench]]
name = "aabb"
harness = false

[[bench]]
name = "wavelength"
harness = false

[[bench]]
name = "spectrum"
harness = false
88 changes: 88 additions & 0 deletions clovers/benches/aabb.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
use std::f32::{INFINITY, NEG_INFINITY};

use clovers::interval::Interval;
use clovers::ray::Ray;
use clovers::wavelength::random_wavelength;
use clovers::{aabb::*, Vec3};
use divan::black_box;
use rand::rngs::SmallRng;
use rand::{Rng, SeedableRng};

fn main() {
divan::main();
}

#[divan::bench]
fn new(bencher: divan::Bencher) {
bencher
.with_inputs(|| {
let mut rng = SmallRng::from_entropy();
random_intervals(&mut rng)
})
.bench_values(|(ab, cd, ef)| black_box(AABB::new(ab, cd, ef)))
}

#[divan::bench]
fn hit(bencher: divan::Bencher) {
bencher
.with_inputs(random_aabb_and_ray)
.bench_values(|(aabb, ray)| black_box(aabb.hit(&ray, NEG_INFINITY, INFINITY)))
}

#[divan::bench]
fn hit_old(bencher: divan::Bencher) {
bencher
.with_inputs(random_aabb_and_ray)
.bench_values(|(aabb, ray)| {
#[allow(deprecated)]
black_box(aabb.hit_old(&ray, NEG_INFINITY, INFINITY))
})
}

#[divan::bench]
fn hit_new(bencher: divan::Bencher) {
bencher
.with_inputs(random_aabb_and_ray)
.bench_values(|(aabb, ray)| {
#[allow(deprecated)]
black_box(aabb.hit_new(&ray, NEG_INFINITY, INFINITY))
})
}

// Helper functions

fn random_intervals(rng: &mut SmallRng) -> (Interval, Interval, Interval) {
let (a, b, c, d, e, f) = black_box((
rng.gen(),
rng.gen(),
rng.gen(),
rng.gen(),
rng.gen(),
rng.gen(),
));
let ab = Interval::new(a, b);
let cd = Interval::new(c, d);
let ef = Interval::new(e, f);
(ab, cd, ef)
}

fn random_aabb(rng: &mut SmallRng) -> AABB {
let (ab, cd, ef) = random_intervals(rng);
black_box(AABB::new(ab, cd, ef))
}

fn random_ray(rng: &mut SmallRng) -> Ray {
black_box(Ray {
origin: Vec3::new(0.0, 0.0, 0.0),
direction: Vec3::new(rng.gen(), rng.gen(), rng.gen()),
time: rng.gen(),
wavelength: random_wavelength(rng),
})
}

fn random_aabb_and_ray() -> (AABB, Ray) {
let mut rng = SmallRng::from_entropy();
let aabb = black_box(random_aabb(&mut rng));
let ray = black_box(random_ray(&mut rng));
(aabb, ray)
}
59 changes: 59 additions & 0 deletions clovers/benches/interval.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use clovers::interval::*;
use divan::black_box;
use rand::rngs::SmallRng;
use rand::{Rng, SeedableRng};

fn main() {
divan::main();
}

#[divan::bench]
fn new(bencher: divan::Bencher) {
bencher
.with_inputs(|| {
let mut rng = SmallRng::from_entropy();
(rng.gen(), rng.gen())
})
.bench_values(|(a, b)| black_box(Interval::new(a, b)))
}

#[divan::bench]
fn new_from_intervals(bencher: divan::Bencher) {
bencher
.with_inputs(|| {
let mut rng = SmallRng::from_entropy();
let ab = random_interval(&mut rng);
let cd = random_interval(&mut rng);
(ab, cd)
})
.bench_values(|(ab, cd)| black_box(Interval::new_from_intervals(ab, cd)))
}

#[divan::bench]
fn expand(bencher: divan::Bencher) {
bencher
.with_inputs(|| {
let mut rng = SmallRng::from_entropy();
let ab = random_interval(&mut rng);
let delta = rng.gen();
(ab, delta)
})
.bench_values(|(ab, delta)| black_box(ab.expand(delta)))
}

#[divan::bench]
fn size(bencher: divan::Bencher) {
bencher
.with_inputs(|| {
let mut rng = SmallRng::from_entropy();
random_interval(&mut rng)
})
.bench_values(|ab| black_box(ab.size()))
}

// Helper functions

fn random_interval(rng: &mut SmallRng) -> Interval {
let (a, b) = (rng.gen(), rng.gen());
Interval::new(a, b)
}
54 changes: 30 additions & 24 deletions clovers/benches/random.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,39 @@
use clovers::{random::*, Vec3};
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use divan::black_box;
use rand::rngs::SmallRng;
#[allow(unused_imports)]
use rand::SeedableRng;

pub fn criterion_benchmark(c: &mut Criterion) {
let mut rng = SmallRng::from_entropy();

c.bench_function("random in unit sphere", |b| {
b.iter(|| random_unit_vector(black_box(&mut rng)))
});

c.bench_function("random in unit disk", |b| {
b.iter(|| random_in_unit_disk(black_box(&mut rng)))
});
fn main() {
divan::main();
}

c.bench_function("random unit vector", |b| {
b.iter(|| random_unit_vector(black_box(&mut rng)))
});
#[divan::bench]
fn unit_vector(bencher: divan::Bencher) {
bencher
.with_inputs(SmallRng::from_entropy)
.bench_values(|mut rng| random_unit_vector(black_box(&mut rng)))
}

c.bench_function("random cosine direction", |b| {
b.iter(|| random_cosine_direction(black_box(&mut rng)))
});
#[divan::bench]
fn unit_disk(bencher: divan::Bencher) {
bencher
.with_inputs(SmallRng::from_entropy)
.bench_values(|mut rng| random_in_unit_disk(black_box(&mut rng)))
}

let normal = Vec3::new(1.0, 0.0, 0.0);
c.bench_function("random in hemisphere", |b| {
b.iter(|| random_on_hemisphere(normal, black_box(&mut rng)))
});
#[divan::bench]
fn cosine_direction(bencher: divan::Bencher) {
bencher
.with_inputs(SmallRng::from_entropy)
.bench_values(|mut rng| random_cosine_direction(black_box(&mut rng)))
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
#[divan::bench]
fn hemisphere(bencher: divan::Bencher) {
bencher
.with_inputs(SmallRng::from_entropy)
.bench_values(|mut rng| {
let normal = Vec3::new(1.0, 0.0, 0.0);
random_on_hemisphere(normal, black_box(&mut rng))
})
}
23 changes: 23 additions & 0 deletions clovers/benches/spectrum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use clovers::spectrum::*;
use clovers::wavelength::*;
use divan::black_box;
use palette::white_point::E;
use palette::Xyz;
use rand::rngs::SmallRng;
use rand::SeedableRng;

fn main() {
divan::main();
}

#[divan::bench]
fn xyz_to_p(bencher: divan::Bencher) {
bencher
.with_inputs(|| {
let mut rng = SmallRng::from_entropy();
let wave = random_wavelength(&mut rng);
let xyz: Xyz<E> = Xyz::new(1.0, 1.0, 1.0);
(wave, xyz)
})
.bench_values(|(wave, xyz)| black_box(spectrum_xyz_to_p(wave, xyz)))
}
35 changes: 35 additions & 0 deletions clovers/benches/wavelength.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use clovers::wavelength::*;
use divan::black_box;
use rand::rngs::SmallRng;
use rand::SeedableRng;

fn main() {
divan::main();
}

#[divan::bench]
fn random(bencher: divan::Bencher) {
bencher
.with_inputs(SmallRng::from_entropy)
.bench_values(|mut rng| black_box(random_wavelength(&mut rng)))
}

#[divan::bench]
fn rotate(bencher: divan::Bencher) {
bencher
.with_inputs(|| {
let mut rng = SmallRng::from_entropy();
random_wavelength(&mut rng)
})
.bench_values(|wave| black_box(rotate_wavelength(wave)))
}

#[divan::bench]
fn into_xyz(bencher: divan::Bencher) {
bencher
.with_inputs(|| {
let mut rng = SmallRng::from_entropy();
random_wavelength(&mut rng)
})
.bench_values(|wave| black_box(wavelength_into_xyz(wave)))
}
Loading

0 comments on commit e4df3c6

Please sign in to comment.