Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions internal/gen/syncset/syncset.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
type Set[T comparable] struct {
lock sync.Mutex
m sync.Map
len int64
len atomic.Int64
}

// NewSet creates a new set.
Expand All @@ -29,7 +29,7 @@ func (s *Set[T]) Add(item T) bool {
defer s.lock.Unlock()
_, loaded := s.m.LoadOrStore(item, struct{}{})
if !loaded {
atomic.AddInt64(&s.len, 1)
s.len.Add(1)
}
return !loaded
}
Expand All @@ -39,7 +39,7 @@ func (s *Set[T]) Remove(item T) {
s.lock.Lock()
defer s.lock.Unlock()
if _, loaded := s.m.LoadAndDelete(item); loaded {
atomic.AddInt64(&s.len, -1)
s.len.Add(-1)
}
}

Expand All @@ -51,7 +51,7 @@ func (s *Set[T]) Contains(item T) bool {

// Len returns the number of items in the set.
func (s *Set[T]) Len() int {
return int(atomic.LoadInt64(&s.len))
return int(s.len.Load())
}

// Items returns a slice of all items in the set.
Expand Down