-
Notifications
You must be signed in to change notification settings - Fork 797
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve perf or String.iter and String.iteri by 25% #9497
Improve perf or String.iter and String.iteri by 25% #9497
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Just some suggestions about the comments. The code is reasonable and there is evidence in the issue that this works well.
Co-authored-by: Phillip Carter <[email protected]>
Co-authored-by: Phillip Carter <[email protected]>
Strange, after i clicked the "Commit suggestion" in the online version of Github and clicked "resolve conversation", it marked the other suggestion as resolved automatically. Maybe because the sentences were equal, even though on different locations in the file. Anyway, all resolved now, tx for review! And I added some tests, since unrolling can introduce subtle new bugs: b1d250e
@cartermp, tx, though critics might say that "one source is no source". Though it's highly unlikely that this introduces a regression on anyone's system, cpu cache sizes and speed, prefetch, branch prediction, physical cores etc can produce different timings here. Of note is also that the CLR team is endeavoring to get better loop-unrolling done in RyuJIT, but this may still be a couple of months/years off. |
Looks like there are more failures. |
It seems very strange to have to unroll manually... it's trivially optimisable by the JIT. Is it possible to debug why the JIT isn't unrolling when it's a simple |
@saul, my guess is that the hof function makes it hard to predict for the JIT that unrolling is safe or guaranteed to improve perf. Also, unrolling in the JIT is done quite rarely (the conditions have to be "just right"), there are efforts underway to improve the situation. It's not uncommon to unroll, but it depends on many factors whether it makes sense. If the callback function is nontrivial, the benefit of unrolling gets undone. But for small inlineable functions, it shows significant improvement. |
Maybe the new tests. I can't see the failure on mobile, but I'll check as soon as I'm back. |
@saul, this report gives some insight: dotnet/runtime#4248 As can be seen, even the most trivial loops do not get unrolled presently in RyuJIT (legacy JIT and x86 RyuJIT do better here) and the examples there are even way simpler than this. Since that was 5 years ago, I don't think we should expect this to change anytime soon. |
@cartermp: there was indeed a bug, glad I wrote the extra tests, which showed it. Since this changed the code for |
After discussion with @KevinRansom, we decided to drop this PR on the grounds that loop-unrolling should eventually be done by the JIT team. This PR can be used as an example where they can improve. |
Following up on the findings and timings presented at #9390 (comment), this PR improves performance to about 25%, sometimes more on modern processors.