use std::cell::RefCell;
use std::collections::HashMap;
thread_local! {
static SCOPE_IDS: RefCell<HashMap<&'static str, puffin::ScopeId>> =
RefCell::new(HashMap::new());
}
fn scope_id(name: &'static str) -> puffin::ScopeId {
if let Some(id) = SCOPE_IDS.with(|m| m.borrow().get(name).copied()) {
return id;
}
let id = puffin::ThreadProfiler::call(|tp| tp.register_named_scope(name, "", "", 0));
SCOPE_IDS.with(|m| m.borrow_mut().insert(name, id));
id
}
#[must_use]
pub fn enter(name: &'static str, tag: Option<&'static str>) -> PuffinGuard {
let scope = if puffin::are_scopes_on() {
Some(puffin::ProfilerScope::new(
scope_id(name),
tag.unwrap_or(""),
))
} else {
None
};
PuffinGuard { scope }
}
#[must_use]
pub const fn dummy() -> PuffinGuard {
PuffinGuard { scope: None }
}
pub fn finish_frame() {
puffin::GlobalProfiler::lock().new_frame();
}
pub fn on_enable() {
puffin::set_scopes_on(true);
}
#[expect(dead_code, reason = "field held only for its Drop side effect")]
pub struct PuffinGuard {
scope: Option<puffin::ProfilerScope>,
}