Discussion:
[go-nuts] sync/atomic 64-bit alignment
enormouspenguin
2015-07-25 14:44:35 UTC
Permalink
Hi,

In http://golang.org/pkg/sync/atomic/#pkg-note-BUG, I quoted:

"...The first word in a global variable or in an allocated struct or slice
can be relied upon to be 64-bit aligned..."

If I have an array of structs, are the first words of each and every
structs in the array automatically 64-bit aligned or just the first one? Do
I need to know the size of a word for each build target in order to make it
work correctly? If so, how can I programatically get the size?

Thanks very much!
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Matt Harden
2015-07-25 15:53:55 UTC
Permalink
http://golang.org/pkg/unsafe/#Sizeof will tell you the size of a type. Also
see AlignOf and OffsetOf. I don't think you can rely on every struct in a
slice or array to be aligned. You can add padding where necessary to ensure
alignment. Of course alignment isn't guaranteed to remain the same across
different compilers or different versions of the same compiler.

My suggestion would be to split the 64 bit fields out into their own
array/slice; then this will be 64-bit aligned on all platforms.
Post by enormouspenguin
Hi,
"...The first word in a global variable or in an allocated struct or slice
can be relied upon to be 64-bit aligned..."
If I have an array of structs, are the first words of each and every
structs in the array automatically 64-bit aligned or just the first one? Do
I need to know the size of a word for each build target in order to make it
work correctly? If so, how can I programatically get the size?
Thanks very much!
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
enormouspenguin
2015-07-25 18:10:17 UTC
Permalink
Post by Matt Harden
http://golang.org/pkg/unsafe/#Sizeof will tell you the size of a type.
Also see AlignOf and OffsetOf.
Thanks Matt, but I think you misread my question. I know about Sizeof,
Alignof and Offsetof but what I want to know is how to find out the size of
a *word*. Is it the same size as an int or uint var?
Post by Matt Harden
I don't think you can rely on every struct in a slice or array to be
aligned. You can add padding where necessary to ensure alignment. Of course
alignment isn't guaranteed to remain the same across different compilers or
different versions of the same compiler.
The piece of doc I quoted is so confusing. Is a struct in an allocated
array considered allocated struct? If so, should all the structs in the
array have its first word 64-bit aligned? The confusion is killing me.
Post by Matt Harden
My suggestion would be to split the 64 bit fields out into their own
array/slice; then this will be 64-bit aligned on all platforms.
By fields do you mean all the fields of primitive types only or fields that
could be struct (on and on recursively)? If I have to split all the fields
to get 64-bit aligned guarantee, then what would we need struct for?
Couldn't rearrange (and add padding if necessary) inside a struct be enough?
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
enormouspenguin
2015-07-25 18:17:57 UTC
Permalink
Also, another source of my confusion: Is array and slice treated equally in
the quoted doc?
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Ian Lance Taylor
2015-07-25 19:12:05 UTC
Permalink
Post by enormouspenguin
Also, another source of my confusion: Is array and slice treated equally in
the quoted doc?
Yes: the first word of an allocated array will be aligned to a 64-bit boundary.

Ian
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
enormouspenguin
2015-07-26 08:04:29 UTC
Permalink
Just to make sure that I got it right in my mind:

+ If I have an array (or slice) of pointers to structs (all the pointers is
not nil), does that means that I get the guarantee of 64-bit aligned first
word for each and every referenced structs? Also, if
unsafe.Sizeof(unsafe.Pointer(&v)) == 8 (64 bits), are all of the pointers
in the array automatically 64-bit aligned too?

+ Could I rely on build tags to conditionally add necessary padding for
specific target archs at compile-time? Such as:

file1.go:

// +build amd64

package main

type S struct {
x int64
y int //or uint or uintptr or unsafe.Pointer
}

var (
s S //s.x is 64-bit aligned by default
arrS [100]S //x in all array items are 64-bit aligned.
)

file2.go:


// +build 386 arm

package main

type S struct {
x int64
y int //or uint or uintptr or unsafe.Pointer
_ int32 //padding, could also be uint32, float32, rune, int, uint, uintptr
or unsafe.Pointer
}

/*
Could I shorten it like this?

type S struct {
x int64
y, _ int
}
*/

var (
s S //s.x is 64-bit aligned by default
arrS [100]S //x in all array items are 64-bit aligned.
)
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Ian Lance Taylor
2015-07-26 20:59:40 UTC
Permalink
Post by enormouspenguin
+ If I have an array (or slice) of pointers to structs (all the pointers is
not nil), does that means that I get the guarantee of 64-bit aligned first
word for each and every referenced structs?
Yes.
Post by enormouspenguin
Also, if
unsafe.Sizeof(unsafe.Pointer(&v)) == 8 (64 bits), are all of the pointers in
the array automatically 64-bit aligned too?
Yes. Ideally, though, you should check unsafe.Alignof.
Post by enormouspenguin
+ Could I rely on build tags to conditionally add necessary padding for
Yes.
Post by enormouspenguin
/*
Could I shorten it like this?
type S struct {
x int64
y, _ int
}
*/
var (
s S //s.x is 64-bit aligned by default
arrS [100]S //x in all array items are 64-bit aligned.
)
On a 32-bit system, yes.

