From 1c6315140a3e08c123c64041f637e42c262f90aa Mon Sep 17 00:00:00 2001 From: goroutine Date: Thu, 18 Apr 2019 17:01:15 +0800 Subject: [PATCH] Speed up decoding column id (#10188) * codec: speed up decoding column id by removing a redundant decoding of varint * add a comment for skipped flag --- tablecodec/tablecodec.go | 12 +++++------- util/codec/codec.go | 11 +++++++++++ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/tablecodec/tablecodec.go b/tablecodec/tablecodec.go index 7eeea8e57cb59..a4ae973da727a 100644 --- a/tablecodec/tablecodec.go +++ b/tablecodec/tablecodec.go @@ -372,25 +372,23 @@ func CutRowNew(data []byte, colIDs map[int64]int) ([][]byte, error) { cnt int b []byte err error + cid int64 ) row := make([][]byte, len(colIDs)) for len(data) > 0 && cnt < len(colIDs) { // Get col id. - b, data, err = codec.CutOne(data) - if err != nil { - return nil, errors.Trace(err) - } - _, cid, err := codec.DecodeOne(b) + data, cid, err = codec.CutColumnID(data) if err != nil { return nil, errors.Trace(err) } + // Get col value. b, data, err = codec.CutOne(data) if err != nil { return nil, errors.Trace(err) } - id := cid.GetInt64() - offset, ok := colIDs[id] + + offset, ok := colIDs[cid] if ok { row[offset] = b cnt++ diff --git a/util/codec/codec.go b/util/codec/codec.go index 0af81e36ae5f7..fde8afe1b7bd3 100644 --- a/util/codec/codec.go +++ b/util/codec/codec.go @@ -429,6 +429,17 @@ func CutOne(b []byte) (data []byte, remain []byte, err error) { return b[:l], b[l:], nil } +// CutColumnID cuts the column ID from b. +// It will return the remains as byte slice and column ID +func CutColumnID(b []byte) (remain []byte, n int64, err error) { + if len(b) < 1 { + return nil, 0, errors.New("invalid encoded key") + } + // skip the flag + b = b[1:] + return DecodeVarint(b) +} + // SetRawValues set raw datum values from a row data. func SetRawValues(data []byte, values []types.Datum) error { for i := 0; i < len(values); i++ {