Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: tikv/client-go
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 74a0617d8eb3e785dd512f8755a43b185f1ed558
Choose a base ref
..
head repository: tikv/client-go
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 29fc98ea5f26a83902c15bdadacbd293536dd161
Choose a head ref
Showing with 24 additions and 19 deletions.
  1. +24 −19 internal/unionstore/memdb_art.go
43 changes: 24 additions & 19 deletions internal/unionstore/memdb_art.go
Original file line number Diff line number Diff line change
@@ -203,7 +203,8 @@ type snapshotBatchedIter struct {
reverse bool

// current batch
kvs []KvPair
keys [][]byte
values [][]byte
pos int
batchSize int
nextKey []byte
@@ -216,7 +217,7 @@ func (db *artDBWithContext) BatchedSnapshotIter(lower, upper []byte, reverse boo
lower: lower,
upper: upper,
reverse: reverse,
batchSize: 4,
batchSize: 32,
}

iter.fillBatch()
@@ -232,10 +233,12 @@ func (it *snapshotBatchedIter) fillBatch() error {
it.db.RLock()
defer it.db.RUnlock()

if it.kvs == nil {
it.kvs = make([]KvPair, 0, it.batchSize)
if it.keys == nil || it.values == nil || cap(it.keys) < it.batchSize || cap(it.values) < it.batchSize {
it.keys = make([][]byte, 0, it.batchSize)
it.values = make([][]byte, 0, it.batchSize)
} else {
it.kvs = it.kvs[:0]
it.keys = it.keys[:0]
it.values = it.values[:0]
}

var snapshotIter Iterator
@@ -255,36 +258,37 @@ func (it *snapshotBatchedIter) fillBatch() error {
defer snapshotIter.Close()

// fill current batch
// Further optimization: let the underlying memdb support batch iter.
for i := 0; i < it.batchSize && snapshotIter.Valid(); i++ {
it.kvs = append(it.kvs, KvPair{
Key: snapshotIter.Key(),
Value: snapshotIter.Value(),
})
it.keys = it.keys[:i+1]
it.values = it.values[:i+1]
it.keys[i] = snapshotIter.Key()
it.values[i] = snapshotIter.Value()
if err := snapshotIter.Next(); err != nil {
return err
}
}

// update state
it.pos = 0
if len(it.kvs) > 0 {
lastKV := &it.kvs[len(it.kvs)-1]
keyLen := len(lastKV.Key)
if len(it.keys) > 0 {
lastKey := it.keys[len(it.keys)-1]
keyLen := len(lastKey)

if it.reverse {
if cap(it.nextKey) >= keyLen {
it.nextKey = it.nextKey[:keyLen]
} else {
it.nextKey = make([]byte, keyLen)
}
copy(it.nextKey, lastKV.Key)
copy(it.nextKey, lastKey)
} else {
if cap(it.nextKey) >= keyLen+1 {
it.nextKey = it.nextKey[:keyLen+1]
} else {
it.nextKey = make([]byte, keyLen+1)
}
copy(it.nextKey, lastKV.Key)
copy(it.nextKey, lastKey)
it.nextKey[keyLen] = 0
}
} else {
@@ -297,7 +301,7 @@ func (it *snapshotBatchedIter) fillBatch() error {

func (it *snapshotBatchedIter) Valid() bool {
return it.snapshotTruncateSeqNo == it.db.SnapshotSeqNo &&
it.pos < len(it.kvs)
it.pos < len(it.keys)
}

func (it *snapshotBatchedIter) Next() error {
@@ -312,7 +316,7 @@ func (it *snapshotBatchedIter) Next() error {
}

it.pos++
if it.pos >= len(it.kvs) {
if it.pos >= len(it.keys) {
return it.fillBatch()
}
return nil
@@ -322,17 +326,18 @@ func (it *snapshotBatchedIter) Key() []byte {
if !it.Valid() {
return nil
}
return it.kvs[it.pos].Key
return it.keys[it.pos]
}

func (it *snapshotBatchedIter) Value() []byte {
if !it.Valid() {
return nil
}
return it.kvs[it.pos].Value
return it.values[it.pos]
}

func (it *snapshotBatchedIter) Close() {
it.kvs = nil
it.keys = nil
it.values = nil
it.nextKey = nil
}