rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
98.34k stars 12.72k forks source link

-Ctarget-cpu=skylake trashes ends_with_str performance #69510

Open matthiaskrgr opened 4 years ago

matthiaskrgr commented 4 years ago

I was running these benchmarks:

#![feature(test)]

extern crate test;
use crate::test::black_box;
use crate::test::Bencher;

fn main() {
    println!("Hello, world!");
}

#[bench]
fn starts_with_char(b: &mut Bencher) {
    let text = black_box("kdjsfhlakfhlsghlkvcnljknfqiunvcijqenwodind");
    b.iter(|| {
        for _ in 0..1024 {
            black_box(text.starts_with('k'));
        }
    })
}

#[bench]
fn starts_with_str(b: &mut Bencher) {
    let text = black_box("kdjsfhlakfhlsghlkvcnljknfqiunvcijqenwodind");
    b.iter(|| {
        for _ in 0..1024 {
            black_box(text.starts_with("k"));
        }
    })
}

#[bench]
fn ends_with_char(b: &mut Bencher) {
    let text = black_box("kdjsfhlakfhlsghlkvcnljknfqiunvcijqenwodind");
    b.iter(|| {
        for _ in 0..1024 {
            black_box(text.ends_with('k'));
        }
    })
}

#[bench]
fn ends_with_str(b: &mut Bencher) {
    let text = black_box("kdjsfhlakfhlsghlkvcnljknfqiunvcijqenwodind");
    b.iter(|| {
        for _ in 0..1024 {
            black_box(text.ends_with("k"));
        }
    })
}

It turned out that I got a major performance drop with -Ctarget-cpu=native (-Ctarget-cpu=skylake in my case).

RUSTFLAGS="-Ctarget-cpu=skylake" cargo bench

running 4 tests
test ends_with_char   ... bench:         692 ns/iter (+/- 38)
test ends_with_str    ... bench:       1,033 ns/iter (+/- 23)
test starts_with_char ... bench:         356 ns/iter (+/- 44)
test starts_with_str  ... bench:         362 ns/iter (+/- 410)

RUSTFLAGS="" cargo bench

running 4 tests
test ends_with_char   ... bench:         468 ns/iter (+/- 21)
test ends_with_str    ... bench:         539 ns/iter (+/- 40)
test starts_with_char ... bench:         356 ns/iter (+/- 12)
test starts_with_str  ... bench:         693 ns/iter (+/- 44)

By generating code "optimized" for my machine, perf dropped from 539 ns/iter to 1,033 ns/iter for ends_with_str :(

Meta

cpu info:

processor   : 3
vendor_id   : GenuineIntel
cpu family  : 6
model       : 142
model name  : Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz
stepping    : 9
microcode   : 0xca
cpu MHz     : 591.388
cache size  : 3072 KB
physical id : 0
siblings    : 4
core id     : 1
cpu cores   : 2
apicid      : 3
initial apicid  : 3
fpu     : yes
fpu_exception   : yes
cpuid level : 22
wp      : yes
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 art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx 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 3dnowprefetch cpuid_fault epb invpcid_single pti ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid mpx rdseed adx smap clflushopt intel_pt xsaveopt xsavec xgetbv1 xsaves dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp md_clear flush_l1d
bugs        : cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf mds swapgs itlb_multihit
bogomips    : 5401.81
clflush size    : 64
cache_alignment : 64
address sizes   : 39 bits physical, 48 bits virtual
power management:

rustc --version --verbose:

rustc 1.43.0-nightly (abc3073c9 2020-02-26)
binary: rustc
commit-hash: abc3073c92df034636a823c5382ece2186d22b9e
commit-date: 2020-02-26
host: x86_64-unknown-linux-gnu
release: 1.43.0-nightly
LLVM version: 9.0
mati865 commented 4 years ago

Assembly doesn't look suspicious: https://godbolt.org/z/BTxUDA

Right now I have Windows machine with totally different CPU nearby and it doesn't reproduce:

$ RUSTFLAGS="" cargo bench
   Compiling bench v0.1.0 (D:\msys64\home\mateusz\bench)
    Finished bench [optimized] target(s) in 2.44s
     Running target\release\deps\bench-2ddd0f70613232dd.exe

running 4 tests
test ends_with_char   ... bench:         288 ns/iter (+/- 16)
test ends_with_str    ... bench:         487 ns/iter (+/- 9)
test starts_with_char ... bench:         285 ns/iter (+/- 3)
test starts_with_str  ... bench:         289 ns/iter (+/- 15)

test result: ok. 0 passed; 0 failed; 0 ignored; 4 measured; 0 filtered out

$ RUSTFLAGS="-Ctarget-cpu=skylake" cargo bench
   Compiling bench v0.1.0 (D:\msys64\home\mateusz\bench)
    Finished bench [optimized] target(s) in 0.86s
     Running target\release\deps\bench-2ddd0f70613232dd.exe

running 4 tests
test ends_with_char   ... bench:         288 ns/iter (+/- 9)
test ends_with_str    ... bench:         413 ns/iter (+/- 8)
test starts_with_char ... bench:         287 ns/iter (+/- 18)
test starts_with_str  ... bench:         287 ns/iter (+/- 2)

test result: ok. 0 passed; 0 failed; 0 ignored; 4 measured; 0 filtered out

cpuinfo:

processor       : 15
vendor_id       : AuthenticAMD
cpu family      : 23
model           : 8
model name      : AMD Ryzen 7 2700X Eight-Core Processor
stepping        : 2
cpu MHz         : 3700.000
cache size      : 16384 KB
physical id     : 0
siblings        : 16
core id         : 7
cpu cores       : 8
apicid          : 15
initial apicid  : 15
fpu             : yes
fpu_exception   : yes
cpuid level     : 13
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm pni cx16 lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw skinit wdt tce topoext perfctr_core perfctr_nb perfctr_l2
clflush size    : 64
cache_alignment : 64
address sizes   : 48 bits physical, 48 bits virtual
power management: ts ttp tm hwpstate cpb eff_freq_ro

rustc -vV:

rustc 1.43.0-nightly (6fd8798f4 2020-02-25)
binary: rustc
commit-hash: 6fd8798f4de63328d743eb2a9a9c12e202a4a182
commit-date: 2020-02-25
host: x86_64-pc-windows-gnu
release: 1.43.0-nightly
LLVM version: 9.0

With -Ctarget-cpu=skylake there are AVX instructions generated. Maybe the CPU downclocks a lot since it's low TDP CPU? Do you have other machines to test it on?