You are viewing a single thread.
View all comments
7 points

It would be interesting to see if an iterator instead of a manual for loop would increase the performance of the base case.

My guess is not, because the compiler should know they are equivalent, but would be interesting to check anyway.

permalink
report
reply
2 points

I wonder if the compiler checks to see if the calls are pure and are therefore safe to run in parallel. It seems like the kind of thing the Rust compiler should be able to do.

permalink
report
parent
reply
5 points

If by parallel you mean across multiple threads in some map-reduce algorithm, the compiler will not do that automatically since that would be both extremely surprising behavior and in most cases, would make performance worse (it’d be interesting to see just how many shapes you’d need to iterate over before you start seeing performance benefits from map-reduce). If you’re referring to vectorization, then the Rust compiler does automatically do that in some cases, and I imagine it depends on how the area is calculated and whether the implementation can be inlined.

permalink
report
parent
reply
1 point
*

Do you mean this for loop?

for shape in &shapes {
  accum += shape.area();
}

That does use an iterator

for-in-loops, or to be more precise, iterator loops, are a simple syntactic sugar over a common practice within Rust, which is to loop over anything that implements IntoIterator until the iterator returned by .into_iter() returns None (or the loop body uses break).

Anti Commercial AI thingy

CC BY-NC-SA 4.0

permalink
report
parent
reply
9 points

I think they meant using for accumulating, like this:

shapes.iter().map(Shape::area).sum()
permalink
report
parent
reply
4 points

Yes. That’s what I meant.

Though I heavily expect the rust compiler to produce identical assembly for both types of iteration.

permalink
report
parent
reply
2 points

Oh, I see. That would be interesting to benchmark too 👍

Anti Commercial AI thingy

CC BY-NC-SA 4.0

permalink
report
parent
reply

Rust

!rust@programming.dev

Create post

Welcome to the Rust community! This is a place to discuss about the Rust programming language.

Wormhole

!performance@programming.dev

Credits
  • The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)

Community stats

  • 753

    Monthly active users

  • 866

    Posts

  • 3.5K

    Comments