Skip to content

Instantly share code, notes, and snippets.

@HeroicKatora
Created July 15, 2019 10:05
Show Gist options
  • Save HeroicKatora/4f3b62f50b4bf8126124ff84f3a8c079 to your computer and use it in GitHub Desktop.
Save HeroicKatora/4f3b62f50b4bf8126124ff84f3a8c079 to your computer and use it in GitHub Desktop.
Showcase failed compiler optimization for struct access or shift respectively.
//! Test the difference between different struct access.
//!
//! Proof that the struct access by member (which compiles to a mov to stack and back) is not
//! always slower than the shift, in a situation where the arithmetic unit is also used intensively
//! by a loop index computation. Note that the results are likely to be *highly* architecture
//! dependent.
//!
//! ## Some results
//!
//! With my cpu:
//!
//! ```
//! model name : Intel(R) Core(TM) i5-4690S CPU @ 3.20GHz
//! flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm cpuid_fault invpcid_single pti ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid xsaveopt dtherm ida arat pln pts md_clear flush_l1d
//! ```
//!
//! When compiling for generic x86-64. Speculation: no fitting bit selection instruction available
//! and the address calculation does not require the arithmetic unit. It also utilizes a different
//! CPU port than the loop index calculation.
//!
//! ```
//! # RUSTFLAGS="-C target-cpu=x86-64" cargo bench
//! test tests::bench_safe ... bench: 3,161 ns/iter (+/- 77)
//! test tests::bench_transmute ... bench: 4,070 ns/iter (+/- 206)
//!
//! ```
//!
//! When compiling and benching for the native architecture, the results are the other way around:
//! ```
//! # cargo bench
//! test tests::bench_safe ... bench: 2,730 ns/iter (+/- 47)
//! test tests::bench_transmute ... bench: 1,539 ns/iter (+/- 78)
//! ```
#![feature(test)]
extern crate test;
#[derive(Clone, Copy)]
struct S {
inner: [u8; 8],
}
impl S {
#[inline(always)]
fn get(self, idx: usize) -> u8 {
self.inner.get(idx).cloned().unwrap_or(0)
}
fn get_transmute(self, idx: u8) -> u8 {
(unsafe { std::mem::transmute::<_, u64>(self.inner) >> (8*idx) }) as u8
}
fn get_transmute_strict(self, idx: usize) -> u8 {
(unsafe { std::mem::transmute::<_, u64>(self.inner)})
.checked_shr(8*idx as u32)
.unwrap_or(0) as u8
}
}
#[cfg(test)]
mod tests {
use super::*;
use test::Bencher;
#[bench]
fn bench_safe(b: &mut Bencher) {
let s = S { inner: [0, 1, 2, 3, 4, 5, 6, 7], };
b.iter(|| -> u8 {
let s = test::black_box(s);
let n = test::black_box(10000);
(0..n).map(|i| s.get(i%8))
.fold(0, u8::wrapping_add)
});
}
#[bench]
fn bench_transmute(b: &mut Bencher) {
let s = S { inner: [0, 1, 2, 3, 4, 5, 6, 7], };
b.iter(|| -> u8 {
let s = test::black_box(s);
let n = test::black_box(10000u64);
(0..n).map(|i| s.get_transmute((i%8) as u8))
.fold(0, u8::wrapping_add)
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment