Skip to content

Commit

Permalink
tablecodec: make decode much more faster (pingcap#7071)
Browse files Browse the repository at this point in the history
  • Loading branch information
ngaut authored and zz-jason committed Jul 17, 2018
1 parent 329c12c commit 533c777
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
29 changes: 21 additions & 8 deletions tablecodec/tablecodec.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ var (
)

const (
idLen = 8
prefixLen = 1 + idLen /*tableID*/ + 2
recordRowKeyLen = prefixLen + idLen /*handle*/
idLen = 8
prefixLen = 1 + idLen /*tableID*/ + 2
recordRowKeyLen = prefixLen + idLen /*handle*/
tablePrefixLength = 1
recordPrefixSepLength = 2
)

// TableSplitKeyLen is the length of key 't{table_id}' which is used for table split.
Expand Down Expand Up @@ -84,25 +86,36 @@ func EncodeRecordKey(recordPrefix kv.Key, h int64) kv.Key {
return buf
}

func hasTablePrefix(key kv.Key) bool {
return key[0] == tablePrefix[0]
}

func hasRecordPrefixSep(key kv.Key) bool {
return key[0] == recordPrefixSep[0] && key[1] == recordPrefixSep[1]
}

// DecodeRecordKey decodes the key and gets the tableID, handle.
func DecodeRecordKey(key kv.Key) (tableID int64, handle int64, err error) {
if len(key) <= prefixLen {
return 0, 0, errInvalidRecordKey.Gen("invalid record key - %q", key)
}

k := key
if !key.HasPrefix(tablePrefix) {
if !hasTablePrefix(key) {
return 0, 0, errInvalidRecordKey.Gen("invalid record key - %q", k)
}

key = key[len(tablePrefix):]
key = key[tablePrefixLength:]
key, tableID, err = codec.DecodeInt(key)
if err != nil {
return 0, 0, errors.Trace(err)
}

if !key.HasPrefix(recordPrefixSep) {
if !hasRecordPrefixSep(key) {
return 0, 0, errInvalidRecordKey.Gen("invalid record key - %q", k)
}

key = key[len(recordPrefixSep):]

key = key[recordPrefixSepLength:]
key, handle, err = codec.DecodeInt(key)
if err != nil {
return 0, 0, errors.Trace(err)
Expand Down
15 changes: 15 additions & 0 deletions tablecodec/tablecodec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"time"

. "github.com/pingcap/check"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/sessionctx/stmtctx"
"github.com/pingcap/tidb/types"
Expand Down Expand Up @@ -359,3 +360,17 @@ func (s *testTableCodecSuite) TestDecodeIndexKey(c *C) {
c.Assert(decodeIndexID, Equals, indexID)
c.Assert(decodeValues, DeepEquals, valueStrs)
}

func BenchmarkHasTablePrefix(b *testing.B) {
k := kv.Key("foobar")
for i := 0; i < b.N; i++ {
hasTablePrefix(k)
}
}

func BenchmarkHasTablePrefixBuiltin(b *testing.B) {
k := kv.Key("foobar")
for i := 0; i < b.N; i++ {
k.HasPrefix(tablePrefix)
}
}

0 comments on commit 533c777

Please sign in to comment.