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 decoder *zstd.Decoder
mu sync.Mutex 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() { func init() {
r := make(registry) r := make(map[int]*zstd.Encoder)
av.Store(r) av.Store(r)
var err error var err error
@ -45,7 +45,7 @@ func CompressLevel(dst, src []byte, compressionLevel int) []byte {
} }
func getEncoder(compressionLevel int) *zstd.Encoder { func getEncoder(compressionLevel int) *zstd.Encoder {
r := av.Load() r := av.Load().(map[int]*zstd.Encoder)
e := r[compressionLevel] e := r[compressionLevel]
if e != nil { if e != nil {
return e return e
@ -54,10 +54,10 @@ func getEncoder(compressionLevel int) *zstd.Encoder {
mu.Lock() mu.Lock()
// Create the encoder under lock in order to prevent from wasted work // Create the encoder under lock in order to prevent from wasted work
// when concurrent goroutines create encoder for the same compressionLevel. // 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 { if e = r1[compressionLevel]; e == nil {
e = newEncoder(compressionLevel) e = newEncoder(compressionLevel)
r2 := make(registry) r2 := make(map[int]*zstd.Encoder)
for k, v := range r1 { for k, v := range r1 {
r2[k] = v r2[k] = v
} }

View File

@ -18,7 +18,9 @@ type TenantID struct {
// CounterMap is a map of counters keyed by tenant. // CounterMap is a map of counters keyed by tenant.
type CounterMap struct { type CounterMap struct {
metric string 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. // NewCounterMap creates new CounterMap for the given metric.