range-over-func has two more alloc than direct-call-func Why?

196 views
Skip to first unread message

qiulaidongfeng

unread,
Mar 12, 2024, 7:06:21 AM3/12/24
to golang-nuts

For the following code


https://go.dev/play/p/ynn7ihKXrp0?v=gotip


I got the benchmark


BenchmarkBelow3-16 9523 147772 ns/op 17 B/op 2 allocs/op

BenchmarkBelow3direct-16 10000 107335 ns/op 0 B/op 0 allocs/op


range-over-func has two more alloc than direct-call-func.


Why?

peterGo

unread,
Mar 12, 2024, 10:54:36 AM3/12/24
to golang-nuts
qiulaidongfeng,

-gcflags=-m=2

peter

qiulaidongfeng

unread,
Mar 13, 2024, 6:55:50 AM3/13/24
to golang-nuts
I got the information

./main_test.go:23:3: func literal escapes to heap:
./main_test.go:23:3:   flow: {heap} = &{storage for func literal}:
./main_test.go:23:3:     from func literal (spill) at ./main_test.go:23:3
./main_test.go:23:3:     from .autotmp_3(func literal) (call parameter) at ./main_test.go:23:3
./main_test.go:23:24: BenchmarkBelow3 capturing by ref: n (addr=false assign=true width=8)
./main_test.go:23:3: BenchmarkBelow3 capturing by ref: #exit1 (addr=false assign=true width=1)
./main_test.go:23:3: #exit1 escapes to heap:
./main_test.go:23:3:   flow: {storage for func literal} = &#exit1:
./main_test.go:23:3:     from #exit1 (captured by a closure) at ./main_test.go:23:3
./main_test.go:23:3:     from #exit1 (reference) at ./main_test.go:23:3
./main_test.go:21:22: b does not escape
./main_test.go:9:15: f does not escape
./main_test.go:11:14: f does not escape
./main_test.go:23:3: moved to heap: #exit1
./main_test.go:23:24: func literal does not escape
./main_test.go:23:24: func literal does not escape
./main_test.go:23:3: func literal escapes to heap
./main_test.go:31:13: func literal escapes to heap:
./main_test.go:31:13:   flow: {heap} = &{storage for func literal}:
./main_test.go:31:13:     from func literal (spill) at ./main_test.go:31:13
./main_test.go:31:13:     from .autotmp_2(func literal) (call parameter) at ./main_test.go:31:12
./main_test.go:31:9: BenchmarkBelow3direct capturing by ref: n (addr=false assign=true width=8)
./main_test.go:29:28: b does not escape
./main_test.go:9:15: f does not escape
./main_test.go:11:14: f does not escape
./main_test.go:31:9: func literal does not escape
./main_test.go:31:9: func literal does not escape
./main_test.go:31:13: func literal escapes to heap

Can anyone explain why range-over-func has two more alloc than direct-call-func?
Reply all
Reply to author
Forward
0 new messages