Skip to content

Commit

Permalink
Profile allocations in benchmarks
Browse files Browse the repository at this point in the history
This adds `AllocProfiler`, an allocator implementation that tracks the
number of allocations and bytes allocated during benchmarks. It can wrap
any `GlobalAlloc`, such as `mimalloc`.
  • Loading branch information
nvzqz committed Dec 11, 2023
1 parent d58e047 commit ec77e10
Show file tree
Hide file tree
Showing 23 changed files with 1,199 additions and 208 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added

- [`AllocProfiler`](https://docs.rs/divan/0.1.6/divan/struct.AllocProfiler.html)
allocator that tracks allocation counts and sizes during benchmarks.

## [0.1.5] - 2023-12-05

### Added
Expand Down
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ clap = { version = "4", default-features = false, features = ["std", "env"] }
condtype = "1.3"
regex = { package = "regex-lite", version = "0.1", default-features = false, features = ["std", "string"] }

[dev-dependencies]
mimalloc = "0.1"

[features]
default = ["wrap_help"]
help = ["clap/help"]
Expand Down
5 changes: 4 additions & 1 deletion examples/benches/collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
use std::collections::{BTreeSet, BinaryHeap, HashSet, LinkedList, VecDeque};

use divan::{black_box, Bencher};
use divan::{black_box, AllocProfiler, Bencher};

mod util;

#[global_allocator]
static ALLOC: AllocProfiler = AllocProfiler::system();

fn main() {
divan::main();
}
Expand Down
4 changes: 4 additions & 0 deletions examples/benches/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
//! ```
use digest::Digest;
use divan::AllocProfiler;

#[global_allocator]
static ALLOC: AllocProfiler = AllocProfiler::system();

fn main() {
divan::main();
Expand Down
5 changes: 4 additions & 1 deletion examples/benches/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
//! cargo bench -q -p examples --bench image --features image
//! ```
use divan::{black_box, counter::BytesCount, Bencher};
use divan::{black_box, counter::BytesCount, AllocProfiler, Bencher};
use image::{GenericImage, ImageBuffer, Rgba};

#[global_allocator]
static ALLOC: AllocProfiler = AllocProfiler::system();

fn main() {
divan::main();
}
Expand Down
5 changes: 4 additions & 1 deletion examples/benches/panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
use std::panic;

use divan::{black_box, black_box_drop};
use divan::{black_box, black_box_drop, AllocProfiler};

#[global_allocator]
static ALLOC: AllocProfiler = AllocProfiler::system();

fn main() {
// Silence panics.
Expand Down
5 changes: 4 additions & 1 deletion examples/benches/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ use std::{
hash::BuildHasher,
};

use divan::{black_box_drop, Bencher};
use divan::{black_box_drop, AllocProfiler, Bencher};
use fastrand::Rng;
use ordsearch::OrderedCollection;

#[global_allocator]
static ALLOC: AllocProfiler = AllocProfiler::system();

fn main() {
divan::Divan::from_args()
.items_count(
Expand Down
5 changes: 4 additions & 1 deletion examples/benches/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
//! cargo bench -q -p examples --bench sort
//! ```
use divan::Bencher;
use divan::{AllocProfiler, Bencher};
use rayon::slice::ParallelSliceMut;

#[global_allocator]
static ALLOC: AllocProfiler = AllocProfiler::system();

fn main() {
divan::main();
}
Expand Down
5 changes: 4 additions & 1 deletion examples/benches/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
use divan::{
black_box, black_box_drop,
counter::{BytesCount, CharsCount},
Bencher,
AllocProfiler, Bencher,
};

#[global_allocator]
static ALLOC: AllocProfiler = AllocProfiler::system();

fn main() {
divan::main();
}
Expand Down
5 changes: 4 additions & 1 deletion examples/benches/threads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ use std::{
thread::{Thread, ThreadId},
};

use divan::{black_box, black_box_drop, Bencher};
use divan::{black_box, black_box_drop, AllocProfiler, Bencher};

#[global_allocator]
static ALLOC: AllocProfiler = AllocProfiler::system();

fn main() {
divan::main();
Expand Down
5 changes: 4 additions & 1 deletion examples/benches/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
use std::time::{Instant, SystemTime};

use divan::Bencher;
use divan::{AllocProfiler, Bencher};

#[global_allocator]
static ALLOC: AllocProfiler = AllocProfiler::system();

fn main() {
divan::main();
Expand Down
Loading

0 comments on commit ec77e10

Please sign in to comment.