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

unsafe: document a difference in behavior between unsafe.Sizeof(x) and reflect.TypeOf(x).Size() #67465

Closed
Gandem opened this issue May 17, 2024 · 4 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. Documentation FixPending Issues that have a fix which has not yet been reviewed or submitted. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@Gandem
Copy link
Contributor

Gandem commented May 17, 2024

Go version

go version go1.22.3 darwin/arm64

Output of go env in your module/workspace:

```shell GO111MODULE='on' GOARCH='arm64' GOBIN='' GOCACHE='/Users/nayef.ghattas/Library/Caches/go-build' GOENV='/Users/nayef.ghattas/Library/Application Support/go/env' GOEXE='' GOEXPERIMENT='' GOFLAGS='' GOHOSTARCH='arm64' GOHOSTOS='darwin' GOINSECURE='' GOMODCACHE='/Users/nayef.ghattas/go/pkg/mod' GONOPROXY='' GOOS='darwin' GOPATH='/Users/nayef.ghattas/go' GOPRIVATE='' GOPROXY='https://proxy.golang.org|direct' GOROOT='/opt/homebrew/Cellar/go/1.22.3/libexec' GOSUMDB='sum.golang.org' GOTMPDIR='' GOTOOLCHAIN='auto' GOTOOLDIR='/opt/homebrew/Cellar/go/1.22.3/libexec/pkg/tool/darwin_arm64' GOVCS='' GOVERSION='go1.22.3' GCCGO='gccgo' AR='ar' CC='cc' CXX='c++' CGO_ENABLED='1' GOMOD='/dev/null' GOWORK='' CGO_CFLAGS='-O2 -g' CGO_CPPFLAGS='' CGO_CXXFLAGS='-O2 -g' CGO_FFLAGS='-O2 -g' CGO_LDFLAGS='-O2 -g' PKG_CONFIG='pkg-config' GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/z1/9gbm9wxx6rl1l71csb2w18hc0000gq/T/go-build3531211748=/tmp/go-build -gno-record-gcc-switches -fno-common' ```

What did you do?

go doc unsafe.Sizeof and go doc reflect.Type

What did you see happen?

Per https://go.dev/play/p/ovkwhK0eSWV for a variable of interface static type:

  • unsafe.Sizeof() returns the size of the static type of the interface (which is 2*WordCount = 16 on most architectures)
  • reflect.TypeOf().Size() returns the size of the dynamic type of the interface (which depends on the number of fields and underlying padding of the struct implementing that interface)

What did you expect to see?

A statement either in go doc unsafe.Sizeof or in go doc reflect.Type that explains the difference in behavior between both functions for variables of interface static type.

As a matter of fact go doc reflect.Type mentions:

	// Size returns the number of bytes needed to store
	// a value of the given type; it is analogous to unsafe.Sizeof.
	Size() uintptr

Which can be slightly misleading, as unsafe.Sizeof(i) will return a different result than reflect.TypeOf(i).Size() if i is of interface static type (as reflect.TypeOf() returns the dynamic type of the interface as already documented in go doc reflect.TypeOf)

I've submitted the following CL as a documentation suggestion: https://go-review.googlesource.com/c/go/+/586275

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/586275 mentions this issue: unsafe: document the behavior of Sizeof on an interface

@dmitshur
Copy link
Contributor

CC @griesemer.

@dmitshur dmitshur added compiler/runtime Issues related to the Go compiler and/or runtime. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels May 17, 2024
@dmitshur dmitshur added this to the Backlog milestone May 17, 2024
@dmitshur dmitshur added the FixPending Issues that have a fix which has not yet been reviewed or submitted. label May 17, 2024
@itstarsun
Copy link

What you want is
reflect.TypeOf(&testAsInterface).Elem().Size() not reflect.TypeOf(testAsInterface).Size(), because reflect.TypeOf take an interface to the type you want.

See reflect.TypeFor for correct implementation:

go/src/reflect/type.go

Lines 3026 to 3033 in 1667dbd

// TypeFor returns the [Type] that represents the type argument T.
func TypeFor[T any]() Type {
var v T
if t := TypeOf(v); t != nil {
return t // optimize for T being a non-interface kind
}
return TypeOf((*T)(nil)).Elem() // only for an interface kind
}

@Gandem
Copy link
Contributor Author

Gandem commented May 18, 2024

What you want is
reflect.TypeOf(&testAsInterface).Elem().Size() not reflect.TypeOf(testAsInterface).Size(), because reflect.TypeOf take an interface to the type you want.

While reflect.TypeOf(&testAsInterface).Elem().Size() is indeed a way to get the size of the static type of the interface through reflect (which in that case will be equivalent to unsafe.Sizeof), I believe it's still worth documenting the slight difference in behavior between reflect.TypeOf(x).Size() and unsafe.Sizeof(x) when x is of interface static type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. Documentation FixPending Issues that have a fix which has not yet been reviewed or submitted. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants