layout_bench/
layout_bench.rs1use std::time::Instant;
2use ui_layout::*;
3
4fn make_node(is_flex: bool) -> LayoutNode {
5 LayoutNode::new(Style {
6 display: if is_flex {
7 Display::Flex {
8 flex_direction: FlexDirection::Row,
9 }
10 } else {
11 Display::Block
12 },
13 size: SizeStyle {
14 width: Length::Auto,
15 height: Length::Auto,
16 min_width: Length::Px(0.0),
17 max_width: Length::Px(10_000.0),
18 min_height: Length::Px(0.0),
19 max_height: Length::Px(10_000.0),
20 },
21 spacing: Spacing {
22 margin_left: Length::Px(2.0),
23 margin_right: Length::Px(2.0),
24 margin_top: Length::Px(1.0),
25 margin_bottom: Length::Px(1.0),
26 padding_left: Length::Px(1.0),
27 padding_right: Length::Px(1.0),
28 padding_top: Length::Px(1.0),
29 padding_bottom: Length::Px(1.0),
30 border_left: Length::Px(1.0),
31 border_right: Length::Px(1.0),
32 border_top: Length::Px(1.0),
33 border_bottom: Length::Px(1.0),
34 ..Default::default()
35 },
36 ..Default::default()
37 })
38}
39
40fn make_tree(depth: usize, max_depth: usize, remaining: &mut usize) -> LayoutNode {
41 let is_flex = depth % 2 == 0; let mut node = make_node(is_flex);
43
44 if depth >= max_depth || *remaining == 0 {
45 return node;
46 }
47
48 let max_children = if depth < 3 {
49 4
50 } else if depth < 6 {
51 3
52 } else if depth < 9 {
53 2
54 } else {
55 1
56 };
57
58 let child_count = max_children.min(*remaining);
59
60 let mut children = Vec::with_capacity(child_count);
61 for _ in 0..child_count {
62 if *remaining == 0 {
63 break;
64 }
65 *remaining -= 1;
66 children.push(make_tree(depth + 1, max_depth, remaining));
67 }
68
69 node.children = children;
70 node
71}
72
73fn main() {
74 const TOTAL_NODES: usize = 7_000;
75 const DEPTH: usize = 16;
76
77 println!("layout performance benchmark");
78 println!("TOTAL_NODES = {}", TOTAL_NODES);
79 println!("DEPTH = {}", DEPTH);
80 println!("-------------------------");
81
82 let mut remaining = TOTAL_NODES - 1;
83 let mut root = make_tree(0, DEPTH, &mut remaining);
84
85 println!("remaining nodes unused = {}", remaining);
86
87 let t1 = {
88 let start = Instant::now();
89 LayoutEngine::layout(&mut root, 800.0, 600.0);
90 start.elapsed()
91 };
92
93 println!("1st layout: {:?}", t1);
94
95 let t2 = {
96 let start = Instant::now();
97 LayoutEngine::layout(&mut root, 800.0, 600.0);
98 start.elapsed()
99 };
100
101 println!("2nd layout: {:?}", t2);
102}