-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
cmd/compile: loops compiled differently when using range or index #31205
Comments
What is the performance difference? |
Using the following benchmark (https://play.golang.org/p/rCxWqmQXvq5), I get these numbers:
So, using an explicit index seems to be slower than using "range". |
Something I noticed is that, if I write "sumRange" as follows, the difference disappears: func sumRange(slc []byte) byte {
res := byte(0)
for i := range slc {
res += slc[i]
}
return res
} |
This appears to occur because of choices in the scheduler.
The inner loop in
The schedule in This has been an issue for a while. I noted it in 2016: #16122 (comment) Perhaps just a tweak to the scheduler is needed. I'm unclear as to why the scheduler makes different choices here. Might just be how the values happened to be ordered on entry to the scheduler. |
No longer reproduces. Fixed in Go 1.21 |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes.
What did you do?
I compiled two functions that sum all the elements of a slice of bytes using "range" or using an index:
"Range" version:
Index version:
What did you expect to see?
I expected both versions of the function to compile to the exact same code.
What did you see instead?
Instead, the looping part of the function is different, resulting in differences in performance:
"Range" version:
Index version:
The text was updated successfully, but these errors were encountered: