Skip to content

Compiler cant hoist following branch out of loop #544

Open

Description

@re0312

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

Activity

  1. Amanieu commented on Aug 21, 2024

    @Amanieu
    Member

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

  2. re0312 commented on Aug 21, 2024

    @re0312
    Author

    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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions