mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-08 04:53:42 +00:00
sync: implement simple pooling in sync.Pool
This commit is contained in:
committed by
Ron Evans
parent
9e34ca9e5f
commit
29f8d22a2d
+9
-3
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user