Skip to content

Commit

Permalink
Fix overhead measures based on real-life data
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Aug 8, 2024
1 parent 3e569df commit 15e131c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn my_function() {
}
```

The Puffin macros write data to a thread-local data stream. When the outermost scope of a thread is closed, the data stream is sent to a global profiler collector. The scopes are pretty light-weight, costing around 60 ns on an M1 MacBook Pro.
The Puffin macros write data to a thread-local data stream. When the outermost scope of a thread is closed, the data stream is sent to a global profiler collector. The scopes are pretty light-weight, costing around 50-200 ns.

You have to turn on the profiler before it captures any data with a call to `puffin::set_scopes_on(true);`. When the profiler is off the profiler scope macros only has an overhead of 1 ns on an M1 MacBook Pro (plus some stack space).

Expand Down
13 changes: 10 additions & 3 deletions puffin_egui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,10 +533,17 @@ impl ProfilerUi {
let frame = frames.frames.first();

let num_scopes = frame.meta.num_scopes;
let overhead_ms = num_scopes as f64 * 50.0e-6; // Around 50 ns per scope on an Apple M1.
if overhead_ms > 0.5 {
let realistic_ns_overhead = 200.0; // Micro-benchmarks puts it at 50ns, but real-life tests show it's much higher.
let overhead_ms = num_scopes as f64 * 1.0e-6 * realistic_ns_overhead;
if overhead_ms > 1.0 {
let overhead = if overhead_ms < 2.0 {
format!("{:.1} ms", overhead_ms)
} else {
format!("{:.0} ms", overhead_ms)
};

let text = format!(
"There are {num_scopes} scopes in this frame, which adds up to ~{overhead_ms:.1} ms of overhead.\n\
"There are {num_scopes} scopes in this frame, which adds around ~{overhead} of overhead.\n\
Use the Table view to find which scopes are triggered often, and either remove them or replace them with profile_function_if!()");

ui.label(egui::RichText::new(text).color(ui.visuals().warn_fg_color));
Expand Down

0 comments on commit 15e131c

Please sign in to comment.