-
Notifications
You must be signed in to change notification settings - Fork 9
/
pool.go
53 lines (46 loc) · 1.45 KB
/
pool.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package stow
import (
"io"
"sync"
)
type pooledCodec struct {
encoderPool sync.Pool
decoderPool sync.Pool
}
// NewPooledCodec creates a new Codec which re-uses Encoder/Decoders created by the codec.
// Warning, this is only useful when creating Encoders/Decoders is 'expensive' and when
// they support being re-used. In order to support re-use encoders/decoders must be
// equivalent. For example, a new Gob Encoder is not equivalent to a Gob Encoder
// which has encoded a non-primitive type [it has cached type info]. On the other hand,
// a json Encoder is safe for re-use and so is a Primed Gob Encoder which has only encoded primed types
// since all primed types are cached for all encoders/decoders.
func NewPooledCodec(codec Codec) Codec {
return &pooledCodec{
encoderPool: sync.Pool{New: func() interface{} {
var enc delegateEncoder
enc.Encoder = codec.NewEncoder(&enc)
return &enc
}},
decoderPool: sync.Pool{New: func() interface{} {
var dec delegateDecoder
dec.Decoder = codec.NewDecoder(&dec)
return &dec
}},
}
}
func (p *pooledCodec) NewEncoder(w io.Writer) Encoder {
enc := p.encoderPool.Get().(*delegateEncoder)
enc.Writer = w
return enc
}
func (p *pooledCodec) NewDecoder(r io.Reader) Decoder {
dec := p.decoderPool.Get().(*delegateDecoder)
dec.Reader = r
return dec
}
func (p *pooledCodec) PutEncoder(enc Encoder) {
p.encoderPool.Put(enc)
}
func (p *pooledCodec) PutDecoder(dec Decoder) {
p.decoderPool.Put(dec)
}