Ian
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Ian Lance Taylor
2015-07-25 19:11:23 UTC
Permalink
Post by enormouspenguin
Post by Matt Harden
http://golang.org/pkg/unsafe/#Sizeof will tell you the size of a type.
Also see AlignOf and OffsetOf.
Thanks Matt, but I think you misread my question. I know about Sizeof,
Alignof and Offsetof but what I want to know is how to find out the size of
a *word*. Is it the same size as an int or uint var?
Yes.
Post by enormouspenguin
Post by Matt Harden
I don't think you can rely on every struct in a slice or array to be
aligned. You can add padding where necessary to ensure alignment. Of course
alignment isn't guaranteed to remain the same across different compilers or
different versions of the same compiler.
The piece of doc I quoted is so confusing. Is a struct in an allocated array
considered allocated struct? If so, should all the structs in the array have
its first word 64-bit aligned? The confusion is killing me.
The quote is "The first word in a global variable or in an allocated
struct or slice can be relied upon to be 64-bit aligned." The key
phrase there is "allocated", not "struct or slice." The first word in
a struct or slice that is allocated via new or via a composite literal
will be 64-bit aligned. A struct inside a slice or array is not
allocated by itself, and no particular alignment is guaranteed beyond
that of unsafe.AlignOf.

Ian
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Dave Cheney
2015-07-25 18:56:56 UTC
Permalink
Post by enormouspenguin
If I have an array of structs, are the first words of each and every
structs in the array automatically 64-bit aligned or just the first one?

Potentially just the first one, it depends if the size of each element,
plus padding, is a multiple of 8.
Post by enormouspenguin
Do I need to know the size of a word for each build target in order to
make it work correctly?

Potentially, you may have to special case // +build arm, 386
Post by enormouspenguin
If so, how can I programatically get the size?
Would something like this work ? http://play.golang.org/p/SXfSY_3cig
Post by enormouspenguin
Hi,
"...The first word in a global variable or in an allocated struct or slice
can be relied upon to be 64-bit aligned..."
If I have an array of structs, are the first words of each and every
structs in the array automatically 64-bit aligned or just the first one? Do
I need to know the size of a word for each build target in order to make it
work correctly? If so, how can I programatically get the size?
Thanks very much!
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Mateusz Czapliński
2015-07-27 12:59:07 UTC
Permalink
Post by enormouspenguin
"...The first word in a global variable or in an allocated struct or slice
can be relied upon to be 64-bit aligned..."
@core-team: is this, or could it be, checked by `go vet`, taking into
account all the slice/goarch/whatever-related special cases?

Thanks,
/M.
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Ian Lance Taylor
2015-07-27 15:57:23 UTC
Permalink
Post by Mateusz Czapliński
Post by enormouspenguin
"...The first word in a global variable or in an allocated struct or slice
can be relied upon to be 64-bit aligned..."
@core-team: is this, or could it be, checked by `go vet`, taking into
account all the slice/goarch/whatever-related special cases?
I assume you are asking: could go vet check whether a value passed to
a sync/atomic function is not 64-bit aligned? The answer to that
question is yes. It could not be 100% reliable, of course, but it
could catch a number of cases. I suggest that you open an issue for
that at https://golang.org/issue. Thanks.

Ian
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Mateusz Czapliński
2015-07-27 20:10:31 UTC
Permalink
Post by Ian Lance Taylor
Post by Mateusz Czapliński
Post by enormouspenguin
"...The first word in a global variable or in an allocated struct or
slice
Post by Mateusz Czapliński
Post by enormouspenguin
can be relied upon to be 64-bit aligned..."
@core-team: is this, or could it be, checked by `go vet`, taking into
account all the slice/goarch/whatever-related special cases?
I assume you are asking: could go vet check whether a value passed to
a sync/atomic function is not 64-bit aligned? The answer to that
question is yes. It could not be 100% reliable, of course, but it
could catch a number of cases. I suggest that you open an issue for
that at https://golang.org/issue. Thanks.
Ian
Done: http://golang.org/issue/11891
Thanks
/M.
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
enormouspenguin
2015-07-28 03:49:05 UTC
Permalink
Post by Mateusz Czapliński
Done: http://golang.org/issue/11891
Thanks
/M.
Great job firing an issue. It's always better to have an automated check.

All this padding, aligning making me feel like I am in C or C++ world,
although I know only a tiny bit about them. I think that's one of the
beauties of Go - usually high and sometimes low enough to play without the
fear that C or C++ bring.
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
titarooss
2015-07-30 01:55:16 UTC
Permalink
I'd like to see alignment directives in the language instead of padding,
but I'm not sure this is possible. Alignment issues were brought up by a
few clients, and my only answer was padding. Everyone of them complained
and called it ugly but I had nothing for them. I'm not an expert in golang
and I'm still looking for possible answers.
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Egon
2015-07-30 06:20:34 UTC
Permalink
Post by titarooss
I'd like to see alignment directives in the language instead of padding,
but I'm not sure this is possible. Alignment issues were brought up by a
few clients, and my only answer was padding. Everyone of them complained
and called it ugly but I had nothing for them. I'm not an expert in golang
and I'm still looking for possible answers.
At the moment you probably could create something like a vetter that checks
whether fields have the correct alignment, e.g.

type Example struct {

}
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Egon
2015-07-30 06:23:16 UTC
Permalink
Post by Egon
Post by titarooss
I'd like to see alignment directives in the language instead of padding,
but I'm not sure this is possible. Alignment issues were brought up by a
few clients, and my only answer was padding. Everyone of them complained
and called it ugly but I had nothing for them. I'm not an expert in golang
and I'm still looking for possible answers.
At the moment you probably could create something like a vetter that
checks whether fields have the correct alignment, e.g.
type Example struct {
}
type Example struct {
X byte
Y int64 // align to 64
}

And I don't think it's just alignment, it's about cache locality and the
whole SOA vs AOS issues. "jai" had some interesting ideas with respect to
it.
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Continue reading on narkive:
Search results for '[go-nuts] sync/atomic 64-bit alignment' (Questions and Answers)
10
replies
What exactly is a leap year?
started 2007-12-29 09:51:05 UTC
astronomy & space
Loading...