use super::*;
#[must_use]
pub const fn allocation_tracking_active() -> bool {
cfg!(feature = "allocation-tracking")
}
#[must_use]
pub fn snapshot_raw_alloc_counters() -> RawAllocCounters {
RawAllocCounters::new(
ALLOC_COUNT.load(Ordering::Relaxed),
FREE_COUNT.load(Ordering::Relaxed),
BYTES_ALLOCATED.load(Ordering::Relaxed),
BYTES_FREED.load(Ordering::Relaxed),
LIVE_BYTES_DELTA.load(Ordering::Relaxed),
LIVE_ALLOCS_DELTA.load(Ordering::Relaxed),
PEAK_LIVE_BYTES.load(Ordering::Relaxed),
)
}
#[must_use]
pub fn measure_allocations<T, F>(operation: F) -> (T, AllocationSnapshot)
where
F: FnOnce() -> T,
{
let guard: AllocationScopeGuard = AllocationScopeGuard::enter();
let result: T = operation();
let snapshot: AllocationSnapshot = guard.elapsed_snapshot();
drop(guard);
(result, snapshot)
}
fn run_native_heap_probe_cycle(
scene: &SceneHandle,
engine: &mut LayoutEngine,
tokens: ThemeTokens,
) {
let p: ThemePalette = tokens.get_palette();
let g: ThemeGeometry = tokens.get_geometry();
let ctx: Context = Context::new(scene.clone(), PlatformKind::Headless);
let (scope, root_node): (Scope, NodeId) = with_context(ctx, || {
Scope::run(|_: Scope| {
let tick: Signal<u32> = Signal::new(1);
let derived: Memo<u32> = Memo::new(move || tick.get().wrapping_mul(3));
let caption_fn: Box<dyn Fn() -> String> =
Box::new(move || format!("{BENCH_BADGE} {}", derived.get()));
view! {
column {
style: {
width: Length::Px(640.0)
padding: Edges::all(SPACE_SM)
gap: SPACE_XS
background: p.get_card()
border: (g.get_border_width(), p.get_border())
radius: g.get_radius_md()
overflow: Overflow::Hidden
}
text {
style: {
width: Length::Percent(100.0)
font_size: 13.0
color: p.get_foreground()
}
{ caption_fn }
}
EuvProgress {
value: 0.65
tokens: tokens
}
}
}
})
});
let _: LayoutStats = scene.with_write(
|tree: &mut SceneTree| {
let root: NodeId = tree.get_root();
tree.replace_children(root, vec![root_node]);
engine.compute(tree, Size::new(640.0, 360.0))
},
LayoutStats::default(),
);
scope.dispose();
scene.with_write(
|tree: &mut SceneTree| {
tree.dispose_subtree(root_node);
let root: NodeId = tree.get_root();
tree.replace_children(root, Vec::new());
},
(),
);
}
#[must_use]
pub fn probe_native_heap_lifecycle(cycles: u64) -> NativeHeapLifecycleProbe {
let cycle_count: u64 = cycles.max(1);
let tokens: ThemeTokens = ThemeTokens::resolve(DesignScheme::Shadcn, ThemeMode::Light);
reset_reactive_runtime();
let scene: SceneHandle = SceneHandle::new_tree();
let mut engine: LayoutEngine = LayoutEngine::default();
for _ in 0..2u64 {
run_native_heap_probe_cycle(&scene, &mut engine, tokens);
}
let guard: AllocationScopeGuard = AllocationScopeGuard::enter();
let baseline_raw: RawAllocCounters = snapshot_raw_alloc_counters();
let baseline_live_bytes: u64 = if baseline_raw.get_live_bytes_delta() > 0 {
baseline_raw.get_live_bytes_delta() as u64
} else {
0
};
let baseline_live_allocations: u64 = if baseline_raw.get_live_allocs_delta() > 0 {
baseline_raw.get_live_allocs_delta() as u64
} else {
0
};
for _ in 0..cycle_count {
run_native_heap_probe_cycle(&scene, &mut engine, tokens);
}
let after_raw: RawAllocCounters = snapshot_raw_alloc_counters();
let delta: AllocationSnapshot = after_raw.delta_since(baseline_raw);
let after_drain_live_bytes: u64 = if after_raw.get_live_bytes_delta() > 0 {
after_raw.get_live_bytes_delta() as u64
} else {
0
};
let after_drain_live_allocations: u64 = if after_raw.get_live_allocs_delta() > 0 {
after_raw.get_live_allocs_delta() as u64
} else {
0
};
let live_bytes_slope_per_cycle: f64 =
((after_drain_live_bytes as f64) - (baseline_live_bytes as f64)) / (cycle_count as f64);
let live_allocations_slope_per_cycle: f64 = ((after_drain_live_allocations as f64)
- (baseline_live_allocations as f64))
/ (cycle_count as f64);
let peak_live_bytes: u64 = delta.get_peak_live_bytes().max(after_drain_live_bytes);
drop(guard);
NativeHeapLifecycleProbe {
cycles: cycle_count,
baseline_live_bytes,
after_drain_live_bytes,
live_bytes_slope_per_cycle,
baseline_live_allocations,
after_drain_live_allocations,
live_allocations_slope_per_cycle,
peak_live_bytes,
total_alloc_count: delta.get_alloc_count(),
total_free_count: delta.get_free_count(),
total_bytes_allocated: delta.get_bytes_allocated(),
}
}