gioser

tawasuyu/taffy

A high performance rust-powered UI layout library

Rust 496HTML 245JavaScript 8CSS 61215 Sutichaykuna

⎇ 08aa50102b44242362de80dd1f4eb0c9b208c758 benches/helpers/mod.rs Rust · 28 siq'ikuna · 959 B Ñawpa willay Pi imata llamkarqan ch'uya

Tree creation benchmarks (#401) 08aa50102 · Nico Burns ·

1#[cfg(feature = "yoga_benchmark")]
2pub mod yoga_helpers;
3
4/// A helper function to recursively construct a deep tree
5#[allow(dead_code)]
6pub fn build_deep_tree<T, N>(
7    tree: &mut T,
8    max_nodes: u32,
9    branching_factor: u32,
10    create_leaf_node: &mut impl FnMut(&mut T) -> N,
11    create_container_node: &mut impl FnMut(&mut T, Vec<N>) -> N,
12) -> Vec<N> {
13    if max_nodes <= branching_factor {
14        // Build leaf nodes
15        return (0..max_nodes).map(|_| create_leaf_node(tree)).collect();
16    }
17
18    // Add another layer to the tree
19    // Each child gets an equal amount of the remaining nodes
20    (0..branching_factor)
21        .map(|_| {
22            let max_nodes = (max_nodes - branching_factor) / branching_factor;
23            let sub_children =
24                build_deep_tree(tree, max_nodes, branching_factor, create_leaf_node, create_container_node);
25            create_container_node(tree, sub_children)
26        })
27        .collect()
28}