Skip to content

Commit

Permalink
Faster Clone() in kv/key.go (#11957)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangyunhao116 authored and sre-bot committed Sep 10, 2019
1 parent 5c18c5d commit 660ce3f
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions kv/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type Key []byte
// Next returns the next key in byte-order.
func (k Key) Next() Key {
// add 0x0 to the end of key
buf := make([]byte, len([]byte(k))+1)
buf := make([]byte, len(k)+1)
copy(buf, []byte(k))
return buf
}
Expand All @@ -41,7 +41,7 @@ func (k Key) Next() Key {
// If we seek 'rowkey1' Next, we will get 'rowkey1_column1'.
// If we seek 'rowkey1' PrefixNext, we will get 'rowkey2'.
func (k Key) PrefixNext() Key {
buf := make([]byte, len([]byte(k)))
buf := make([]byte, len(k))
copy(buf, []byte(k))
var i int
for i = len(k) - 1; i >= 0; i-- {
Expand All @@ -68,9 +68,11 @@ func (k Key) HasPrefix(prefix Key) bool {
return bytes.HasPrefix(k, prefix)
}

// Clone returns a copy of the Key.
// Clone returns a deep copy of the Key.
func (k Key) Clone() Key {
return append([]byte(nil), k...)
ck := make([]byte, len(k))
copy(ck, []byte(k))
return ck
}

// String implements fmt.Stringer interface.
Expand Down

0 comments on commit 660ce3f

Please sign in to comment.