Would be grateful if someone can point out the obvious to us for the following 4 benchmarks.
We can understand why there would be 1 allocation for A, but why is B and C shows zero allocation and zero memory usage? Thanks in advance for any pointers.
Go-version: Go 1.3beta2
----------------------
type Dummy struct {
A int64
B int64
}
type Dummy2 struct {
A int64
}
func BenchmarkA(t *testing.B) {
var r *Dummy
for i := 0; i < t.N; i++ {
r = &Dummy{A: int64(i)}
_ = r
}
}
func BenchmarkB(t *testing.B) {
for i := 0; i < t.N; i++ {
r := &Dummy{A: int64(i)}
_ = r
}
}
func BenchmarkC(t *testing.B) {
var r Dummy
for i := 0; i < t.N; i++ {
r = Dummy{A: int64(i)}
_ = r
}
}
func BenchmarkD(t *testing.B) {
var r *Dummy2
for i := 0; i < t.N; i++ {
r = &Dummy2{A: int64(i)}
_ = r
}
}
------ Result ------
type Dummy struct {
A int64
B int64
}
type Dummy2 struct {
A int64
}
func BenchmarkA(t *testing.B) {
var r *Dummy
for i := 0; i < t.N; i++ {
r = &Dummy{A: int64(i)}
_ = r
}
}
func BenchmarkB(t *testing.B) {
for i := 0; i < t.N; i++ {
r := &Dummy{A: int64(i)}
_ = r
}
}
func BenchmarkC(t *testing.B) {
var r Dummy
for i := 0; i < t.N; i++ {
r = Dummy{A: int64(i)}
_ = r
}
}
func BenchmarkD(t *testing.B) {
var r *Dummy2
for i := 0; i < t.N; i++ {
r = &Dummy2{A: int64(i)}
_ = r
}
}
---- Benchmark Result -----
BenchmarkA 100000000 80.4 ns/op 16 B/op 1 allocs/op
BenchmarkB 5000000000 2.36 ns/op 0 B/op 0 allocs/op
BenchmarkC 10000000000 1.17 ns/op 0 B/op 0 allocs/op
BenchmarkD 200000000 40.9 ns/op 8 B/op 0 allocs/op
-------------------------------
Stranger to us still is that for D where the struct contains only 1 less int64 var, the benchmark shows 1/2 the memory which is correct, but now has zero allocation?
So we are confused at two fronts. 1) Why is B and C showing zero memory and zero allocation? 2) Why is A and D off by 1 allocation?
Thanks.
Xing