-1

I was exploring the possibility of concurrently accessing a map with fixed keys without a lock for performance improvement. I've explored the similar with slice before and seems it works:

func TestConcurrentSlice(t *testing.T) {
    fixed := []int{1, 2, 3}
    wg := &sync.WaitGroup{}
    for i := 0; i < len(fixed); i++ {
        idx := i
        wg.Add(1)
        go func() {
            defer wg.Done()
            fixed[idx]++
        }()
    }
    wg.Wait()
    fmt.Printf("%v\n", fixed)
}

The above code will pass the -race test.

That gave me the confidence of achieving the same thing with map with fixed size (fixed number of keys) because I assume if the number of keys doesn't change, so the underline array (in map) does not need to expand, so it will be safe for us to access different key (different memory location) in different go-routine. So I wrote this test:

type simpleStruct struct {
    val int
}

func TestConcurrentAccessMap(t *testing.T) {
    fixed := map[string]*simpleStruct{
        "a": {0},
        "b": {0},
    }
    wg := &sync.WaitGroup{}
    // here I use array instead of iterating the map to avoid read access
    keys := []string{"a", "b"}
    for _, k := range keys {
        kcopy := k
        wg.Add(1)
        go func() {
            defer wg.Done()
            // this failed the race test
            fixed[kcopy] = &simpleStruct{}

            // this actually can pass the race test!
            //fixed[kcopy].val++
        }()
    }
    wg.Wait()
}

however, the test failed the race test with error message concurrent write by runtime.mapassign_faststr() function.

And one more interesting I found is the code I've commented out "fixed[kcopy].val++" actually passed the race test (I assume it's because of the writings are at different memory location). But I'm wondering since the go-routines are accessing different keys of the map, why it will fail the race test?

icza
  • 389,944
  • 63
  • 907
  • 827
Yuguang
  • 125
  • 5

1 Answers1

5

Accessing different slice elements without synchronization from multiple goroutines is OK, because each slice element acts as an individual variable. For details, see Can I concurrently write different slice elements.

However, this is not the case with maps. A value for a specific key does not act as a variable, and it is not addressable (because the actual memory space the value is stored at may be internally changed–at the sole discretion of the implementation).

So with maps, the general rule applies: if the map is accessed from multiple goroutines where at least one of them is a write (assign a value to a key), explicit synchronization is needed.

icza
  • 389,944
  • 63
  • 907
  • 827
  • Thanks for the explanation. If fixed[kcopy] does not guarantee a fixed memory space, how is `fixed[kcopy].val++` different in this case? – Yuguang Aug 30 '18 at 10:03
  • @Yuguang `fixed[kcopy].val++` is a shorthand for `fixed[kcopy] = fixed[kcopy] + 1`. In this form you can see that a re-assignment happens. – icza Aug 30 '18 at 10:05
  • Sorry for the confusion here, the map I used for test is string-> *simpleStruct, so `fixed[kcopy].val++` is short for `fixed[kcopy].val = fixed[kcopy].val + 1`. However it actually can pass the test if we change `fixed[kcopy] = &simpleStruct{}` to `fixed[kcopy].val++`. Do you see it as a false positive of the race testing? – Yuguang Aug 30 '18 at 10:12
  • 1
    @Yuguang The race detector does not detect all races, only those that do happen during the run-time of your app. Seeing no races does not guarantee there could be no races. – icza Aug 30 '18 at 10:14