Escape analysis result and benchmark result conflict

75 views
Skip to first unread message

tapi...@gmail.com

unread,
Jun 23, 2021, 1:14:36 AM6/23/21
to golang-nuts
The code:

package concat

import (
    "testing"
)

var s33 = []byte{32: 'b'}
var a = string(s33)


func Benchmark_e_33(b *testing.B) {
    for i := 0; i < b.N; i++ {
        _ = a + a // a + a does not escape
    }
}

"go test -gcflags=-m" shows: a + a does not escape
but "go test -bench=. -benchmem" shows:
Benchmark_e_33-4       16544446            68.35 ns/op          80 B/op           1 allocs/op

In my impression, memory blocks allocated on stack are not counted in the bench result.

tapi...@gmail.com

unread,
Jun 23, 2021, 2:02:28 AM6/23/21
to golang-nuts
Is it possible that gc automatically replaces the "_" with a hidden declared package-level variable?
Reply all
Reply to author
Forward
0 new messages