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