Skip to content

cmd/go: generic function increases binary size #70215

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

Closed
rittneje opened this issue Nov 6, 2024 · 7 comments
Closed

cmd/go: generic function increases binary size #70215

rittneje opened this issue Nov 6, 2024 · 7 comments

Comments

@rittneje
Copy link
Contributor

rittneje commented Nov 6, 2024

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.

@gabyhelp
Copy link

gabyhelp commented Nov 6, 2024

Related Issues and Documentation

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

@adonovan
Copy link
Member

adonovan commented Nov 6, 2024

What did you expect to see?

There should not be any size difference.

Why do you expect that? Generic functions are fundamentally more complex than non-generic ones, and an executable contains all kinds of bookkeeping information (e.g. for debugging) beyond the object code itself, so it shouldn't be surprising that the generic version results in a larger file in some cases.

@dr2chase
Copy link
Contributor

dr2chase commented Nov 6, 2024

This is not a large enough problem to worry about.

@dr2chase dr2chase closed this as completed Nov 6, 2024
@rittneje
Copy link
Contributor Author

rittneje commented Nov 6, 2024

@adonovan Because it doesn't happen for any other generic function I've tried.

@dr2chase I'm reporting this as a compiler bug - it definitely seems like it's forgetting to remove something.

@adonovan
Copy link
Member

adonovan commented Nov 6, 2024

Because it doesn't happen for any other generic function I've tried.

Can you give an a priori (principled, not empirical) rebuttal to my argument?

@rittneje
Copy link
Contributor Author

rittneje commented Nov 6, 2024

@adonovan You asked why I expected that. And the reason is that I would have expected whatever bookkeeping should apply to all generic functions. But clearly that's not what happens (somehow). I don't think it's unreasonable to expect the compiler to work in a consistent fashion, or be surprised when it doesn't, especially because in this case it seems as though the compiler is forgetting to optimize something away. Whether this is some known or intentional limitation of the compiler or whatnot is not something I would be privy to.

@adonovan
Copy link
Member

adonovan commented Nov 6, 2024

I don't think it's unreasonable to expect the compiler to work in a consistent fashion, or be surprised when it doesn't, especially because in this case it seems as though the compiler is forgetting to optimize something away.

If you can find a specific element of the file that you think should have been optimized away but was not, then please do report a bug. But my point is that file size increases per se are not surprising.

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

No branches or pull requests

4 participants