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