Open puzpuzpuz opened 1 year ago
I believe this is because of https://github.com/golang/go/issues/40301.
Having a special unsafe.Align[const int]
type wouldn't require adding compiletime-turing-completness to the language.
type slotOfPadded[I any] struct {
_ unsafe.Align[64]
slotOf[I]
_ unsafe.Align[64]
}
type slotOf[I any] struct {
turn uint64
item I
}
It would also allows to help solve bugs like the uint64 atomic on 32bits platforms.
I guess you could also leave unsafe.Sizeof
non const if used within functions but then it's even more inconsistent.
That suggestion seems to be #19057.
It has the same roots but it proposes hardcoded types instead of some const int parameter.
Edit: I see suggestions to make arrays of .Alligned
to have bigger alignments in #19057's comments, which is surprising be would work I guess.
I'm trying to add padding on a generic struct (the full code is here):
and getting a compiler error as a result:
The padding is required to both align atomic ops on the
turn
field (I need to deal with an array ofslotOfPadded
structs) and to prevent false sharing.The language doesn't seem to support this use case, but maybe there are any workarounds I'm not aware of. If there are none, it would be great to support adding padding on a generic struct.