我正在探索使用固定键并发访问地图而无需锁定的可能性,以提高性能。
我之前已经探索过与 slice 类似的功能,并且似乎它有效:
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)
}
上面的代码将通过-race测试。
这让我有信心用固定大小(固定数量的键)的地图实现同样的事情,因为我假设如果键的数量不改变,那么下划线数组(在地图中)不需要扩展,所以它我们在不同的 go 例程中访问不同的密钥(不同的内存位置)将是安全的。所以我写了这个测试:
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()
}
但是,该测试未能通过运行时测试,并出现错误消息并发写入。
我发现的一个更有趣的是我注释掉的代码“fixed[kcopy].val++”实际上通过了竞赛测试(我认为这是因为写入的内容位于不同的内存位置)。但我想知道既然 go 例程正在访问地图的不同键,为什么它会在比赛测试中失败?