Skip to content

Commit

Permalink
optimize batched iter
Browse files Browse the repository at this point in the history
Signed-off-by: ekexium <[email protected]>
  • Loading branch information
ekexium committed Jan 24, 2025
1 parent e1a3b5a commit 74a0617
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions internal/unionstore/memdb_art.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ func (db *artDBWithContext) BatchedSnapshotIter(lower, upper []byte, reverse boo
batchSize: 4,
}

// Position at first key immediately
iter.fillBatch()
return iter
}
Expand Down Expand Up @@ -269,11 +268,24 @@ func (it *snapshotBatchedIter) fillBatch() error {
// update state
it.pos = 0
if len(it.kvs) > 0 {
lastKV := it.kvs[len(it.kvs)-1]
lastKV := &it.kvs[len(it.kvs)-1]
keyLen := len(lastKV.Key)

if it.reverse {
it.nextKey = append([]byte(nil), lastKV.Key...)
if cap(it.nextKey) >= keyLen {
it.nextKey = it.nextKey[:keyLen]
} else {
it.nextKey = make([]byte, keyLen)
}
copy(it.nextKey, lastKV.Key)
} else {
it.nextKey = append(append([]byte(nil), lastKV.Key...), 0)
if cap(it.nextKey) >= keyLen+1 {
it.nextKey = it.nextKey[:keyLen+1]
} else {
it.nextKey = make([]byte, keyLen+1)
}
copy(it.nextKey, lastKV.Key)
it.nextKey[keyLen] = 0
}
} else {
it.nextKey = nil
Expand Down

0 comments on commit 74a0617

Please sign in to comment.