Skip to content

rustc apparently optimizes away the loop in 3.3 #28713

Closed
@lojic

Description

@lojic

I just implemented the code in section 3.3 to compare Ruby to Rust. It appears that rustc optimizes away the inner loop of the thread, because I kept increasing the limit and the completion time remained constant. I introduced a simple if statement in the loop, and then I got expected timings.

It might be worth modifying the loop so it's not optimized away for a fairer comparison. Here's what I ended up with:

pub extern fn process() {
  let handles: Vec<_> = (0..10).map(|_| {
    thread::spawn(|| {
      let mut x = 0u64;
      for i in (0..7_700_000_000u64) {
        if i < 1 {
          x += 2
        } else {
          x += 1
        }
      }
      x
    })
  }).collect();

  for h in handles {
    println!("Thread finished with count={}",
             h.join().map_err(|_| "Could not join a thread!").unwrap());
  }
  println!("done!");
}

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