|
| 1 | +use bevy_ecs::bundle::Bundle; |
| 2 | +use bevy_ecs::reflect::AppTypeRegistry; |
| 3 | +use bevy_ecs::{component::Component, reflect::ReflectComponent, world::World}; |
| 4 | +use bevy_hierarchy::{BuildChildren, CloneEntityHierarchyExt}; |
| 5 | +use bevy_math::Mat4; |
| 6 | +use bevy_reflect::{GetTypeRegistration, Reflect}; |
| 7 | +use criterion::{black_box, criterion_group, criterion_main, Bencher, Criterion}; |
| 8 | + |
| 9 | +criterion_group!(benches, reflect_benches, clone_benches); |
| 10 | +criterion_main!(benches); |
| 11 | + |
| 12 | +#[derive(Component, Reflect, Default, Clone)] |
| 13 | +#[reflect(Component)] |
| 14 | +struct C1(Mat4); |
| 15 | + |
| 16 | +#[derive(Component, Reflect, Default, Clone)] |
| 17 | +#[reflect(Component)] |
| 18 | +struct C2(Mat4); |
| 19 | + |
| 20 | +#[derive(Component, Reflect, Default, Clone)] |
| 21 | +#[reflect(Component)] |
| 22 | +struct C3(Mat4); |
| 23 | + |
| 24 | +#[derive(Component, Reflect, Default, Clone)] |
| 25 | +#[reflect(Component)] |
| 26 | +struct C4(Mat4); |
| 27 | + |
| 28 | +#[derive(Component, Reflect, Default, Clone)] |
| 29 | +#[reflect(Component)] |
| 30 | +struct C5(Mat4); |
| 31 | + |
| 32 | +#[derive(Component, Reflect, Default, Clone)] |
| 33 | +#[reflect(Component)] |
| 34 | +struct C6(Mat4); |
| 35 | + |
| 36 | +#[derive(Component, Reflect, Default, Clone)] |
| 37 | +#[reflect(Component)] |
| 38 | +struct C7(Mat4); |
| 39 | + |
| 40 | +#[derive(Component, Reflect, Default, Clone)] |
| 41 | +#[reflect(Component)] |
| 42 | +struct C8(Mat4); |
| 43 | + |
| 44 | +#[derive(Component, Reflect, Default, Clone)] |
| 45 | +#[reflect(Component)] |
| 46 | +struct C9(Mat4); |
| 47 | + |
| 48 | +#[derive(Component, Reflect, Default, Clone)] |
| 49 | +#[reflect(Component)] |
| 50 | +struct C10(Mat4); |
| 51 | + |
| 52 | +type ComplexBundle = (C1, C2, C3, C4, C5, C6, C7, C8, C9, C10); |
| 53 | + |
| 54 | +fn hierarchy<C: Bundle + Default + GetTypeRegistration>( |
| 55 | + b: &mut Bencher, |
| 56 | + width: usize, |
| 57 | + height: usize, |
| 58 | + clone_via_reflect: bool, |
| 59 | +) { |
| 60 | + let mut world = World::default(); |
| 61 | + let registry = AppTypeRegistry::default(); |
| 62 | + { |
| 63 | + let mut r = registry.write(); |
| 64 | + r.register::<C>(); |
| 65 | + } |
| 66 | + world.insert_resource(registry); |
| 67 | + world.register_bundle::<C>(); |
| 68 | + if clone_via_reflect { |
| 69 | + let mut components = Vec::new(); |
| 70 | + C::get_component_ids(world.components(), &mut |id| components.push(id.unwrap())); |
| 71 | + for component in components { |
| 72 | + world |
| 73 | + .get_component_clone_handlers_mut() |
| 74 | + .set_component_handler( |
| 75 | + component, |
| 76 | + bevy_ecs::component::ComponentCloneHandler::reflect_handler(), |
| 77 | + ); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + let id = world.spawn(black_box(C::default())).id(); |
| 82 | + |
| 83 | + let mut hierarchy_level = vec![id]; |
| 84 | + |
| 85 | + for _ in 0..height { |
| 86 | + let current_hierarchy_level = hierarchy_level.clone(); |
| 87 | + hierarchy_level.clear(); |
| 88 | + for parent_id in current_hierarchy_level { |
| 89 | + for _ in 0..width { |
| 90 | + let child_id = world |
| 91 | + .spawn(black_box(C::default())) |
| 92 | + .set_parent(parent_id) |
| 93 | + .id(); |
| 94 | + hierarchy_level.push(child_id) |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + world.flush(); |
| 99 | + |
| 100 | + b.iter(move || { |
| 101 | + world.commands().entity(id).clone_and_spawn_with(|builder| { |
| 102 | + builder.recursive(true); |
| 103 | + }); |
| 104 | + world.flush(); |
| 105 | + }); |
| 106 | +} |
| 107 | + |
| 108 | +fn simple<C: Bundle + Default + GetTypeRegistration>(b: &mut Bencher, clone_via_reflect: bool) { |
| 109 | + let mut world = World::default(); |
| 110 | + let registry = AppTypeRegistry::default(); |
| 111 | + { |
| 112 | + let mut r = registry.write(); |
| 113 | + r.register::<C>(); |
| 114 | + } |
| 115 | + world.insert_resource(registry); |
| 116 | + world.register_bundle::<C>(); |
| 117 | + if clone_via_reflect { |
| 118 | + let mut components = Vec::new(); |
| 119 | + C::get_component_ids(world.components(), &mut |id| components.push(id.unwrap())); |
| 120 | + for component in components { |
| 121 | + world |
| 122 | + .get_component_clone_handlers_mut() |
| 123 | + .set_component_handler( |
| 124 | + component, |
| 125 | + bevy_ecs::component::ComponentCloneHandler::reflect_handler(), |
| 126 | + ); |
| 127 | + } |
| 128 | + } |
| 129 | + let id = world.spawn(black_box(C::default())).id(); |
| 130 | + |
| 131 | + b.iter(move || { |
| 132 | + world.commands().entity(id).clone_and_spawn(); |
| 133 | + world.flush(); |
| 134 | + }); |
| 135 | +} |
| 136 | + |
| 137 | +fn reflect_benches(c: &mut Criterion) { |
| 138 | + c.bench_function("many components reflect", |b| { |
| 139 | + simple::<ComplexBundle>(b, true); |
| 140 | + }); |
| 141 | + |
| 142 | + c.bench_function("hierarchy wide reflect", |b| { |
| 143 | + hierarchy::<C1>(b, 10, 4, true); |
| 144 | + }); |
| 145 | + |
| 146 | + c.bench_function("hierarchy tall reflect", |b| { |
| 147 | + hierarchy::<C1>(b, 1, 50, true); |
| 148 | + }); |
| 149 | + |
| 150 | + c.bench_function("hierarchy many reflect", |b| { |
| 151 | + hierarchy::<ComplexBundle>(b, 5, 5, true); |
| 152 | + }); |
| 153 | +} |
| 154 | + |
| 155 | +fn clone_benches(c: &mut Criterion) { |
| 156 | + c.bench_function("many components clone", |b| { |
| 157 | + simple::<ComplexBundle>(b, false); |
| 158 | + }); |
| 159 | + |
| 160 | + c.bench_function("hierarchy wide clone", |b| { |
| 161 | + hierarchy::<C1>(b, 10, 4, false); |
| 162 | + }); |
| 163 | + |
| 164 | + c.bench_function("hierarchy tall clone", |b| { |
| 165 | + hierarchy::<C1>(b, 1, 50, false); |
| 166 | + }); |
| 167 | + |
| 168 | + c.bench_function("hierarchy many clone", |b| { |
| 169 | + hierarchy::<ComplexBundle>(b, 5, 5, false); |
| 170 | + }); |
| 171 | +} |
0 commit comments