News for hackers. Stuff that matters.
Re:Padding is hard by zeveb on Friday October 09, 2015 @11:27AM (#10360650)
Given how much Go already does for one—and given the static nature of Go binaries—is there any reason why the compiler shouldn't reorder structs so that they take the least space possible?

I can see that that would prevent one from reliably dumping memory to disk and then re-reading it, but It's hardly reliable to do that anyway. One should be using an encoding anyway…

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.
Re:Padding is hard by masklinn on Friday October 09, 2015 @12:35PM (#10361256)
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.

[0] https://github.com/golang/go/issues/10014

Re:Padding is hard by kibwen on Saturday October 10, 2015 @02:10AM (#10364675)
Is this mentioned in the spec anywhere? That piece of information was precisely what I was looking for, but wasn't able to find.
Re:Padding is hard by masklinn on Saturday October 10, 2015 @04:29AM (#10364902)
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)

Re:Padding is hard by kibwen on Saturday October 10, 2015 @02:02PM (#10366311)
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.
Re:Padding is hard by kazinator on Friday October 09, 2015 @12:50PM (#10361422)
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.)

Re:Padding is hard by ori_b on Friday October 09, 2015 @02:29PM (#10362314)
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.

Re:Padding is hard by adonovan on Friday October 09, 2015 @12:17PM (#10361093)
The most compact representation is not always the most efficient. If two fields in the same struct variable are accessed by different goroutines, placing them far apart so that they reside in different cache lines may avoid a great deal of contention.
Re:Padding is hard by masklinn on Friday October 09, 2015 @12:36PM (#10361268)
Except technically struct memory layout is currently undefined in Go so you're relying on an implementation detail.
Re:Padding is hard by adonovan on Friday October 09, 2015 @12:53PM (#10361456)
This kind of optimized placement could in principle be done by a sufficiently smart compiler, but no such compiler exists. That's why existing compilers allow their users to control layout by, as you say, relying on an implementation detail.
Re:Padding is hard by masklinn on Friday October 09, 2015 @01:05PM (#10361565)
No, the language can specify source—order struct layout, either by default or opt-in.
Re:Padding is hard by coldtea on Friday October 09, 2015 @12:03PM (#10360943)
>Given how much Go already does for one

How much is that? As far as modern (2000 and onwards) languages Go, Golang and the compiler are pretty barebones. The compiler especially is as basic as it gets...

Re:Padding is hard by IshKebab on Friday October 09, 2015 @01:46PM (#10361952)
> The compiler especially is as basic as it gets

Which is fantastic. Nobody likes the bazillion options GCC has.

Re:Padding is hard by pcwalton on Friday October 09, 2015 @02:02PM (#10362105)
People do, however, like (and rely on) its enormous suite of optimizations.
Re:Padding is hard by gtfierro on Friday October 09, 2015 @11:50AM (#10360849)
Going beyond this being a minor optimization that isn't going to matter for most people, my guess is that they probably don't reorder structs because it may not be what the programmer intended. When you start introducing mechanisms for altering how the compiler sees a certain piece of code (like a struct definition), then you also need to give the programmer a way of expressing which thing they want the compiler to do, like with __attribute__ in C. Adding that feature in a straightforward way would probably involve introducing more syntax, which Go wants to avoid.
Re:Padding is hard by masklinn on Friday October 09, 2015 @11:54AM (#10360878)
At the same time, getting either source-layout guarantees or a way to explicitly specify layout is necessary for some task types, to avoid false sharing for instance. Go currently does neither.
Re:Padding is hard by Someone on Friday October 09, 2015 @12:19PM (#10361112)
I would guess that their stance is "If you care about that, do it yourself"

IMO, that is the right attitude, given that moving fields around may interfere with how parts of large structures map to cache lines, and that may have huge performance impacts, if some parts of a structure are rarely accessed.

Yes, the compiler could try to optimize that, too, but saying that is a hard problem is an understatement, and it needs statistics that the compiler doesn't have available. So, it definitely is unsuitable for go's goal of fast compilation.

Re:Padding is hard by masklinn on Friday October 09, 2015 @12:37PM (#10361283)
> I would guess that their stance is "If you care about that, do it yourself"

No, struct layout is currently undefined[0] so you can't do that yourself. Any manual packing you defined today may or may not hold across platforms or future go revisions.

[0] https://github.com/golang/go/issues/10014

Re:Padding is hard by neilc on Friday October 09, 2015 @12:39PM (#10361307)
> why the compiler shouldn't reorder structs so that they take the least space possible

I think you'd get most of the benefit by writing a static analysis tool (a la "go vet") that suggests when reordering a struct's fields would reduce memory consumption.

Re:Padding is hard by masklinn on Friday October 09, 2015 @02:00PM (#10362097)
Except Go doesn't guarantee source-order struct layout, so that'd be a suggestion relying on implementation details (see also: relying on scheduling order of goroutines).