From 5c5d86450bafd3edcad471591e11afead7fad0fe Mon Sep 17 00:00:00 2001 From: yxq <707312973@qq.com> Date: Wed, 24 Nov 2021 16:42:53 +0800 Subject: [PATCH] perf: update key generator --- common/db/mvcc.go | 44 +++++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/common/db/mvcc.go b/common/db/mvcc.go index 11a5ed6e2c..9083669eb2 100644 --- a/common/db/mvcc.go +++ b/common/db/mvcc.go @@ -433,37 +433,47 @@ func pad(version int64) []byte { //GetKeyPerfix 获取key前缀 func GetKeyPerfix(key []byte) []byte { - b := append([]byte{}, mvccData...) - newkey := append(b, key...) - newkey = append(newkey, []byte(".")...) - return newkey + result := make([]byte, 0, len(mvccData) + len(key) + 1) + result = append(result, mvccData...) + result = append(result, key...) + result = append(result, []byte(".")...) + return result } //GetKey 获取键 func GetKey(key []byte, version int64) ([]byte, error) { - newkey := append(GetKeyPerfix(key), pad(version)...) - return newkey, nil + result := make([]byte, 0, len(mvccData) + len(key) + 21) + result = append(result, mvccData...) + result = append(result, key...) + result = append(result, []byte(".")...) + result = append(result, pad(version)...) + return result, nil } func getLastKey(key []byte) []byte { - b := append([]byte{}, mvccLast...) - return append(b, key...) + result := make([]byte, 0, len(mvccLast) + len(key)) + result = append(result, mvccLast...) + result = append(result, key...) + return result } func getVersionHashKey(hash []byte) []byte { - b := append([]byte{}, mvccMeta...) - key := append(b, hash...) - return key + result := make([]byte, 0, len(mvccMeta) + len(hash)) + result = append(result, mvccMeta...) + result = append(result, hash...) + return result } func getVersionKey(version int64) []byte { - b := append([]byte{}, mvccMetaVersion...) - key := append(b, pad(version)...) - return key + result := make([]byte, 0, len(mvccMetaVersion) + 20) + result = append(result, mvccMetaVersion...) + result = append(result, pad(version)...) + return result } func getVersionKeyListKey(version int64) []byte { - b := append([]byte{}, mvccMetaVersionKeyList...) - key := append(b, pad(version)...) - return key + result := make([]byte, 0, len(mvccMetaVersionKeyList) + 20) + result = append(result, mvccMetaVersionKeyList...) + result = append(result, pad(version)...) + return result }