Skip to content

Use range for instead of next in fold - #455

Merged
Ralith merged 2 commits into
Ralith:masterfrom
Koranir:fold-microoptimize
Aug 20, 2026
Merged

Use range for instead of next in fold#455
Ralith merged 2 commits into
Ralith:masterfrom
Koranir:fold-microoptimize

Conversation

@Koranir

@Koranir Koranir commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

Related (but doesn't fix): #351

Allows the compiler to optimise more by giving it extra information to work with, namely giving it proper length information instead of hoping it peeks through repeated while let calls.

By all rights it should be able to optimise without this change but possibly due to some non-optimal optimisation ordering it doesn't end up being able to vectorize loops, even though it inlines everything to essentially a range for in the end.

The for_each 100k benchmark goes from 100 µs to 22 µs on my system, about a 4x speedup (which tracks with using 128-bit SIMD instructions). Now this is a very contrived benchmark and I suspect actual gains will be much less impactful, but might as well..?

This doesn't change the speed of the iterate mut 100k benchmark, which doesn't use fold (perhaps there should be more documentation explaining that fold/for_each should be used when possible).

Allows the compiler to optimise more by giving it extra information to work with, namely giving it proper length information instead of hoping it peeks through repeated while let calls.

By all rights it should be able to optimise without this change but possibly due to some non-optimal optimisation ordering it doesn't end up being able to vectorize loops.

@Ralith Ralith left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@Ralith

Ralith commented Aug 20, 2026

Copy link
Copy Markdown
Owner

about a 4x speedup (which tracks with using 128-bit SIMD instructions).

Out of curiosity, do we actually see that in the output?

perhaps there should be more documentation explaining that fold/for_each should be used when possible

Given that the overwhelming majority of applications won't benefit, I'm reluctant to broadly recommend a much less ergonomic pattern.

@Ralith
Ralith merged commit fa15a71 into Ralith:master Aug 20, 2026
5 checks passed
@newDINO

newDINO commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

The for_each 100k benchmark has much higher improvement on my device: from 168.9us to 8.14us, which is around 95% percent time decrease !

The performance improvement is not only due to SIMD, but also the reduced branches. The next() based method has 2 branches per item while the for-range based only has 1 branch per item.

Hoping to see this published into crates.io soon.

@newDINO

newDINO commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

The performance improvement is not only due to SIMD, but also the reduced branches. The next() based method has 2 branches per item while the for-range based only has 1 branch per item.

My mistake, this improvement is actually due to #436

@newDINO

newDINO commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

The for_each 100k benchmark has much higher improvement on my device: from 168.9us to 8.14us, which is around 95% percent time decrease !

Another interesting point is that this result is tested when using hecs from another crate. When benchmarking in the hecs crate, the improvement is only 77%: from 36us to 8.4us.

@Ralith

Ralith commented Aug 31, 2026

Copy link
Copy Markdown
Owner

Hoping to see this published into crates.io soon.

My mistake, this improvement is actually due to #436

Note that #436 is already in the current release.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants