Use cork encoder/decoder pools

This commit is contained in:
Tobie Morgan Hitchcock 2017-05-05 16:22:11 +01:00
parent 4c16dafcf3
commit 54dd56cbd5

View file

@ -19,24 +19,26 @@ import (
"github.com/abcum/cork" "github.com/abcum/cork"
) )
var opt cork.Handle var opt = cork.Handle{
Precision: false,
func init() { ArrType: make([]interface{}, 0),
opt.Precision = false MapType: make(map[string]interface{}),
opt.ArrType = []interface{}{}
opt.MapType = map[string]interface{}{}
} }
// Encode encodes a data object into a CORK. // Encode encodes a data object into a CORK.
func Encode(src interface{}) (dst []byte) { func Encode(src interface{}) (dst []byte) {
buf := bytes.NewBuffer(nil) buf := bytes.NewBuffer(nil)
cork.NewEncoder(buf).Options(&opt).Encode(src) enc := cork.NewEncoderFromPool(buf).Options(&opt)
enc.Encode(src)
enc.Done()
return buf.Bytes() return buf.Bytes()
} }
// Decode decodes a CORK into a data object. // Decode decodes a CORK into a data object.
func Decode(src []byte, dst interface{}) { func Decode(src []byte, dst interface{}) {
buf := bytes.NewReader(src) buf := bytes.NewReader(src)
cork.NewDecoder(buf).Options(&opt).Decode(dst) dec := cork.NewDecoderFromPool(buf).Options(&opt)
dec.Decode(dst)
dec.Done()
return return
} }