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

runtime,cmd/compile: miss an optimization case for "struct{byte}" #49879

Closed
go101 opened this issue Nov 30, 2021 · 9 comments
Closed

runtime,cmd/compile: miss an optimization case for "struct{byte}" #49879

go101 opened this issue Nov 30, 2021 · 9 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@go101
Copy link

go101 commented Nov 30, 2021

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

$ go version
go version go1.17.3 linux/amd64

Does this issue reproduce with the latest release?

Yes

What did you do?

package interfaces

import "testing"

var r interface{}

type T8 struct{n byte}
func Benchmark_ByteField(b *testing.B) {
	for i := 0; i < b.N; i++ { r = T8{byte(i&255)} }
}

type T16 struct{n int16}
func Benchmark_Int16Field(b *testing.B) {
	for i := 0; i < b.N; i++ { r = T16{int16(i&255)} }
}

type T32 struct{n int32}
func Benchmark_Int32Field(b *testing.B) {
	for i := 0; i < b.N; i++ { r = T32{int32(i&255)} }
}

type T64 struct{n int64}
func Benchmark_Int64Field(b *testing.B) {
	for i := 0; i < b.N; i++ { r = T64{int64(i&255)} }
}

What did you expect to see?

Similar value boxing costs.

What did you see instead?

Boxing struct{n byte} values is more costly than others.

goos: linux
goarch: amd64
Benchmark_ByteField-4    	60815828	        19.89 ns/op
Benchmark_Int16Field-4   	341708020	         3.424 ns/op
Benchmark_Int32Field-4   	348755539	         3.403 ns/op
Benchmark_Int64Field-4   	348057927	         3.637 ns/op
@mknyszek mknyszek added this to the Backlog milestone Nov 30, 2021
@mknyszek mknyszek added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Nov 30, 2021
@mknyszek mknyszek changed the title runtime (or cmd/compile): miss an optimization case for "struct{byte}" runtime,cmd/compile: miss an optimization case for "struct{byte}" Nov 30, 2021
@mknyszek
Copy link
Contributor

mknyszek commented Nov 30, 2021

CC @randall77 @dr2chase

@cuonglm
Copy link
Member

cuonglm commented Nov 30, 2021

It seems to me that we need to extend dataWord to handle Type.SoleComponent() when fromType.Size() == 1:

Benchmark_ByteField-8    	1000000000	         0.6414 ns/op	       0 B/op	       0 allocs/op
Benchmark_Uint8Field-8   	1000000000	         0.6495 ns/op	       0 B/op	       0 allocs/op
Benchmark_Int16Field-8   	787386027	         1.590 ns/op	       0 B/op	       0 allocs/op
Benchmark_Int32Field-8   	663530320	         1.573 ns/op	       0 B/op	       0 allocs/op
Benchmark_Int64Field-8   	808163055	         1.574 ns/op	       0 B/op	       0 allocs/op

@randall77
Copy link
Contributor

randall77 commented Nov 30, 2021

Sounds good. Can dataWord just do fromType := n.Type().SoleComponent() at its first line? Then all the cases handle wrapping with struct{}.

@randall77
Copy link
Contributor

randall77 commented Nov 30, 2021

Or,

   fromType := n.Type()
   if s := fromType.SoleComponent(); s != nil { fromType = s }

@randall77
Copy link
Contributor

randall77 commented Nov 30, 2021

Then dataWordFuncName doesn't need to call SoleComponent, as its arg is already normalized like that.

@cuonglm
Copy link
Member

cuonglm commented Nov 30, 2021

@randall77 dataWordFuncName is also called in order.go, so it still needs to call SoleComponent, unless we duplicate your code above in order.go, too.

@randall77
Copy link
Contributor

randall77 commented Nov 30, 2021

A little bit unfortunate. We can lift to both callsites and then add an assertion in dataWordFuncName that its arg is already SoleComponented.

@gopherbot
Copy link

gopherbot commented Dec 1, 2021

Change https://golang.org/cl/367755 mentions this issue: cmd/compile: handle sole component for 1-byte type interface conversion

@cuonglm
Copy link
Member

cuonglm commented Dec 1, 2021

A little bit unfortunate. We can lift to both callsites and then add an assertion in dataWordFuncName that its arg is already SoleComponented.

I sent https://go-review.googlesource.com/c/go/+/367755 for quick prototyping. I'm a bit nervous with adding the assertion in dataWordFuncName:

if sc := fromType.SoleComponent(); sc != nil && sc == fromType {
    base.Fatalf("type is not sole component-ed")
}

looks a bit ugly to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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

5 participants