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