From 6ac9d6876f7c0b71b1f84ec3e5df3460acba9449 Mon Sep 17 00:00:00 2001 From: Daniel McNab <36049421+DJMcNab@users.noreply.github.com> Date: Thu, 3 Feb 2022 22:34:29 +0000 Subject: [PATCH] Make ECS benchmark more representative (#2941) # Objective - The addition was being optimised out in the `for_each` loop, but not the `for` loop - Previously this meant that the `for_each` loop looked 3 times as fast - it's actually only 2 times as fast - Effectively, the addition take one unit of time, the for_each takes one unit of time, and the for loop version takes two units of time. ## Solution - `black_box` the count in each loop Note that this does not fix `for_each` being faster than `for`, unfortunately. --- benches/benches/bevy_ecs/world_get.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/benches/benches/bevy_ecs/world_get.rs b/benches/benches/bevy_ecs/world_get.rs index 0cc420e2b3..7c43789e37 100644 --- a/benches/benches/bevy_ecs/world_get.rs +++ b/benches/benches/bevy_ecs/world_get.rs @@ -23,7 +23,7 @@ const RANGE: std::ops::Range = 5..6; fn setup(entity_count: u32) -> World { let mut world = World::default(); world.spawn_batch((0..entity_count).map(|_| (T::default(),))); - world + black_box(world) } fn world_entity(criterion: &mut Criterion) { @@ -126,6 +126,7 @@ fn world_query_iter(criterion: &mut Criterion) { for comp in query.iter(&world) { black_box(comp); count += 1; + black_box(count); } assert_eq!(black_box(count), entity_count); }); @@ -139,6 +140,7 @@ fn world_query_iter(criterion: &mut Criterion) { for comp in query.iter(&world) { black_box(comp); count += 1; + black_box(count); } assert_eq!(black_box(count), entity_count); }); @@ -163,6 +165,7 @@ fn world_query_for_each(criterion: &mut Criterion) { query.for_each(&world, |comp| { black_box(comp); count += 1; + black_box(count); }); assert_eq!(black_box(count), entity_count); }); @@ -176,6 +179,7 @@ fn world_query_for_each(criterion: &mut Criterion) { query.for_each(&world, |comp| { black_box(comp); count += 1; + black_box(count); }); assert_eq!(black_box(count), entity_count); });