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

Undocumented behaviour by map.LoadOrStore #27114

Closed
kaypour opened this issue Aug 21, 2018 · 1 comment
Closed

Undocumented behaviour by map.LoadOrStore #27114

kaypour opened this issue Aug 21, 2018 · 1 comment
Labels
Documentation Issues describing a change to documentation. FrozenDueToAge

Comments

@kaypour
Copy link

kaypour commented Aug 21, 2018

I am not sure if this behaviour is expected, but I could not find any documentation regarding it.

According to the documentation:

LoadOrStore returns the existing value for the key if present.
Otherwise, it stores and returns the given value.
The loaded result is true if the value was loaded, false if stored.

I naturally expect that if the value is returned by a function, that function is not invoked if the key already exists.

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

go1.10.3

Does this issue reproduce with the latest release?

N/A

What operating system and processor architecture are you using (go env)?

Darwin / amd64

What did you do?

I tried to store a value into the sync.Map concurrently, by using LoadOrStore method, with the same key.

https://play.golang.org/p/PQ3pye-SZGN

What did you expect to see?

I expected to see that the function Foo was only invoked once by the sMap.LoadOrStore method.

What did you see instead?

The sMap.LoadOrStore method did only store once, but it invoked the Foo function 10 times.

@gopherbot gopherbot added the Documentation Issues describing a change to documentation. label Aug 21, 2018
@cespare
Copy link
Contributor

cespare commented Aug 21, 2018

This doesn't have anything to do with sync.Map; you're incrementing functionCallCounter before LoadOrStore is even called. This version has the same semantics; maybe that makes it clearer what's happening: https://play.golang.org/p/x31ExkgLzIO

@cespare cespare closed this as completed Aug 21, 2018
@golang golang locked and limited conversation to collaborators Aug 21, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Documentation Issues describing a change to documentation. FrozenDueToAge
Projects
None yet
Development

No branches or pull requests

3 participants