diff --git a/pkg/xsync/README.md b/pkg/xsync/README.md index 79be085..3a25552 100644 --- a/pkg/xsync/README.md +++ b/pkg/xsync/README.md @@ -343,19 +343,18 @@ func (s *SyncMap[K, V]) ToMap() map[K]V ToMap returns a copy of the map as a regular map. -## type [SyncPool]() +## type [SyncPool]() ```go type SyncPool[T any] struct { - New func() T // contains filtered or unexported fields } ``` -### func [NewSyncPool]() +### func [NewSyncPool]() ```go func NewSyncPool[T any](new func() T) *SyncPool[T] @@ -364,7 +363,7 @@ func NewSyncPool[T any](new func() T) *SyncPool[T] NewSyncPool creates a new SyncPool with specified init function -### func \(\*SyncPool\[T\]\) [Get]() +### func \(\*SyncPool\[T\]\) [Get]() ```go func (s *SyncPool[T]) Get() T @@ -373,7 +372,7 @@ func (s *SyncPool[T]) Get() T Get wraps sync.Pool.Get. -### func \(\*SyncPool\[T\]\) [Put]() +### func \(\*SyncPool\[T\]\) [Put]() ```go func (s *SyncPool[T]) Put(x T) diff --git a/pkg/xsync/sync_pool.go b/pkg/xsync/sync_pool.go index 571c6de..fb156db 100644 --- a/pkg/xsync/sync_pool.go +++ b/pkg/xsync/sync_pool.go @@ -3,37 +3,25 @@ package xsync import "sync" type SyncPool[T any] struct { - New func() T pool sync.Pool - once sync.Once } // NewSyncPool creates a new SyncPool with specified init function func NewSyncPool[T any](new func() T) *SyncPool[T] { sp := &SyncPool[T]{ - New: new, + pool: sync.Pool{New: func() interface{} { + return new() + }}, } - sp.init() return sp } -// init initializes the SyncPool -func (s *SyncPool[T]) init() { - s.once.Do(func() { - s.pool.New = func() interface{} { - return s.New() - } - }) -} - // Get wraps sync.Pool.Get. func (s *SyncPool[T]) Get() T { - s.init() return s.pool.Get().(T) } // Put wraps sync.Pool.Put. func (s *SyncPool[T]) Put(x T) { - s.init() s.pool.Put(x) }