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