Go version
go1.22.0
Output of go env in your module/workspace:
What did you do?
Run the following benchmark:
package main
import "testing"
type Option interface{ isOption() }
type maxWindowSize uint64
func (maxWindowSize) isOption() {}
func MaxWindowSize(n uint64) Option { return maxWindowSize(n) }
var opt Option
func BenchmarkFunc(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
opt = MaxWindowSize(1024)
}
}
func BenchmarkDirect(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
opt = maxWindowSize(1024)
}
}
What did you see happen?
I see:
BenchmarkFunc-24 109294134 11.11 ns/op 8 B/op 1 allocs/op
BenchmarkDirect-24 1000000000 0.4708 ns/op 0 B/op 0 allocs/op
What did you expect to see?
I expect to see:
BenchmarkFunc-24 1000000000 0.4708 ns/op 0 B/op 0 allocs/op
BenchmarkDirect-24 1000000000 0.4708 ns/op 0 B/op 0 allocs/op
The MaxWindowSize function is inlineable, so I would expect it to be identical to declaring a maxWindowSize literal directly.
This seems to be an odd interaction between static allocations, constant propagation, and inlining. I'm not sure if the title properly reflects what's going on.
Go version
go1.22.0
Output of
go envin your module/workspace:What did you do?
Run the following benchmark:
What did you see happen?
I see:
What did you expect to see?
I expect to see:
The
MaxWindowSizefunction is inlineable, so I would expect it to be identical to declaring amaxWindowSizeliteral directly.This seems to be an odd interaction between static allocations, constant propagation, and inlining. I'm not sure if the title properly reflects what's going on.