Update 0.3.x for 0.3.11 (#455)
1//! This file includes benchmarks for tree creation
2use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
3use rand::prelude::*;
4use rand_chacha::ChaCha8Rng;
5use taffy::prelude::*;
6use taffy::style::Style;
7
8mod helpers;
9
10#[cfg(feature = "yoga_benchmark")]
11use helpers::yoga_helpers;
12#[cfg(feature = "yoga_benchmark")]
13use slotmap::SlotMap;
14#[cfg(feature = "yoga_benchmark")]
15use yoga_helpers::yg;
16
17/// Build a random leaf node
18fn build_random_leaf(taffy: &mut Taffy) -> Node {
19 taffy.new_with_children(Style::DEFAULT, &[]).unwrap()
20}
21
22/// A tree with many children that have shallow depth
23fn build_taffy_flat_hierarchy(total_node_count: u32, use_with_capacity: bool) -> (Taffy, Node) {
24 let mut taffy = if use_with_capacity { Taffy::with_capacity(total_node_count as usize) } else { Taffy::new() };
25 let mut rng = ChaCha8Rng::seed_from_u64(12345);
26 let mut children = Vec::new();
27 let mut node_count = 0;
28
29 while node_count < total_node_count {
30 let sub_children_count = rng.gen_range(1..=4);
31 let sub_children: Vec<Node> = (0..sub_children_count).map(|_| build_random_leaf(&mut taffy)).collect();
32 let node = taffy.new_with_children(Style::DEFAULT, &sub_children).unwrap();
33
34 children.push(node);
35 node_count += 1 + sub_children_count;
36 }
37
38 let root = taffy.new_with_children(Style::DEFAULT, children.as_slice()).unwrap();
39 (taffy, root)
40}
41
42#[cfg(feature = "yoga_benchmark")]
43/// A tree with many children that have shallow depth
44fn build_yoga_flat_hierarchy(total_node_count: u32) -> (yg::YogaTree, Node) {
45 let mut tree = SlotMap::new();
46 let mut rng = ChaCha8Rng::seed_from_u64(12345);
47 let mut children = Vec::new();
48 let mut node_count = 0;
49
50 while node_count < total_node_count {
51 let sub_children_count = rng.gen_range(1..=4);
52 let sub_children: Vec<Node> =
53 (0..sub_children_count).map(|_| yoga_helpers::new_default_style_with_children(&mut tree, vec![])).collect();
54 let node = yoga_helpers::new_default_style_with_children(&mut tree, sub_children);
55
56 children.push(node);
57 node_count += 1 + sub_children_count;
58 }
59
60 let root = yoga_helpers::new_default_style_with_children(&mut tree, children);
61 (tree, root)
62}
63
64fn taffy_benchmarks(c: &mut Criterion) {
65 let mut group = c.benchmark_group("Tree creation");
66 for node_count in [1_000u32, 10_000, 100_000].iter() {
67 #[cfg(feature = "yoga_benchmark")]
68 let benchmark_id = BenchmarkId::new(format!("Yoga"), node_count);
69 #[cfg(feature = "yoga_benchmark")]
70 group.bench_with_input(benchmark_id, node_count, |b, &node_count| {
71 b.iter(|| {
72 let (taffy, root) = build_yoga_flat_hierarchy(node_count);
73 std::hint::black_box(taffy);
74 std::hint::black_box(root);
75 })
76 });
77 let benchmark_id = BenchmarkId::new(format!("Taffy::new"), node_count);
78 group.bench_with_input(benchmark_id, node_count, |b, &node_count| {
79 b.iter(|| {
80 let (tree, root) = build_taffy_flat_hierarchy(node_count, false);
81 std::hint::black_box(tree);
82 std::hint::black_box(root);
83 })
84 });
85
86 let benchmark_id = BenchmarkId::new(format!("Taffy::with_capacity"), node_count);
87 group.bench_with_input(benchmark_id, node_count, |b, &node_count| {
88 b.iter(|| {
89 let (tree, root) = build_taffy_flat_hierarchy(node_count, true);
90 std::hint::black_box(tree);
91 std::hint::black_box(root);
92 })
93 });
94 }
95 group.finish();
96}
97
98criterion_group!(benches, taffy_benchmarks);
99criterion_main!(benches);