You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
cmd/compile: optimized range memclr doesn't work with pointers to arrays #52635
@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
The following generates slightly more efficient code for my computer.
func (t*T) resetC() {
*t.a= [10]int{}
}
There's no runtime.memclrNoHeapPointers call at all. Instead the compiler seems to choose a suitable algorithm based on the size of the array. To me it seems like all of the loops above should compile to this.
It looks the memclr way is more efficient than the assignment way for small arrays.
Should the runtime internal unify the implementation for the two ways?
Activity
josharian commentedon Apr 30, 2022
As evidence that this is just a missed optimization, here is equivalent code using a new intermediate type:
This generates
runtime.memclrNoHeapPointers. (And also provides a workaround.)renthraysk commentedon May 1, 2022
There is a simpler workaround
for i := range *t.a {.josharian commentedon May 1, 2022
@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
renthraysk commentedon May 1, 2022
Yea, agreed. Actually thought I filed an issue for this, but seems just tweeted about it back in 2017, so my bad.
gopherbot commentedon May 1, 2022
Change https://go.dev/cl/403337 mentions this issue:
cmd/compile: support pointers to arrays in arrayClearrip-create-your-account commentedon May 1, 2022
The following generates slightly more efficient code for my computer.
There's no
runtime.memclrNoHeapPointerscall at all. Instead the compiler seems to choose a suitable algorithm based on the size of the array. To me it seems like all of the loops above should compile to this.zigo101 commentedon May 2, 2022
It looks the
memclrway is more efficient than the assignment way for small arrays.Should the runtime internal unify the implementation for the two ways?
cmd/compile: support pointers to arrays in arrayClear