Skip to content

sync/atomic: operations cause unnecessary escapes #16241

@aclements

Description

@aclements

The atomic Load* and Store* operations cause their argument to unnecessarily escape. For example, in the following program, x is heap allocated:

package main

import "sync/atomic"

func main() {
    var x int32
    atomic.StoreInt32(&x, 42)
}

This is fairly low priority, since it's silly to use atomics on variables unless they're shared, and if the variable is shared it has to be in the heap anyway, but we should fix it since the same code path may be used for both shared and unshared data (there will obviously be unnecessary overhead from the atomics, but we can address the additional heap allocation overhead). This has been right for the runtime-internal atomics for some time, and we should simply copy what they do. In particular, we need to be careful with the *Pointer functions, since their values potentially do escape.

Pointed out by OneOfOne/lfchan#3 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsFixThe path to resolution is known, but the work has not been done.Performance

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions