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
proposal: cmd/compile: allow optimizing struct field alignment at compile time #81874
I use fieldalignment to get the best possible memory layout for structs.
One downside is that the optimal field order for memory usage is not necessarily the most readable order for humans. Fields that belong together logically may introduce additional padding.
For example, I may naturally want to think about a struct like this:
typeResourcestruct { // struct with 104 pointer bytes could be 72IDintNamestringKindstringCreatedAtint64UpdatedAtint64PriorityintAssignedmap[string]stringTasks []stringTags []string
}
But I do not want the order of the fields to constrain fieldalignment. I still want to use the optimal layout for the entire struct, even if that means rearranging them.
It would be useful to have a struct-level annotation that explicitly communicates this intent.
For a struct marked this way, fieldalignment could assume that field order has no logical significance and freely reorder all fields for the optimal memory layout.
The exact comment syntax is not important to me. The main idea is to provide an explicit opt-in that means:
For this struct, I do not care about logical field order. Use the optimal layout
I think this would make it easier to distinguish structs where source ordering should be treated conservatively from structs where unrestricted reordering is desirable.
changed the title [-]proposal: fieldalignment: allow optimizing struct fields at compile time[/-][+]proposal: cmd/compile: allow optimizing struct field alignment at compile time[/+]on Sep 30, 2026
I'm all for this idea, because it keeps the source human-readable, and the outcome cpu & memory optimized.
If an external tool can do this, it can't be hard to fix this in the compiler.
Activity
gabyhelp commentedon Sep 30, 2026
Related Issues
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)
ianlancetaylor commentedon Sep 30, 2026
Sounds like a dup of #53048.
[-]proposal: fieldalignment: allow optimizing struct fields at compile time[/-][+]proposal: cmd/compile: allow optimizing struct field alignment at compile time[/+]seankhliao commentedon Sep 30, 2026
#10014 seems to be the last time this was discussed in depth?
marcelloh commentedon Sep 30, 2026
I'm all for this idea, because it keeps the source human-readable, and the outcome cpu & memory optimized.
If an external tool can do this, it can't be hard to fix this in the compiler.