rust-lang / hashbrown

Rust port of Google's SwissTable hash map
https://rust-lang.github.io/hashbrown
Apache License 2.0
2.46k stars 288 forks source link

Compiler cant hoist following branch out of loop #544

Open re0312 opened 3 months ago

re0312 commented 3 months ago

Objectives

Why can't the compiler hoist branch out of the loop in the following code?

use std::hint::black_box;

use hashbrown::HashMap;

pub fn main() {
    let mut map: HashMap<u32, u32> = hashbrown::HashMap::default();
    map.insert(0, 0);

    for i in 0..10000 {
        if let Some(_) = map.get(&0) {
            continue;
        } else {
            black_box(i);
        }
    }
}

online assembly https://rust.godbolt.org/z/vr3WGsfhP

Amanieu commented 3 months ago

This happens because hashbrown::raw::RawTable<T,A>::find is not inlined and therefore LLVM doesn't know that it is a pure function.

re0312 commented 3 months ago

This happens because hashbrown::raw::RawTable<T,A>::find is not inlined and therefore LLVM doesn't know that it is a pure function.

Thanks for your reply. but I checked the code and it seems that RawTable::find has been marked as inline, and there are no extra call in the assembly