sync: implement simple pooling in sync.Pool

Этот коммит содержится в:
Anuraag Agrawal 2022-10-17 16:35:14 +09:00 коммит произвёл Ron Evans
родитель 9e34ca9e5f
коммит 29f8d22a2d
2 изменённых файлов: 59 добавлений и 3 удалений

Просмотреть файл

@ -1,13 +1,18 @@
package sync package sync
// Pool is a very simple implementation of sync.Pool. It does not actually // Pool is a very simple implementation of sync.Pool.
// implement a pool.
type Pool struct { type Pool struct {
New func() interface{} New func() interface{}
items []interface{}
} }
// Get returns the value of calling Pool.New(). // Get returns the value of calling Pool.New().
func (p *Pool) Get() interface{} { func (p *Pool) Get() interface{} {
if len(p.items) > 0 {
x := p.items[len(p.items)-1]
p.items = p.items[:len(p.items)-1]
return x
}
if p.New == nil { if p.New == nil {
return nil return nil
} }
@ -16,4 +21,5 @@ func (p *Pool) Get() interface{} {
// Put drops the value put into the pool. // Put drops the value put into the pool.
func (p *Pool) Put(x interface{}) { func (p *Pool) Put(x interface{}) {
p.items = append(p.items, x)
} }

50
src/sync/pool_test.go Обычный файл
Просмотреть файл

@ -0,0 +1,50 @@
package sync_test
import (
"sync"
"testing"
)
type testItem struct {
val int
}
func TestPool(t *testing.T) {
p := sync.Pool{
New: func() interface{} {
return &testItem{}
},
}
i1P := p.Get()
if i1P == nil {
t.Error("pool with New returned nil")
}
i1 := i1P.(*testItem)
if got, want := i1.val, 0; got != want {
t.Errorf("empty pool item value: got %v, want %v", got, want)
}
i1.val = 1
i2 := p.Get().(*testItem)
if got, want := i2.val, 0; got != want {
t.Errorf("empty pool item value: got %v, want %v", got, want)
}
i2.val = 2
p.Put(i1)
i3 := p.Get().(*testItem)
if got, want := i3.val, 1; got != want {
t.Errorf("pool with item value: got %v, want %v", got, want)
}
}
func TestPool_noNew(t *testing.T) {
p := sync.Pool{}
i1 := p.Get()
if i1 != nil {
t.Errorf("pool without New returned %v, want nil", i1)
}
}