From 0ce8e785770928911a6288232f8aa19afdb898dc Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Thu, 16 Nov 2017 19:44:46 +0000 Subject: [PATCH] Make use of direct byte encoding / decoding With the lates github.com/abcum/cork package, it is now possible to encode and decode a cork data stream without creating a new buffer each time. Instead the github.com/abcum/bump pacakge efficiently buffers the byte slice without any unnecessary allocations. --- util/pack/pack.go | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/util/pack/pack.go b/util/pack/pack.go index 9364a52d..85f5a7ac 100644 --- a/util/pack/pack.go +++ b/util/pack/pack.go @@ -15,30 +15,35 @@ package pack import ( - "bytes" "github.com/abcum/cork" ) var opt = cork.Handle{ - Precision: false, - ArrType: make([]interface{}, 0), - MapType: make(map[string]interface{}), + SortMaps: true, + ArrType: make([]interface{}, 0), + MapType: make(map[string]interface{}), } // Encode encodes a data object into a CORK. func Encode(src interface{}) (dst []byte) { - buf := bytes.NewBuffer(nil) - enc := cork.NewEncoderFromPool(buf).Options(&opt) - enc.Encode(src) - enc.Done() - return buf.Bytes() + enc := cork.NewEncoderBytesFromPool(&dst) + enc.Options(&opt) + err := enc.Encode(src) + enc.Reset() + if err != nil { + panic(err) + } + return } // Decode decodes a CORK into a data object. func Decode(src []byte, dst interface{}) { - buf := bytes.NewReader(src) - dec := cork.NewDecoderFromPool(buf).Options(&opt) - dec.Decode(dst) - dec.Done() + dec := cork.NewDecoderBytesFromPool(src) + dec.Options(&opt) + err := dec.Decode(dst) + dec.Reset() + if err != nil { + panic(err) + } return }