From e87ff2a2c9a1042811ea207bcc8ea9c44a32cce5 Mon Sep 17 00:00:00 2001 From: foxhlchen Date: Fri, 11 Jun 2021 22:46:24 +0800 Subject: [PATCH] https://github.com/Rust-for-Linux/linux/pull/356#issuecomment-859621440 By Bjorn3: With opt-level=0 you can't do any realistic benchmarks. The compiler won't optimize away any zero-cost abstractions. For example for item in vec.iter() { ... } can be faster than while i < vec.len() { ...; i+=1; } with optimizations due to no bounds checking, but without optimizations it is likely much slower. black_box calls didn't help as you likely passed a function item and not function pointer through the black box. This means that it is still known which function is called based on the type of the function item. Taking fn() -> Result instead of F: Fn() -> Result fixes this issue. Replacing the rt = match ... with rt += match ... inside use_result is also necessary to prevent optimizing away the match. --- Cargo.toml | 9 --------- src/main.rs | 37 +++++++++++++++++++------------------ 2 files changed, 19 insertions(+), 27 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 067a168..05bc608 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,14 +5,5 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html -[profile.dev] -opt-level = 0 - -[profile.release] -opt-level = 0 - -[profile.bench] -opt-level = 0 - [dependencies] diff --git a/src/main.rs b/src/main.rs index 48c9ea0..a9aaf89 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,10 @@ -#![feature(test)] +#![feature(test, bench_black_box)] -use core::num::{NonZeroI16, NonZeroI32}; extern crate test; +use std::hint::black_box; +use std::num::{NonZeroI16, NonZeroI32}; + #[derive(Clone, Copy, PartialEq, Eq)] pub struct ErrorNzi32(NonZeroI32); pub struct ErrorNzi16(NonZeroI16); @@ -67,13 +69,12 @@ mod bench { } } - fn use_result(n: i32, f: F) -> (i32, Result) - where - F: Fn() -> Result, + #[inline(never)] + fn use_result(n: i32, f: fn() -> Result) -> (i32, Result) { let mut rt :i32 = 0; for _ in 0..n { - rt = match f() { + rt += match f() { Ok(_) => 0, Err(_) => -1, }; @@ -84,62 +85,62 @@ mod bench { #[bench] fn bench_nzi32_100(b: &mut Bencher) { - b.iter(|| use_result(100, return_nzi32)); + b.iter(|| use_result(100, black_box(return_nzi32))); } #[bench] fn bench_nzi32_10000(b: &mut Bencher) { - b.iter(|| use_result(10000, return_nzi32)); + b.iter(|| use_result(10000, black_box(return_nzi32))); } #[bench] fn bench_nzi32_1000000(b: &mut Bencher) { - b.iter(|| use_result(1000000, return_nzi32)); + b.iter(|| use_result(1000000, black_box(return_nzi32))); } #[bench] fn bench_nzi16_100(b: &mut Bencher) { - b.iter(|| use_result(100, return_nzi16)); + b.iter(|| use_result(100, black_box(return_nzi16))); } #[bench] fn bench_nzi16_10000(b: &mut Bencher) { - b.iter(|| use_result(10000, return_nzi16)); + b.iter(|| use_result(10000, black_box(return_nzi16))); } #[bench] fn bench_nzi16_1000000(b: &mut Bencher) { - b.iter(|| use_result(1000000, return_nzi16)); + b.iter(|| use_result(1000000, black_box(return_nzi16))); } #[bench] fn bench_i32_100(b: &mut Bencher) { - b.iter(|| use_result(100, return_i32)); + b.iter(|| use_result(100, black_box(return_i32))); } #[bench] fn bench_i32_10000(b: &mut Bencher) { - b.iter(|| use_result(10000, return_i32)); + b.iter(|| use_result(10000, black_box(return_i32))); } #[bench] fn bench_i32_1000000(b: &mut Bencher) { - b.iter(|| use_result(1000000, return_i32)); + b.iter(|| use_result(1000000, black_box(return_i32))); } #[bench] fn bench_i16_100(b: &mut Bencher) { - b.iter(|| use_result(100, return_i16)); + b.iter(|| use_result(100, black_box(return_i16))); } #[bench] fn bench_i16_10000(b: &mut Bencher) { - b.iter(|| use_result(10000, return_i16)); + b.iter(|| use_result(10000, black_box(return_i16))); } #[bench] fn bench_i16_1000000(b: &mut Bencher) { - b.iter(|| use_result(1000000, return_i16)); + b.iter(|| use_result(1000000, black_box(return_i16))); } }