Revert "Optimization for String.Store("")" (#32)
This optimization causes data races since we're changing the value field
without using atomics. E.g., a caller who has multiple goroutines
calling `Set("1")` and `Set("")` will race on the access to `s.v` since
one goroutine is trying to read it while the other sets it, neither
using atomic operations.
This reverts commit 16b44f14f0.
This commit is contained in:
committed by
GitHub
parent
54f72d3243
commit
e81582a97d
@@ -45,9 +45,5 @@ func (s *String) Load() string {
|
|||||||
// Note: Converting the string to an interface{} to store in the Value
|
// Note: Converting the string to an interface{} to store in the Value
|
||||||
// requires an allocation.
|
// requires an allocation.
|
||||||
func (s *String) Store(str string) {
|
func (s *String) Store(str string) {
|
||||||
if str == "" {
|
|
||||||
s.v = Value{}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
s.v.Store(str)
|
s.v.Store(str)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,7 +40,4 @@ func TestString(t *testing.T) {
|
|||||||
|
|
||||||
atom = NewString("bcd")
|
atom = NewString("bcd")
|
||||||
require.Equal(t, "bcd", atom.Load(), "Expected Load to return initialized value")
|
require.Equal(t, "bcd", atom.Load(), "Expected Load to return initialized value")
|
||||||
|
|
||||||
atom.Store("")
|
|
||||||
require.Equal(t, "", atom.Load(), "Expected Load to return empty value")
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user