-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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: empty struct takes space #12884
Comments
Nice find! |
In general, they do not take space. E.g., this program prints 4:
The change to pad out structs that have a final zero-size field was to fix issue #9401. I think this is working as intended. |
So we should change type ProgInfo struct {
Flags uint32 // flag bits
Reguse uint64 // registers implicitly used by this instruction
Regset uint64 // registers implicitly set by this instruction
Regindex uint64 // registers used by addressing mode
_ struct{} // to prevent unkeyed literals
} to type ProgInfo struct {
_ struct{} // to prevent unkeyed literals
Flags uint32 // flag bits
Reguse uint64 // registers implicitly used by this instruction
Regset uint64 // registers implicitly set by this instruction
Regindex uint64 // registers used by addressing mode
} ... I guess? |
That should work to avoid the padding. |
ahh, I forgot about that trailing zero-sized field issue entirely.
thanks for pointing it out.
precise GC and zero-sized fields are subtle.
|
Moving the _ field to the top of the struct SGTM. On Fri, 9 Oct 2015 11:10 Minux Ma notifications@github.com wrote:
|
Perhaps instead of moving the _ field, augmenting the comment to mention However on 32 bit platforms, leaving the zero sized field at the end will On Fri, 9 Oct 2015 11:12 Dave Cheney dave@cheney.net wrote:
|
It won't take any space at the start of the struct. |
CL https://golang.org/cl/15660 mentions this issue. |
In case anyone's interested in investigating more structs that might benefit from reordering their fields, I put together github.com/mdempsky/maligned as a quick program to calculate struct sizes using cmd/compile's field layout rules, and compare against what they would be if the fields were sorted. Example:
|
How can one take the address of an unnamed field in a struct, zero sized or not ? In #9401 the type in question was
But in the example above the field is unnamed and inaccessible. |
It's possible at least via package reflect: http://play.golang.org/p/esuSUoKomT |
@mdempsky, yes of course, thank you for explaining to me. |
This should help `go vet` detecting invalid structs' copyings. See golang/go#8005 (comment) for details.
's comment at golang/go#12884 for details
Found while experimenting with https://golang.org/cl/15522.
Empty field (
_ struct{}
) takes space in Go 1.5, but not in Go 1.4or any previous Go releases. See http://play.golang.org/p/OTr-GLz2UN.
It's a compiler regression introduced by Go 1.5.
Tentatively labeled Go 1.5.2 as this can't be workaround.
The text was updated successfully, but these errors were encountered: