nnethercote / dhat-rs

Heap profiling and ad hoc profiling for Rust programs.
Apache License 2.0
713 stars 36 forks source link

Check if profiler is enabled / HeapStats without panic #37

Open Dav1dde opened 1 month ago

Dav1dde commented 1 month ago

I am profiling a bigger application and switching relatively often between profiled/non-profiled and have a working checkout of the code with dhat profiling inserted into many submodules of a bigger workspace. I created a macro to print the current memory stats across a bunch of different functions and lines of code. In the macro I get the heap stats via HeapStats::get(), but this is problematic and fails if I don't have the profiler enabled. Since this macro is for testing copy pasted in a few crates it's quite annoying to toggle it via feature flag (the feature flag would need to be passed down from the binary crate) and it's also annoying to comment out the code everytime I toggle the profiling.

It would be nice if there was a way to check if profiling is enabled, or if HeapStats::get had a variant which returned Option<HeapStats> or Result<HeapStats, SomeError>.

I am not sure what your preferred API surface for this is, if you have any ideas/recommendations I can draft the PR for it.