From 4a1ca4ecc16c40cb5cef0fce712ccd6d3c2fa1a9 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Mon, 22 Mar 2021 11:10:01 +0300 Subject: [PATCH] [#443] metabase: speedup encodelist GOB appears to be almost twice as slow as this implementation. Signed-off-by: Evgenii Stratonikov --- pkg/local_object_storage/metabase/put.go | 32 +++++++++++++----------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/pkg/local_object_storage/metabase/put.go b/pkg/local_object_storage/metabase/put.go index 1ed976173..17484f37f 100644 --- a/pkg/local_object_storage/metabase/put.go +++ b/pkg/local_object_storage/metabase/put.go @@ -1,11 +1,10 @@ package meta import ( - "bytes" - "encoding/gob" "errors" "fmt" + "github.com/nspcc-dev/neo-go/pkg/io" objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object" "github.com/nspcc-dev/neofs-node/pkg/core/object" "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobovnicza" @@ -349,15 +348,15 @@ func putListIndexItem(tx *bbolt.Tx, item namedBucketItem) error { // encodeList decodes list of bytes into a single blog for list bucket indexes. func encodeList(lst [][]byte) ([]byte, error) { - buf := bytes.NewBuffer(nil) - encoder := gob.NewEncoder(buf) - - // consider using protobuf encoding instead of glob - if err := encoder.Encode(lst); err != nil { - return nil, err + w := io.NewBufBinWriter() + w.WriteVarUint(uint64(len(lst))) + for i := range lst { + w.WriteVarBytes(lst[i]) } - - return buf.Bytes(), nil + if w.Err != nil { + return nil, w.Err + } + return w.Bytes(), nil } // decodeList decodes blob into the list of bytes from list bucket index. @@ -365,12 +364,15 @@ func decodeList(data []byte) (lst [][]byte, err error) { if len(data) == 0 { return nil, nil } - - decoder := gob.NewDecoder(bytes.NewReader(data)) - if err := decoder.Decode(&lst); err != nil { - return nil, err + r := io.NewBinReaderFromBuf(data) + l := r.ReadVarUint() + lst = make([][]byte, l) + for i := range lst { + lst[i] = r.ReadVarBytes() + } + if r.Err != nil { + return nil, r.Err } - return lst, nil }