Some confusion about escape analysis

161 views
Skip to first unread message

Lee Chou

unread,
Aug 24, 2022, 2:59:34 AM8/24/22
to golang-nuts
I was try using net.Buffers for writev. But I meet a unexptected allocs in benchmark.  

https://go.dev/play/p/SHpxSYbP1I5

when I run the benchmark:

goos: linux
goarch: amd64
pkg: writev
BenchmarkWrite_Writev/escape-4               272901          4198 ns/op          24 B/op           1 allocs/op
BenchmarkWrite_Writev/noescape-4             273751          4267 ns/op           0 B/op           0 allocs/op
PASS
ok      writev    3.410s

Is the noescape safe ? 
Is there a better way to avoid the alloc/escape ?
-
Best regards
Lee
Message has been deleted

Sean Foley

unread,
Aug 25, 2022, 2:46:11 PM8/25/22
to golang-nuts
That trick was copied from here: https://cs.opensource.google/go/go/+/refs/tags/go1.19:src/strings/builder.go;l=26

See https://github.com/golang/go/issues/23382 and https://github.com/golang/go/issues/7921

I don't know why the trick works in your case.  It appears to be safe.

Sean Foley

unread,
Aug 25, 2022, 2:49:08 PM8/25/22
to golang-nuts
Actually, looking more closely at your benchmark numbers, the trick does not work in your case.  Which makes sense to me, I don't see why it would work.

On Wednesday, August 24, 2022 at 2:59:34 AM UTC-4 mozart...@gmail.com wrote:

Lee Chou

unread,
Aug 26, 2022, 3:32:32 AM8/26/22
to golang-nuts
Thanks for taking the time for the clue. It seems that this is an open issue.

Actually, It work for this case. aovid the alloc will have more benefit for gc.

Sean Foley

unread,
Aug 26, 2022, 10:14:26 AM8/26/22
to Lee Chou, golang-nuts
Perhaps it will have a small benefit.  However, according to the benchmark, the performance is slightly better in the case where there is 1 alloc/op,
Normally people avoid the heap allocation for better performance and there is no evidence of that here.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/95daef29-ff45-4d3a-bca8-d8911b74eabbn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages