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

cmd/compile: inline-able function with named results performs worse #48507

Open
go101 opened this issue Sep 21, 2021 · 3 comments
Open

cmd/compile: inline-able function with named results performs worse #48507

go101 opened this issue Sep 21, 2021 · 3 comments
Labels
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. Performance
Milestone

Comments

@go101
Copy link

go101 commented Sep 21, 2021

What version of Go are you using (go version)?

$ go version
go version go1.17.1 linux/amd64

Does this issue reproduce with the latest release?

Yes

What did you do?

package functions

import "testing"

const N = 1<<12
var buf = make([]byte, N)
var r [128][N]byte

func Benchmark_SliceToArray_Named(b *testing.B) {
	for i := 0; i < b.N; i++ {
		r[i&127] = SliceToArray_Named(buf)
	}
}

func Benchmark_SliceToArray_Unnamed(b *testing.B) {
	for i := 0; i < b.N; i++ {
		r[i&127] = SliceToArray_Unnamed(buf)
	}
}

func SliceToArray_Named(b []byte) (ret [N]byte) {
	ret = *(*[N]byte)(b)
	return
}

func SliceToArray_Unnamed(b []byte) [N]byte {
	return *(*[N]byte)(b)
}

What did you expect to see?

No performance difference

What did you see instead?

The named reult version is slower than the anonymous result version.

Benchmark_SliceToArray_Named-4     	 2983450	       397.7 ns/op
Benchmark_SliceToArray_Unnamed-4   	 4106510	       293.0 ns/op

It looks this is related to code inlining.

@randall77
Copy link
Contributor

Related #20859

@go101
Copy link
Author

go101 commented Sep 21, 2021

Yes, some related, but not totally the same.
If we add the //go:noinline directive for the two functions,
then there is not performance difference.
That means the code generated for the two functions are the same
if they are not inlined, but not the same if they are inlined.

@ALTree ALTree added NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Performance labels Sep 21, 2021
@ALTree ALTree added this to the Unplanned milestone Sep 21, 2021
@randall77
Copy link
Contributor

There is an extra zeroing of the named return value that doesn't exist in the unnamed one.

@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Jul 13, 2022
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. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Performance
Projects
None yet
Development

No branches or pull requests

4 participants