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
type T struct {
x int
pad [7]int
}
type U struct {
x int
y string
pad [5]int
}
func f(t *T) {
*t = T{x: 7}
}
func g(u *U) {
*u = U{x: 7}
}
For f we set *t by just doing a copy from a global constant.
For g we first allocate a temporary on the stack, initialize it, then copy it to the result.
There's no need for g to allocate a stack temporary. The initializing value, even if y is a non-empty string constant, is constant. We could do just like f does and build a global constant for the initializer. (g needs to use typedmemmove instead of duffcopy because it needs a write barrier, at least with the new hybrid barriers.)
See also #18636. Even for f maybe we should probably write/zero fields directly instead of copying.
The text was updated successfully, but these errors were encountered:
I think "strings" here is a red herring. I believe the issue is just that the value contains pointers. For example, we generate identical code if you change the definition of U to:
Simply removing that entire chunk (lines 414--423) eliminates the stack copy so that we just typedmemmove directly from static data.
We used to need that code because removing it caused net/http to become flaky (see review discussion on CL 21242). I vaguely recall a couple weeks ago looking at this again, and finding that it wasn't necessary anymore since some other CL (maybe c39918a?). I didn't establish a good confidence about why it's not necessary anymore though. (Of course, I have no confidence about why it used to be necessary either.)
For
f
we set*t
by just doing a copy from a global constant.For
g
we first allocate a temporary on the stack, initialize it, then copy it to the result.There's no need for
g
to allocate a stack temporary. The initializing value, even ify
is a non-empty string constant, is constant. We could do just likef
does and build a global constant for the initializer. (g
needs to use typedmemmove instead of duffcopy because it needs a write barrier, at least with the new hybrid barriers.)See also #18636. Even for
f
maybe we should probably write/zero fields directly instead of copying.The text was updated successfully, but these errors were encountered: