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.
This commit is contained in:
parent
44591abfe5
commit
0ce8e78577
1 changed files with 18 additions and 13 deletions
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue