use std::alloc::{GlobalAlloc, Layout, System};
use std::sync::atomic::{AtomicUsize, Ordering};
static ALLOCS: AtomicUsize = AtomicUsize::new(0);
struct Counting;
unsafe impl GlobalAlloc for Counting {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
ALLOCS.fetch_add(1, Ordering::Relaxed);
System.alloc(layout)
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
System.dealloc(ptr, layout)
}
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
ALLOCS.fetch_add(1, Ordering::Relaxed);
System.realloc(ptr, layout, new_size)
}
}
#[global_allocator]
static GLOBAL: Counting = Counting;
fn take_allocs() -> usize {
ALLOCS.swap(0, Ordering::Relaxed)
}
use y901_ui::{
Constraints, EventCtx, EventResult, Flex, Key, Modifiers, Rect, RenderCtx, Semantics, Size,
TextMetrics, TextStyle, UiEvent, UiTree, Widget,
};
struct Mono;
impl TextMetrics for Mono {
fn measure_text(&self, t: &str, _s: &TextStyle) -> Size {
Size::new(t.chars().count() as f32 * 8.0, 16.0)
}
fn line_height(&self, _s: &TextStyle) -> f32 {
16.0
}
fn scale(&self) -> f32 {
1.0
}
}
struct Leaf {
id: String,
rect: Rect,
}
impl Widget for Leaf {
fn id(&self) -> &str {
&self.id
}
fn measure(
&mut self,
_m: &dyn TextMetrics,
c: Constraints,
_cache: &mut y901_ui::layout::LayoutCache,
) -> Size {
c.constrain(Size::new(40.0, 20.0))
}
fn arrange(
&mut self,
r: Rect,
_m: &dyn TextMetrics,
_cache: &mut y901_ui::layout::LayoutCache,
) {
self.rect = r;
}
fn rect(&self) -> Rect {
self.rect
}
fn event(&mut self, _c: &mut EventCtx, _e: &UiEvent) -> EventResult {
EventResult::Handled
}
fn render(&mut self, _c: &mut dyn RenderCtx) {}
fn semantics(&self) -> Semantics {
Semantics::default()
}
}
fn flat(n: usize) -> Box<dyn Widget> {
let mut col = Flex::column("root");
for i in 0..n {
col = col.push(Box::new(Leaf {
id: format!("f{i}"),
rect: Rect::ZERO,
}));
}
Box::new(col)
}
#[test]
#[ignore = "基准:--ignored --nocapture"]
fn dispatch_allocs_per_event() {
let mut tree = UiTree::new(flat(100));
tree.layout(Size::new(400.0, 400.0), &Mono);
let ev = UiEvent::Key {
key: Key::Char('a'),
modifiers: Modifiers::default(),
};
for _ in 0..10 {
let _ = tree.dispatch(&ev);
}
let _ = take_allocs();
const N: usize = 1000;
for _ in 0..N {
let _ = tree.dispatch(&ev);
}
let total = take_allocs();
eprintln!(
"[dispatch] 100 节点树,键盘事件:每帧 {:.2} 次分配({N} 次共 {total} 次)",
total as f64 / N as f64
);
assert!(
total > 0,
"dispatch 至少应有若干次分配,否则说明计数器没接上"
);
}
#[test]
#[ignore = "基准:--ignored --nocapture"]
fn hover_allocs_per_event() {
let mut tree = UiTree::new(flat(100));
tree.layout(Size::new(400.0, 400.0), &Mono);
let ev = UiEvent::MouseMove {
pos: y901_ui::Pos::new(20.0, 20.0),
};
for _ in 0..10 {
let _ = tree.dispatch(&ev);
}
let _ = take_allocs();
const N: usize = 1000;
for _ in 0..N {
let _ = tree.dispatch(&ev);
}
let total = take_allocs();
eprintln!(
"[dispatch] 100 节点树,同位置 MouseMove:每帧 {:.2} 次分配",
total as f64 / N as f64
);
assert!(total > 0);
}
#[test]
#[ignore = "基准:--ignored --nocapture"]
fn hover_blank_area_allocs_per_event() {
let mut tree = UiTree::new(flat(100));
tree.layout(Size::new(400.0, 400.0), &Mono);
let ev = UiEvent::MouseMove {
pos: y901_ui::Pos::new(395.0, 395.0),
};
for _ in 0..10 {
let _ = tree.dispatch(&ev);
}
let _ = take_allocs();
const N: usize = 1000;
for _ in 0..N {
let _ = tree.dispatch(&ev);
}
let total = take_allocs();
eprintln!(
"[dispatch] 100 节点树,空白处 MouseMove:每帧 {:.2} 次分配",
total as f64 / N as f64
);
assert!(total < 100_000, "计数器应接上");
}