Skip to content

Commit

Permalink
https://github.com/Rust-for-Linux/linux/pull/356#issuecomment-859621440
Browse files Browse the repository at this point in the history
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<V, E> instead of F: Fn() -> Result<V, E> fixes this issue. Replacing the rt = match ... with rt += match ... inside use_result is also necessary to prevent optimizing away the match.
  • Loading branch information
foxhlchen committed Jun 11, 2021
1 parent 3920655 commit e87ff2a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 27 deletions.
9 changes: 0 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
37 changes: 19 additions & 18 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -67,13 +69,12 @@ mod bench {
}
}

fn use_result<F, V, E>(n: i32, f: F) -> (i32, Result<V, E>)
where
F: Fn() -> Result<V, E>,
#[inline(never)]
fn use_result<V, E>(n: i32, f: fn() -> Result<V, E>) -> (i32, Result<V, E>)
{
let mut rt :i32 = 0;
for _ in 0..n {
rt = match f() {
rt += match f() {
Ok(_) => 0,
Err(_) => -1,
};
Expand All @@ -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)));
}
}

Expand Down

0 comments on commit e87ff2a

Please sign in to comment.