Skip to content

Commit 442f9dc

Browse files
init sync pool when create
1 parent 04a077c commit 442f9dc

1 file changed

Lines changed: 3 additions & 15 deletions

File tree

pkg/xsync/sync_pool.go

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,25 @@ package xsync
33
import "sync"
44

55
type SyncPool[T any] struct {
6-
New func() T
76
pool sync.Pool
8-
once sync.Once
97
}
108

119
// NewSyncPool creates a new SyncPool with specified init function
1210
func NewSyncPool[T any](new func() T) *SyncPool[T] {
1311
sp := &SyncPool[T]{
14-
New: new,
12+
pool: sync.Pool{New: func() interface{} {
13+
return new()
14+
}},
1515
}
16-
sp.init()
1716
return sp
1817
}
1918

20-
// init initializes the SyncPool
21-
func (s *SyncPool[T]) init() {
22-
s.once.Do(func() {
23-
s.pool.New = func() interface{} {
24-
return s.New()
25-
}
26-
})
27-
}
28-
2919
// Get wraps sync.Pool.Get.
3020
func (s *SyncPool[T]) Get() T {
31-
s.init()
3221
return s.pool.Get().(T)
3322
}
3423

3524
// Put wraps sync.Pool.Put.
3625
func (s *SyncPool[T]) Put(x T) {
37-
s.init()
3826
s.pool.Put(x)
3927
}

0 commit comments

Comments
 (0)