Skip to content
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

proposal: support padding for generic structs #62172

Open
puzpuzpuz opened this issue Aug 21, 2023 · 4 comments
Open

proposal: support padding for generic structs #62172

puzpuzpuz opened this issue Aug 21, 2023 · 4 comments
Labels
Milestone

Comments

@puzpuzpuz
Copy link

puzpuzpuz commented Aug 21, 2023

I'm trying to add padding on a generic struct (the full code is here):

type slotOfPadded[I any] struct {
	slotOf[I]
	pad [64 - (unsafe.Sizeof(slotOf[I]{}) % 64)]byte
}

type slotOf[I any] struct {
	turn uint64
	item I
}

and getting a compiler error as a result:

array length 64 - (unsafe.Sizeof(slotOf[I]{}) % 64) (value of type uintptr) must be constant

The padding is required to both align atomic ops on the turn field (I need to deal with an array of slotOfPadded 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.

@gopherbot gopherbot added this to the Proposal milestone Aug 21, 2023
@ericlagergren
Copy link
Contributor

I believe this is because of #40301.

@Jorropo
Copy link
Member

Jorropo commented Aug 21, 2023

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.

@zephyrtronium
Copy link
Contributor

That suggestion seems to be #19057.

@Jorropo
Copy link
Member

Jorropo commented Aug 21, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Incoming
Development

No branches or pull requests

5 participants