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

4

6

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.

Balbinder answered 16/8, 2018 at 20:46 Comment(1)
Possible duplicate of #51104121Loafer
A
-1

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

            }
        }
    }
}
Advantageous answered 16/8, 2018 at 22:47 Comment(2)
This does not answer the question, however. How do you do computeIfAbsent in Go - specifically, only create a new value if it's missing in the map?Trainee
This example defeats the point of using sync.Map in the first placeToupee
B
0

you can try:

var m sync.Map
s, ok := m.Load("key")
if !ok {
    s, _ = m.LoadOrStore("key", "value")
}

fmt.Println(s)

play demo

Befitting answered 12/9, 2019 at 8:34 Comment(1)
Just fixed my code with the above solution, and reduced used RAM 2x, also cpu was reducedMalcom
Q
0

This is my solution: use sync.Map and sync.One

type syncData struct {
    data interface{}
    once *sync.Once
}

func LoadOrStore(m *sync.Map, key string, f func() (interface{}, error)) (interface{}, error) {
    temp, _ := m.LoadOrStore(key, &syncData{
        data: nil,
        once: &sync.Once{},
    })
    d := temp.(*syncData)
    var err error
    if d.data == nil {
        d.once.Do(func() {
            d.data, err = f()
            if err != nil {
                //if failed, will try again by new sync.Once
                d.once = &sync.Once{}
            }
        })
    }
    return d.data, err
}
Quartering answered 27/8, 2021 at 13:37 Comment(0)
V
0

It can be implemented using sync.OnceValue. However, you need to be cautious about the type of values though.

func computeIfAbsent[E any](m *sync.Map, key any, f func() E) E {
    v, ok := m.LoadOrStore(key, sync.OnceValue(f))
    if !ok {
        v := v.(func() E)()
        m.Store(key, v)
        return v
    } else if f, ok := v.(func() E); ok {
        return f()
    }
    return v.(E)
}
Viewfinder answered 15/7 at 14:15 Comment(0)
A
-1

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

            }
        }
    }
}
Advantageous answered 16/8, 2018 at 22:47 Comment(2)
This does not answer the question, however. How do you do computeIfAbsent in Go - specifically, only create a new value if it's missing in the map?Trainee
This example defeats the point of using sync.Map in the first placeToupee

© 2022 - 2024 — McMap. All rights reserved.