[#443] metabase: speedup encodelist

GOB appears to be almost twice as slow as this implementation.

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2021-03-22 11:10:01 +03:00 committed by Alex Vanin
parent 62b8958177
commit 4a1ca4ecc1

View file

@ -1,11 +1,10 @@
package meta package meta
import ( import (
"bytes"
"encoding/gob"
"errors" "errors"
"fmt" "fmt"
"github.com/nspcc-dev/neo-go/pkg/io"
objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object" 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/core/object"
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobovnicza" "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. // encodeList decodes list of bytes into a single blog for list bucket indexes.
func encodeList(lst [][]byte) ([]byte, error) { func encodeList(lst [][]byte) ([]byte, error) {
buf := bytes.NewBuffer(nil) w := io.NewBufBinWriter()
encoder := gob.NewEncoder(buf) w.WriteVarUint(uint64(len(lst)))
for i := range lst {
// consider using protobuf encoding instead of glob w.WriteVarBytes(lst[i])
if err := encoder.Encode(lst); err != nil {
return nil, err
} }
if w.Err != nil {
return buf.Bytes(), nil return nil, w.Err
}
return w.Bytes(), nil
} }
// decodeList decodes blob into the list of bytes from list bucket index. // 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 { if len(data) == 0 {
return nil, nil return nil, nil
} }
r := io.NewBinReaderFromBuf(data)
decoder := gob.NewDecoder(bytes.NewReader(data)) l := r.ReadVarUint()
if err := decoder.Decode(&lst); err != nil { lst = make([][]byte, l)
return nil, err for i := range lst {
lst[i] = r.ReadVarBytes()
}
if r.Err != nil {
return nil, r.Err
} }
return lst, nil return lst, nil
} }