lib/encoding/zstd: switch back from atomic.Pointer to atomic.Value for map[...]...

The map[...]... is already a pointer type, so atomic.Pointer[map[...]...] results in double pointer.

This is a follow-up for 140e7b6b74
This commit is contained in:
Aliaksandr Valialkin 2023-07-20 20:55:27 -07:00
parent c94a5d95a2
commit dcf5b42670
No known key found for this signature in database
GPG key ID: A72BEC6CD3D0DED1
2 changed files with 10 additions and 8 deletions

View file

@ -15,13 +15,13 @@ var (
decoder *zstd.Decoder
mu sync.Mutex
av atomic.Pointer[registry]
// do not use atomic.Pointer, since the stored map there is already a pointer type.
av atomic.Value
)
type registry map[int]*zstd.Encoder
func init() {
r := make(registry)
r := make(map[int]*zstd.Encoder)
av.Store(r)
var err error
@ -45,7 +45,7 @@ func CompressLevel(dst, src []byte, compressionLevel int) []byte {
}
func getEncoder(compressionLevel int) *zstd.Encoder {
r := av.Load()
r := av.Load().(map[int]*zstd.Encoder)
e := r[compressionLevel]
if e != nil {
return e
@ -54,10 +54,10 @@ func getEncoder(compressionLevel int) *zstd.Encoder {
mu.Lock()
// Create the encoder under lock in order to prevent from wasted work
// when concurrent goroutines create encoder for the same compressionLevel.
r1 := av.Load()
r1 := av.Load().(map[int]*zstd.Encoder)
if e = r1[compressionLevel]; e == nil {
e = newEncoder(compressionLevel)
r2 := make(registry)
r2 := make(map[int]*zstd.Encoder)
for k, v := range r1 {
r2[k] = v
}

View file

@ -18,7 +18,9 @@ type TenantID struct {
// CounterMap is a map of counters keyed by tenant.
type CounterMap struct {
metric string
m atomic.Value
// do not use atomic.Pointer, since the stored map there is already a pointer type.
m atomic.Value
}
// NewCounterMap creates new CounterMap for the given metric.