1use criterion::{criterion_group, criterion_main, Criterion};
2
3fn build_deep_hierarchy(taffy: &mut taffy::node::Taffy) -> taffy::node::Node {
4 let node111 = taffy
5 .new_node(
6 taffy::style::Style {
7 size: taffy::geometry::Size {
8 width: taffy::style::Dimension::Points(10.0),
9 height: taffy::style::Dimension::Points(10.0),
10 },
11 ..Default::default()
12 },
13 &[],
14 )
15 .unwrap();
16 let node112 = taffy
17 .new_node(
18 taffy::style::Style {
19 size: taffy::geometry::Size {
20 width: taffy::style::Dimension::Points(10.0),
21 height: taffy::style::Dimension::Points(10.0),
22 },
23 ..Default::default()
24 },
25 &[],
26 )
27 .unwrap();
28
29 let node121 = taffy
30 .new_node(
31 taffy::style::Style {
32 size: taffy::geometry::Size {
33 width: taffy::style::Dimension::Points(10.0),
34 height: taffy::style::Dimension::Points(10.0),
35 },
36 ..Default::default()
37 },
38 &[],
39 )
40 .unwrap();
41 let node122 = taffy
42 .new_node(
43 taffy::style::Style {
44 size: taffy::geometry::Size {
45 width: taffy::style::Dimension::Points(10.0),
46 height: taffy::style::Dimension::Points(10.0),
47 },
48 ..Default::default()
49 },
50 &[],
51 )
52 .unwrap();
53
54 let node11 = taffy.new_node(taffy::style::Style { ..Default::default() }, &[node111, node112]).unwrap();
55 let node12 = taffy.new_node(taffy::style::Style { ..Default::default() }, &[node121, node122]).unwrap();
56 let node1 = taffy.new_node(taffy::style::Style { ..Default::default() }, &[node11, node12]).unwrap();
57
58 let node211 = taffy
59 .new_node(
60 taffy::style::Style {
61 size: taffy::geometry::Size {
62 width: taffy::style::Dimension::Points(10.0),
63 height: taffy::style::Dimension::Points(10.0),
64 },
65 ..Default::default()
66 },
67 &[],
68 )
69 .unwrap();
70 let node212 = taffy
71 .new_node(
72 taffy::style::Style {
73 size: taffy::geometry::Size {
74 width: taffy::style::Dimension::Points(10.0),
75 height: taffy::style::Dimension::Points(10.0),
76 },
77 ..Default::default()
78 },
79 &[],
80 )
81 .unwrap();
82
83 let node221 = taffy
84 .new_node(
85 taffy::style::Style {
86 size: taffy::geometry::Size {
87 width: taffy::style::Dimension::Points(10.0),
88 height: taffy::style::Dimension::Points(10.0),
89 },
90 ..Default::default()
91 },
92 &[],
93 )
94 .unwrap();
95 let node222 = taffy
96 .new_node(
97 taffy::style::Style {
98 size: taffy::geometry::Size {
99 width: taffy::style::Dimension::Points(10.0),
100 height: taffy::style::Dimension::Points(10.0),
101 },
102 ..Default::default()
103 },
104 &[],
105 )
106 .unwrap();
107
108 let node21 = taffy.new_node(taffy::style::Style { ..Default::default() }, &[node211, node212]).unwrap();
109 let node22 = taffy.new_node(taffy::style::Style { ..Default::default() }, &[node221, node222]).unwrap();
110
111 let node2 = taffy.new_node(taffy::style::Style { ..Default::default() }, &[node21, node22]).unwrap();
112
113 taffy.new_node(taffy::style::Style { ..Default::default() }, &[node1, node2]).unwrap()
114}
115
116fn taffy_benchmarks(c: &mut Criterion) {
117 c.bench_function("deep hierarchy - build", |b| {
118 b.iter(|| {
119 let mut taffy = taffy::node::Taffy::new();
120 build_deep_hierarchy(&mut taffy);
121 })
122 });
123
124 c.bench_function("deep hierarchy - single", |b| {
125 b.iter(|| {
126 let mut taffy = taffy::node::Taffy::new();
127 let root = build_deep_hierarchy(&mut taffy);
128 taffy.compute_layout(root, taffy::geometry::Size::undefined()).unwrap()
129 })
130 });
131
132 c.bench_function("deep hierarchy - relayout", |b| {
133 let mut taffy = taffy::node::Taffy::new();
134 let root = build_deep_hierarchy(&mut taffy);
135
136 b.iter(|| {
137 taffy.mark_dirty(root).unwrap();
138 taffy.compute_layout(root, taffy::geometry::Size::undefined()).unwrap()
139 })
140 });
141}
142
143criterion_group!(benches, taffy_benchmarks);
144criterion_main!(benches);