Skip to content

cmd/go: generic function increases binary size #70215

Closed
@rittneje

Description

@rittneje

Go version

go version go1.22.8 darwin/amd64

Output of go env in your module/workspace:

n/a

What did you do?

I created two variations of a function - one that is generic and one that is not - and compared the resulting binary size.

package main

func New(x string) *string {
	return &x
}

var s *string

func main() {
	s = New("abc")
}
$ go build main.go && wc -c  main
 1371632 main

package main

func New[T any](x T) *T {
	return &x
}

var s *string

func main() {
	s = New("abc")
}
$ go build main.go && wc -c  main
 1371664 main

What did you see happen?

The version that uses generics is 32 bytes larger for some reason.

I have tested a few other variants of generic vs. non-generic function, but have not reproduced this issue with any of them. There seems to be something peculiar about this particular function.

What did you expect to see?

There should not be any size difference.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions