//! Named wall-clock spans, summed per thread: how a measurement says where a//! fresh copy's time goes on a box where `perf` is not allowed. A span costs//! two clock reads; nothing reads the sums but a measurement.use std::cell::RefCell;use std::collections::BTreeMap;use std::time::{Duration, Instant};thread_local! {static SUMS: RefCell<BTreeMap<&'static str, (Duration, u64)>> = const { RefCell::new(BTreeMap::new()) };}/// Times `name` until it is dropped.pubstructSpan { name: &'static str, began: Instant,}/// Start the span `name`; its time is added when the answer is dropped.pubfn span(name: &'static str) -> Span {Span { name, began: Instant::now() }}implDropforSpan {fn drop(&mutself) {let took = self.began.elapsed(); SUMS.with(|sums| {letmut sums = sums.borrow_mut();let entry = sums.entry(self.name).or_default(); entry.0 += took; entry.1 += 1; }); }}/// This thread's sums since the last take, longest first, and start again.pubfn take() -> Vec<(&'static str, Duration, u64)> {letmut sums: Vec<_> = SUMS.with(|sums| std::mem::take(&mut *sums.borrow_mut())) .into_iter() .map(|(name, (took, count))| (name, took, count)) .collect(); sums.sort_by(|a, b| b.1.cmp(&a.1)); sums}