Re:Padding is hard
by kibwen on Friday October 09, 2015 @12:05PM
(#10360960)
If you're handing off a struct to C code, the order of your fields is going to be significant. If the Go compiler began reordering struct fields as an optimization, it would need to provide some way to disable that optimization on a per-struct basis (and unless Go could automatically detect which structs are crossing an FFI boundary, this optimization would need to be opt-in rather than opt-out in order to avoid breaking existing code). As a precedent, Rust deliberately does what you describe and reserves the right to reorder struct fields (though AFAIK it's not actually exercising this right yet), and in order to manually enforce a struct's layout you must tag it with the #[repr(C)] attribute.
[ Parent
| on HN ]
Go struct memory layout is currently undefined[0], so you're already not supposed to hand structs off to C, or to memcpy it to or from the wire. Unless you're using cgo maybe, I don't know if (or that) cgo provides any guarantee.
Is this mentioned in the spec anywhere? That piece of information was precisely what I was looking for, but wasn't able to find.
The spec doesn't define field ordering anywhere, which is why it's undefined. Russ Cox commented on the bug I linked essentially confirming it:
> It seems fine to me for the spec not to guarantee anything about struct field order in memory. The spec doesn't operate at that level.
And while he adds
> That said, no Go compiler should probably ever reorder struct fields.
the final result is struct memory layout is unspecified and implementation-dependent (essentially identical to repr(rust), as I understand it)
The intent of a specification is to be explicit, and in doing so it's poor form to say "this is undefined, so we're simply not going to mention it in the spec at all". If it's undefined then the spec should say so, especially if the alternative is for one to cast about in confusion until hopefully stumbling across the relevant documentation in, of all places, the bug tracker.
That is what FFI is for, right? Native structures do whatever they want. When gluing to C code, you use your FFI primitives to express the interfaces; and the structures which are expressed there are made to correspond to the C layout in some way.
You generally don't want to be constraining how data structures work in a language so that they can be handed off to C code directly. (Unless perhaps you're writing a language that is nearly a C dialect or something.)
You don't want to rearrange structs on every system call. That would be slow.
Re:Padding is hard
by tedunangst on Friday October 09, 2015 @03:05PM
(#10362563)
I think the idea is you would use the "FFI" type throughout the program, perhaps embedded in a larger structure if you need to track additional data.
The same thing is frequently done in C. Wire format data gets padded and __packed as necessary, and then that's usually the program representation as well.