Skip to content
Permalink
Browse files
Port histogram/history queue to Rust
The histogram implementation needs some optimizations while the queue
implementation is considerably faster as the C one for some operations
and equally fast for others.
  • Loading branch information
sdroege committed Aug 9, 2020
1 parent ab9d6bb commit 546f1a7a905befb1f2215c3ad83dfb93fd93e6a1
Show file tree
Hide file tree
Showing 10 changed files with 1,163 additions and 451 deletions.
@@ -13,6 +13,7 @@ readme = "README.md"

[dependencies]
bitflags = "1.0"
lazy_static = "1.0"

[build-dependencies]
cc = "1.0"
@@ -21,6 +22,9 @@ cc = "1.0"
criterion = "0.3"
float-cmp = "0.8"
ebur128-c = { package = "ebur128", version = "=0.1.1" }
quickcheck = "0.9"
quickcheck_macros = "0.9"
rand = "0.7"

[features]
internal-tests = []
@@ -33,6 +37,10 @@ harness = false
name = "true_peak"
harness = false

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

[[bench]]
name = "ebur128"
harness = false
@@ -0,0 +1,274 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};

pub fn criterion_benchmark(c: &mut Criterion) {
#[cfg(feature = "internal-tests")]
{
let mut energies = vec![0.0; 1_000_000];
for (i, e) in energies.iter_mut().enumerate() {
*e = f64::powf(10.0, ((i % 1000) as f64 / 10.0 - 69.95 + 0.691) / 10.0);
}

let mut group = c.benchmark_group("history add: 1M Histogram");
group.bench_function("C", |b| {
b.iter(|| {
use ebur128::history;

unsafe {
let hist = history::history_create_c(1, 100_000);

for e in black_box(&energies) {
history::history_add_c(hist, *e);
}

history::history_destroy_c(hist);
}
})
});
group.bench_function("Rust", |b| {
b.iter(|| {
use ebur128::history;

let mut hist = history::History::new(true, 100_000);
for e in black_box(&energies) {
hist.add(*e);
}
})
});
group.finish();

let mut group = c.benchmark_group("history add: 1M Queue");
group.bench_function("C", |b| {
b.iter(|| {
use ebur128::history;

unsafe {
let hist = history::history_create_c(0, 100_000);

for e in black_box(&energies) {
history::history_add_c(hist, *e);
}

history::history_destroy_c(hist);
}
})
});
group.bench_function("Rust", |b| {
b.iter(|| {
use ebur128::history;

let mut hist = history::History::new(false, 100_000);
for e in black_box(&energies) {
hist.add(*e);
}
})
});
group.finish();

let mut group = c.benchmark_group("history gated loudness: 1M Histogram");
unsafe {
use ebur128::history;

let hist = history::history_create_c(1, 100_000);

for e in black_box(&energies) {
history::history_add_c(hist, *e);
}

group.bench_function("C", |b| {
b.iter(|| {
black_box(history::history_gated_loudness_c(hist));
})
});

history::history_destroy_c(hist);
}
{
use ebur128::history;
let mut hist = history::History::new(true, 100_000);

for e in black_box(&energies) {
hist.add(*e);
}

group.bench_function("Rust", |b| {
b.iter(|| {
black_box(hist.gated_loudness());
})
});
}
group.finish();

let mut group = c.benchmark_group("history gated loudness: 1M Queue");
unsafe {
use ebur128::history;

let hist = history::history_create_c(0, 100_000);

for e in black_box(&energies) {
history::history_add_c(hist, *e);
}

group.bench_function("C", |b| {
b.iter(|| {
black_box(history::history_gated_loudness_c(hist));
})
});

history::history_destroy_c(hist);
}
{
use ebur128::history;
let mut hist = history::History::new(false, 100_000);

for e in black_box(&energies) {
hist.add(*e);
}

group.bench_function("Rust", |b| {
b.iter(|| {
black_box(hist.gated_loudness());
})
});
}
group.finish();

let mut group = c.benchmark_group("history relative threshold: 1M Histogram");
unsafe {
use ebur128::history;

let hist = history::history_create_c(1, 100_000);

for e in black_box(&energies) {
history::history_add_c(hist, *e);
}

group.bench_function("C", |b| {
b.iter(|| {
black_box(history::history_relative_threshold_c(hist));
})
});

history::history_destroy_c(hist);
}
{
use ebur128::history;
let mut hist = history::History::new(true, 100_000);

for e in black_box(&energies) {
hist.add(*e);
}

group.bench_function("Rust", |b| {
b.iter(|| {
black_box(hist.relative_threshold());
})
});
}
group.finish();

let mut group = c.benchmark_group("history relative threshold: 1M Queue");
unsafe {
use ebur128::history;

let hist = history::history_create_c(0, 100_000);

for e in black_box(&energies) {
history::history_add_c(hist, *e);
}

group.bench_function("C", |b| {
b.iter(|| {
black_box(history::history_relative_threshold_c(hist));
})
});

history::history_destroy_c(hist);
}
{
use ebur128::history;
let mut hist = history::History::new(false, 100_000);

for e in black_box(&energies) {
hist.add(*e);
}

group.bench_function("Rust", |b| {
b.iter(|| {
black_box(hist.relative_threshold());
})
});
}
group.finish();

let mut group = c.benchmark_group("history loudness range: 1M Histogram");
unsafe {
use ebur128::history;

let hist = history::history_create_c(1, 100_000);

for e in black_box(&energies) {
history::history_add_c(hist, *e);
}

group.bench_function("C", |b| {
b.iter(|| {
black_box(history::history_loudness_range_c(hist));
})
});

history::history_destroy_c(hist);
}
{
use ebur128::history;
let mut hist = history::History::new(true, 100_000);

for e in black_box(&energies) {
hist.add(*e);
}

group.bench_function("Rust", |b| {
b.iter(|| {
black_box(hist.loudness_range());
})
});
}
group.finish();

let mut group = c.benchmark_group("history loudness range: 1M Queue");
unsafe {
use ebur128::history;

let hist = history::history_create_c(0, 100_000);

for e in black_box(&energies) {
history::history_add_c(hist, *e);
}

group.bench_function("C", |b| {
b.iter(|| {
black_box(history::history_loudness_range_c(hist));
})
});

history::history_destroy_c(hist);
}
{
use ebur128::history;
let mut hist = history::History::new(false, 100_000);

for e in black_box(&energies) {
hist.add(*e);
}

group.bench_function("Rust", |b| {
b.iter(|| {
black_box(hist.loudness_range());
})
});
}
group.finish();
}
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
@@ -6,6 +6,7 @@ fn main() {
b.compiler("clang");
b.file("src/c/tests/interp.c");
b.file("src/c/tests/true_peak.c");
b.file("src/c/tests/history.c");
}

b.file("src/c/ebur128.c");

0 comments on commit 546f1a7

Please sign in to comment.