goconcurrency

LoadOrStore in a sync.Map without creating a new structure each time


Is it possible to LoadOrStore into a Go sync.Map without creating a new structure every time? If not, what alternatives are available?

The use case here is if I'm using the sync.Map as a cache where cache misses are rare (but possible) and on a cache miss I want to add to the map, I need to initialize a structure every single time LoadOrStore is called rather than just creating the struct when needed. I'm worried this will hurt the GC, initializing hundreds of thousands of structures that will not be needed.

In Java this can be done using computeIfAbsent.


Solution

  • Package sync

    import "sync"
    

    type Map

    Map is like a Go map[interface{}]interface{} but is safe for concurrent use by multiple goroutines without additional locking or coordination. Loads, stores, and deletes run in amortized constant time.

    The Map type is specialized. Most code should use a plain Go map instead, with separate locking or coordination, for better type safety and to make it easier to maintain other invariants along with the map content.

    The Map type is optimized for two common use cases: (1) when the entry for a given key is only ever written once but read many times, as in caches that only grow, or (2) when multiple goroutines read, write, and overwrite entries for disjoint sets of keys. In these two cases, use of a Map may significantly reduce lock contention compared to a Go map paired with a separate Mutex or RWMutex.


    The usual way to solve these problems is to construct a usage model and then benchmark it.

    For example, since "cache misses are rare", assume that Load wiil work most of the time and only LoadOrStore (with value allocation and initialization) when necessary.

    $ go test map_test.go -bench=. -benchmem
    BenchmarkHit-4     2     898810447 ns/op        44536 B/op        1198 allocs/op
    BenchmarkMiss-4    1    2958103053 ns/op    483957168 B/op    43713042 allocs/op
    $
    

    map_test.go:

    package main
    
    import (
        "strconv"
        "sync"
        "testing"
    )
    
    func BenchmarkHit(b *testing.B) {
        for N := 0; N < b.N; N++ {
            var m sync.Map
            for i := 0; i < 64*1024; i++ {
                for k := 0; k < 256; k++ {
    
                    // Assume cache hit
                    v, ok := m.Load(k)
                    if !ok {
                        // allocate and initialize value
                        v = strconv.Itoa(k)
                        a, loaded := m.LoadOrStore(k, v)
                        if loaded {
                            v = a
                        }
                    }
                    _ = v
    
                }
            }
        }
    }
    
    func BenchmarkMiss(b *testing.B) {
        for N := 0; N < b.N; N++ {
            var m sync.Map
            for i := 0; i < 64*1024; i++ {
                for k := 0; k < 256; k++ {
    
                    // Assume cache miss
                    // allocate and initialize value
                    var v interface{} = strconv.Itoa(k)
                    a, loaded := m.LoadOrStore(k, v)
                    if loaded {
                        v = a
                    }
                    _ = v
    
                }
            }
        }
    }