-
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: optimized range memclr doesn't work with pointers to arrays #52635
Comments
As evidence that this is just a missed optimization, here is equivalent code using a new intermediate type: type T struct {
c *A
}
type A struct {
x [10]int
}
func (t *T) resetC() {
for i := range t.c.x {
t.c.x[i] = 0
}
} This generates |
There is a simpler workaround |
@renthraysk I am astonished that that works, and I wrote the original optimization. Now that you say it, I can imagine why, although it has to do with weird quirks in the guts of the compiler. Anyway, I still think this is worth fixing. :P |
Yea, agreed. Actually thought I filed an issue for this, but seems just tweeted about it back in 2017, so my bad.
|
Change https://go.dev/cl/403337 mentions this issue: |
The following generates slightly more efficient code for my computer. func (t *T) resetC() {
*t.a = [10]int{}
} There's no |
It looks the |
resetA compiles to a loop. resetB compiles to a call to runtime.memclrNoHeapPointers. I believe that resetA should also call memclrNoHeapPointers.
This showed up as a hot spot in my code today (with much larger arrays).
cc @randall77 @cuonglm @mdempsky
The text was updated successfully, but these errors were encountered: