From 47409c8a72cf3fb841a70020207f20dbd1c7c2f3 Mon Sep 17 00:00:00 2001 From: Nicola Papale Date: Mon, 2 Oct 2023 14:52:18 +0200 Subject: [PATCH] Add inline(never) to bench systems (#9824) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Objective It is difficult to inspect the generated assembly of benchmark systems using a tool such as `cargo-asm` ## Solution Mark the related functions as `#[inline(never)]`. This way, you can pass the module name as argument to `cargo-asm` to get the generated assembly for the given function. It may have as side effect to make benchmarks a bit more predictable and useful too. As it prevents inlining where in bevy no inlining could possibly take place. ### Measurements Following the recommendations in , I 1. Put my CPU in "AMD ECO" mode, which surprisingly is the equivalent of disabling turboboost, giving more consistent performances 2. Disabled all hyperthreading cores using `echo 0 > /sys/devices/system/cpu/cpu{11,12…}/online` 3. Set the scaling governor to `performance` 4. Manually disabled AMD boost with `echo 0 > /sys/devices/system/cpu/cpufreq/boost` 5. Set the nice level of the criterion benchmark using `cargo bench … & sudo renice -n -5 -p $! ; fg` 6. Not running any other program than the benchmarks (outside of system daemons and the X11 server) With this setup, running multiple times the same benchmarks on `main` gives me a lot of "regression" and "improvement" messages, which is absurd given that no code changed. On this branch, there is still some spurious performance change detection, but they are much less frequent. This only accounts for `iter_simple` and `iter_frag` benchmarks of course. --- benches/benches/bevy_ecs/iteration/iter_frag.rs | 1 + benches/benches/bevy_ecs/iteration/iter_frag_foreach.rs | 1 + benches/benches/bevy_ecs/iteration/iter_frag_foreach_sparse.rs | 1 + benches/benches/bevy_ecs/iteration/iter_frag_foreach_wide.rs | 1 + .../benches/bevy_ecs/iteration/iter_frag_foreach_wide_sparse.rs | 1 + benches/benches/bevy_ecs/iteration/iter_frag_sparse.rs | 1 + benches/benches/bevy_ecs/iteration/iter_frag_wide.rs | 1 + benches/benches/bevy_ecs/iteration/iter_frag_wide_sparse.rs | 1 + benches/benches/bevy_ecs/iteration/iter_simple.rs | 1 + benches/benches/bevy_ecs/iteration/iter_simple_foreach.rs | 1 + .../benches/bevy_ecs/iteration/iter_simple_foreach_sparse_set.rs | 1 + benches/benches/bevy_ecs/iteration/iter_simple_foreach_wide.rs | 1 + .../bevy_ecs/iteration/iter_simple_foreach_wide_sparse_set.rs | 1 + benches/benches/bevy_ecs/iteration/iter_simple_sparse_set.rs | 1 + benches/benches/bevy_ecs/iteration/iter_simple_system.rs | 1 + benches/benches/bevy_ecs/iteration/iter_simple_wide.rs | 1 + .../benches/bevy_ecs/iteration/iter_simple_wide_sparse_set.rs | 1 + 17 files changed, 17 insertions(+) diff --git a/benches/benches/bevy_ecs/iteration/iter_frag.rs b/benches/benches/bevy_ecs/iteration/iter_frag.rs index e2e765c870414..376ac6ea934cd 100644 --- a/benches/benches/bevy_ecs/iteration/iter_frag.rs +++ b/benches/benches/bevy_ecs/iteration/iter_frag.rs @@ -27,6 +27,7 @@ impl<'w> Benchmark<'w> { Self(world, query) } + #[inline(never)] pub fn run(&mut self) { for mut data in self.1.iter_mut(&mut self.0) { data.0 *= 2.0; diff --git a/benches/benches/bevy_ecs/iteration/iter_frag_foreach.rs b/benches/benches/bevy_ecs/iteration/iter_frag_foreach.rs index 480d6f942d2e9..b4a8877074e44 100644 --- a/benches/benches/bevy_ecs/iteration/iter_frag_foreach.rs +++ b/benches/benches/bevy_ecs/iteration/iter_frag_foreach.rs @@ -27,6 +27,7 @@ impl<'w> Benchmark<'w> { Self(world, query) } + #[inline(never)] pub fn run(&mut self) { self.1.for_each_mut(&mut self.0, |mut data| { data.0 *= 2.0; diff --git a/benches/benches/bevy_ecs/iteration/iter_frag_foreach_sparse.rs b/benches/benches/bevy_ecs/iteration/iter_frag_foreach_sparse.rs index add30629afe3b..0d92b39999724 100644 --- a/benches/benches/bevy_ecs/iteration/iter_frag_foreach_sparse.rs +++ b/benches/benches/bevy_ecs/iteration/iter_frag_foreach_sparse.rs @@ -38,6 +38,7 @@ impl<'w> Benchmark<'w> { Self(world, query) } + #[inline(never)] pub fn run(&mut self) { self.1.for_each_mut(&mut self.0, |mut data| { data.0 *= 2.0; diff --git a/benches/benches/bevy_ecs/iteration/iter_frag_foreach_wide.rs b/benches/benches/bevy_ecs/iteration/iter_frag_foreach_wide.rs index f7807486a14f8..f385b04ebd6d8 100644 --- a/benches/benches/bevy_ecs/iteration/iter_frag_foreach_wide.rs +++ b/benches/benches/bevy_ecs/iteration/iter_frag_foreach_wide.rs @@ -55,6 +55,7 @@ impl<'w> Benchmark<'w> { Self(world, query) } + #[inline(never)] pub fn run(&mut self) { self.1.for_each_mut(&mut self.0, |mut data| { data.0 .0 *= 2.0; diff --git a/benches/benches/bevy_ecs/iteration/iter_frag_foreach_wide_sparse.rs b/benches/benches/bevy_ecs/iteration/iter_frag_foreach_wide_sparse.rs index 99ada032f3fd9..e21887204d26b 100644 --- a/benches/benches/bevy_ecs/iteration/iter_frag_foreach_wide_sparse.rs +++ b/benches/benches/bevy_ecs/iteration/iter_frag_foreach_wide_sparse.rs @@ -65,6 +65,7 @@ impl<'w> Benchmark<'w> { Self(world, query) } + #[inline(never)] pub fn run(&mut self) { self.1.for_each_mut(&mut self.0, |mut data| { data.0 .0 *= 2.0; diff --git a/benches/benches/bevy_ecs/iteration/iter_frag_sparse.rs b/benches/benches/bevy_ecs/iteration/iter_frag_sparse.rs index b49e91b8e921d..7385bf28d403c 100644 --- a/benches/benches/bevy_ecs/iteration/iter_frag_sparse.rs +++ b/benches/benches/bevy_ecs/iteration/iter_frag_sparse.rs @@ -38,6 +38,7 @@ impl<'w> Benchmark<'w> { Self(world, query) } + #[inline(never)] pub fn run(&mut self) { for mut data in self.1.iter_mut(&mut self.0) { data.0 *= 2.0; diff --git a/benches/benches/bevy_ecs/iteration/iter_frag_wide.rs b/benches/benches/bevy_ecs/iteration/iter_frag_wide.rs index 878535c3fa5ec..84442c78e547c 100644 --- a/benches/benches/bevy_ecs/iteration/iter_frag_wide.rs +++ b/benches/benches/bevy_ecs/iteration/iter_frag_wide.rs @@ -55,6 +55,7 @@ impl<'w> Benchmark<'w> { Self(world, query) } + #[inline(never)] pub fn run(&mut self) { for mut data in self.1.iter_mut(&mut self.0) { data.0 .0 *= 2.0; diff --git a/benches/benches/bevy_ecs/iteration/iter_frag_wide_sparse.rs b/benches/benches/bevy_ecs/iteration/iter_frag_wide_sparse.rs index be6b9ebbf3be5..928ffea0731be 100644 --- a/benches/benches/bevy_ecs/iteration/iter_frag_wide_sparse.rs +++ b/benches/benches/bevy_ecs/iteration/iter_frag_wide_sparse.rs @@ -65,6 +65,7 @@ impl<'w> Benchmark<'w> { Self(world, query) } + #[inline(never)] pub fn run(&mut self) { for mut data in self.1.iter_mut(&mut self.0) { data.0 .0 *= 2.0; diff --git a/benches/benches/bevy_ecs/iteration/iter_simple.rs b/benches/benches/bevy_ecs/iteration/iter_simple.rs index 41693fbf604d5..bf01a049d485b 100644 --- a/benches/benches/bevy_ecs/iteration/iter_simple.rs +++ b/benches/benches/bevy_ecs/iteration/iter_simple.rs @@ -33,6 +33,7 @@ impl<'w> Benchmark<'w> { Self(world, query) } + #[inline(never)] pub fn run(&mut self) { for (velocity, mut position) in self.1.iter_mut(&mut self.0) { position.0 += velocity.0; diff --git a/benches/benches/bevy_ecs/iteration/iter_simple_foreach.rs b/benches/benches/bevy_ecs/iteration/iter_simple_foreach.rs index 36972562b6128..05956851e50c7 100644 --- a/benches/benches/bevy_ecs/iteration/iter_simple_foreach.rs +++ b/benches/benches/bevy_ecs/iteration/iter_simple_foreach.rs @@ -33,6 +33,7 @@ impl<'w> Benchmark<'w> { Self(world, query) } + #[inline(never)] pub fn run(&mut self) { self.1 .for_each_mut(&mut self.0, |(velocity, mut position)| { diff --git a/benches/benches/bevy_ecs/iteration/iter_simple_foreach_sparse_set.rs b/benches/benches/bevy_ecs/iteration/iter_simple_foreach_sparse_set.rs index 24c37debb2562..f9396f0ff287f 100644 --- a/benches/benches/bevy_ecs/iteration/iter_simple_foreach_sparse_set.rs +++ b/benches/benches/bevy_ecs/iteration/iter_simple_foreach_sparse_set.rs @@ -35,6 +35,7 @@ impl<'w> Benchmark<'w> { Self(world, query) } + #[inline(never)] pub fn run(&mut self) { self.1 .for_each_mut(&mut self.0, |(velocity, mut position)| { diff --git a/benches/benches/bevy_ecs/iteration/iter_simple_foreach_wide.rs b/benches/benches/bevy_ecs/iteration/iter_simple_foreach_wide.rs index a6f5902e41b27..4c0ee60dbec4e 100644 --- a/benches/benches/bevy_ecs/iteration/iter_simple_foreach_wide.rs +++ b/benches/benches/bevy_ecs/iteration/iter_simple_foreach_wide.rs @@ -55,6 +55,7 @@ impl<'w> Benchmark<'w> { Self(world, query) } + #[inline(never)] pub fn run(&mut self) { self.1.for_each_mut(&mut self.0, |mut item| { item.1 .0 += item.0 .0; diff --git a/benches/benches/bevy_ecs/iteration/iter_simple_foreach_wide_sparse_set.rs b/benches/benches/bevy_ecs/iteration/iter_simple_foreach_wide_sparse_set.rs index 0b91fe976a02f..f15dd5850f848 100644 --- a/benches/benches/bevy_ecs/iteration/iter_simple_foreach_wide_sparse_set.rs +++ b/benches/benches/bevy_ecs/iteration/iter_simple_foreach_wide_sparse_set.rs @@ -57,6 +57,7 @@ impl<'w> Benchmark<'w> { Self(world, query) } + #[inline(never)] pub fn run(&mut self) { self.1.for_each_mut(&mut self.0, |mut item| { item.1 .0 += item.0 .0; diff --git a/benches/benches/bevy_ecs/iteration/iter_simple_sparse_set.rs b/benches/benches/bevy_ecs/iteration/iter_simple_sparse_set.rs index 1f3c267d4d50a..f31554ee0d93b 100644 --- a/benches/benches/bevy_ecs/iteration/iter_simple_sparse_set.rs +++ b/benches/benches/bevy_ecs/iteration/iter_simple_sparse_set.rs @@ -35,6 +35,7 @@ impl<'w> Benchmark<'w> { Self(world, query) } + #[inline(never)] pub fn run(&mut self) { for (velocity, mut position) in self.1.iter_mut(&mut self.0) { position.0 += velocity.0; diff --git a/benches/benches/bevy_ecs/iteration/iter_simple_system.rs b/benches/benches/bevy_ecs/iteration/iter_simple_system.rs index 2b09ada53eb25..f4d606689765b 100644 --- a/benches/benches/bevy_ecs/iteration/iter_simple_system.rs +++ b/benches/benches/bevy_ecs/iteration/iter_simple_system.rs @@ -41,6 +41,7 @@ impl Benchmark { Self(world, Box::new(system)) } + #[inline(never)] pub fn run(&mut self) { self.1.run((), &mut self.0); } diff --git a/benches/benches/bevy_ecs/iteration/iter_simple_wide.rs b/benches/benches/bevy_ecs/iteration/iter_simple_wide.rs index 7e5b763d5275d..8995fb843f1c8 100644 --- a/benches/benches/bevy_ecs/iteration/iter_simple_wide.rs +++ b/benches/benches/bevy_ecs/iteration/iter_simple_wide.rs @@ -55,6 +55,7 @@ impl<'w> Benchmark<'w> { Self(world, query) } + #[inline(never)] pub fn run(&mut self) { for mut item in self.1.iter_mut(&mut self.0) { item.1 .0 += item.0 .0; diff --git a/benches/benches/bevy_ecs/iteration/iter_simple_wide_sparse_set.rs b/benches/benches/bevy_ecs/iteration/iter_simple_wide_sparse_set.rs index 8be4a85cccc9f..5235fd93683ad 100644 --- a/benches/benches/bevy_ecs/iteration/iter_simple_wide_sparse_set.rs +++ b/benches/benches/bevy_ecs/iteration/iter_simple_wide_sparse_set.rs @@ -57,6 +57,7 @@ impl<'w> Benchmark<'w> { Self(world, query) } + #[inline(never)] pub fn run(&mut self) { for mut item in self.1.iter_mut(&mut self.0) { item.1 .0 += item.0 .0;