Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sync/atomic: add //go:noescape to atomic functions accepting pointer #36651

Closed
valyala opened this issue Jan 20, 2020 · 1 comment
Closed

sync/atomic: add //go:noescape to atomic functions accepting pointer #36651

valyala opened this issue Jan 20, 2020 · 1 comment

Comments

@valyala
Copy link
Contributor

valyala commented Jan 20, 2020

What version of Go are you using (go version)?

$ go version
go version devel +cfe3cd903f Tue Jan 14 19:54:20 2020 +0000 linux/amd64

Does this issue reproduce with the latest release?

yes, it is reproducible in go1.13 and go tip.

What did you do?

Run the following benchmark:

func BenchmarkAtomicAddUint64(b *testing.B) {
        b.ReportAllocs()
        for i := 0; i < b.N; i++ {
                n := uint64(123)
                sink += atomic.AddUint64(&n, 1)
        }
}

var sink uint64

What did you expect to see?

Zero memory allocations in the benchmark loop.

What did you see instead?

A memory allocation. The following is the output from go tool pprof --alloc_objects:

  25919883   25919883 (flat, cum)   100% of Total
         .          .     10:)
         .          .     11:
         .          .     12:func BenchmarkAtomicAddUint64(b *testing.B) {
         .          .     13:	b.ReportAllocs()
         .          .     14:	for i := 0; i < b.N; i++ {
  25919883   25919883     15:		n := uint64(123)
         .          .     16:		sink += atomic.AddUint64(&n, 1)
         .          .     17:	}
         .          .     18:}
         .          .     19:
         .          .     20:var sink uint64

It looks like Go compiler thinks that args passed to sync/atomic functions may escape.

Possible solution is to add //go:noescape to these functions.

@randall77
Copy link
Contributor

Closing as duplicate of #16241

@golang golang locked and limited conversation to collaborators Jan 19, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants