11 stable releases

3.0.1 Mar 8, 2022
3.0.0 Nov 24, 2021
2.0.4 Jan 28, 2021
1.0.3 Jan 16, 2021

#18 in Profiling

Download history 30693/week @ 2023-12-12 27642/week @ 2023-12-19 19393/week @ 2023-12-26 32504/week @ 2024-01-02 38781/week @ 2024-01-09 39708/week @ 2024-01-16 41220/week @ 2024-01-23 44956/week @ 2024-01-30 46965/week @ 2024-02-06 49245/week @ 2024-02-13 46631/week @ 2024-02-20 48160/week @ 2024-02-27 53642/week @ 2024-03-05 52412/week @ 2024-03-12 53632/week @ 2024-03-19 43360/week @ 2024-03-26

210,395 downloads per month
Used in 238 crates (12 directly)

MIT/Apache

13KB
256 lines

A library to quickly get the live/total/max counts of allocated instances.

#[derive(Default)]
struct Widget {
  _c: countme::Count<Self>,
  ...
}

let w1 = Widget::default();
let w2 = Widget::default();
let w3 = Widget::default();
drop(w1);

let counts = countme::get::<Widget>();
assert_eq!(counts.live, 2);
assert_eq!(counts.max_live, 3);
assert_eq!(counts.total, 3);

eprintln!("{}", countme::get_all());

lib.rs:

A library to quickly get the live/total/max counts of allocated instances.

Example


#[derive(Default)]
struct Widget {
  _c: countme::Count<Self>,
}

countme::enable(true);

let w1 = Widget::default();
let w2 = Widget::default();
let w3 = Widget::default();
drop(w1);

let counts = countme::get::<Widget>();
assert_eq!(counts.live, 2);
assert_eq!(counts.max_live, 3);
assert_eq!(counts.total, 3);

eprintln!("{}", countme::get_all());

Configuration

By default, the implementation compiles to no-ops. Therefore it is possible to include Count fields into library types.

The enable cargo feature ungates the counting code. The feature can be enabled anywhere in the crate graph.

At run-time, the counters are controlled with enable function. Counting is enabled by default if print_at_exit feature is enabled. Otherwise counting is disabled by default. Call enable(true) early in main to enable:

fn main() {
    countme::enable(std::env::var("COUNTME").is_ok());
}

The code is optimized for the case where counting is not enabled at runtime (counting is a relaxed load and a branch to a function call).

The print_at_exit Cargo feature uses atexit call to print final counts before the program exits (it also enables counting at runtime). Use it only when you can't modify the main to print counts -- atexit is not guaranteed to work with rust's runtime.

Dependencies

~0–6.5MB