From 442f9dcc5cdb2ccc976a988ea3bf76dbcd52dedd Mon Sep 17 00:00:00 2001 From: "kevin.zhao" Date: Fri, 10 Oct 2025 14:33:33 +0800 Subject: [PATCH 1/2] init sync pool when create --- pkg/xsync/sync_pool.go | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) 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) } From 50ad8864952eeb0f615ef4eb9d5dd6bfd07bcf8d Mon Sep 17 00:00:00 2001 From: "kevin.zhao" Date: Fri, 10 Oct 2025 14:35:18 +0800 Subject: [PATCH 2/2] fix --- pkg/xsync/README.md | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) 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)