sync: implement simple pooling in sync.Pool

This commit is contained in:
Anuraag Agrawal
2022-10-17 16:35:14 +09:00
committed by Ron Evans
parent 9e34ca9e5f
commit 29f8d22a2d
2 changed files with 59 additions and 3 deletions
+9 -3
View File
@@ -1,13 +1,18 @@
package sync
// Pool is a very simple implementation of sync.Pool. It does not actually
// implement a pool.
// Pool is a very simple implementation of sync.Pool.
type Pool struct {
New func() interface{}
New func() interface{}
items []interface{}
}
// Get returns the value of calling Pool.New().
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 {
return nil
}
@@ -16,4 +21,5 @@ func (p *Pool) Get() interface{} {
// Put drops the value put into the pool.
func (p *Pool) Put(x interface{}) {
p.items = append(p.items, x)
}