|
44 | 44 |
|
45 | 45 | // cache converted values. use weak pointers to avoid holding on to values in the cache
|
46 | 46 | // that are no longer in use.
|
47 |
| - convertCacheLock sync.Mutex |
48 |
| - convertCache map[weak.Pointer[ConstrainedValue]]any |
| 47 | + convertCache sync.Map // map[weak.Pointer[ConstrainedValue]]any |
49 | 48 | }
|
50 | 49 |
|
51 | 50 | subscription[T any] struct {
|
@@ -110,7 +109,6 @@ func NewCollection(client Client, logger log.Logger) *Collection {
|
110 | 109 | logger: logger,
|
111 | 110 | errCount: -1,
|
112 | 111 | subscriptions: make(map[Key]map[int]any),
|
113 |
| - convertCache: make(map[weak.Pointer[ConstrainedValue]]any), |
114 | 112 | }
|
115 | 113 | }
|
116 | 114 |
|
@@ -522,33 +520,25 @@ func dispatchUpdateWithConstrainedDefault[T any](
|
522 | 520 | func convertWithCache[T any](c *Collection, key Key, convert func(any) (T, error), cvp *ConstrainedValue) (T, error) {
|
523 | 521 | weakcvp := weak.Make(cvp)
|
524 | 522 |
|
525 |
| - c.convertCacheLock.Lock() |
526 |
| - if converted, ok := c.convertCache[weakcvp]; ok { |
| 523 | + if converted, ok := c.convertCache.Load(weakcvp); ok { |
527 | 524 | if t, ok := converted.(T); ok {
|
528 |
| - c.convertCacheLock.Unlock() |
529 | 525 | return t, nil
|
530 | 526 | }
|
531 | 527 | // Each key can only be used with a single type, so this shouldn't happen
|
532 | 528 | c.logger.Warn("Cached converted value has wrong type", tag.Key(key.String()))
|
533 | 529 | // Fall through to regular conversion
|
534 | 530 | }
|
535 |
| - c.convertCacheLock.Unlock() |
536 | 531 |
|
537 |
| - // unlock around convert |
538 | 532 | t, err := convert(cvp.Value)
|
539 | 533 | if err != nil {
|
540 | 534 | var zero T
|
541 | 535 | return zero, err
|
542 | 536 | }
|
543 | 537 |
|
544 |
| - c.convertCacheLock.Lock() |
545 |
| - c.convertCache[weakcvp] = t |
546 |
| - c.convertCacheLock.Unlock() |
| 538 | + c.convertCache.Store(weakcvp, t) |
547 | 539 |
|
548 | 540 | runtime.AddCleanup(cvp, func(w weak.Pointer[ConstrainedValue]) {
|
549 |
| - c.convertCacheLock.Lock() |
550 |
| - delete(c.convertCache, w) |
551 |
| - c.convertCacheLock.Unlock() |
| 541 | + c.convertCache.Delete(w) |
552 | 542 | }, weakcvp)
|
553 | 543 |
|
554 | 544 | return t, nil
|
|
0 commit comments