Skip to content

Commit

Permalink
cache key buf
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepymole committed Nov 25, 2021
1 parent 8928683 commit 12d58d9
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions br/pkg/lightning/backend/local/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,7 @@ type Writer struct {
// if the kvs in writeBatch are in order, we can avoid doing a `sort.Slice` which
// is quite slow. in our bench, the sort operation eats about 5% of total CPU
isWriteBatchSorted bool
sortedKeyBuf []byte

batchCount int
batchSize int64
Expand Down Expand Up @@ -1019,10 +1020,13 @@ func (w *Writer) appendRowsSorted(kvs []common.KvPair) error {
// noopKeyAdapter doesn't really change the key,
// skipping the encoding to avoid unnecessary alloc and copy.
if _, ok := keyAdapter.(noopKeyAdapter); !ok {
buf := make([]byte, 0, totalKeySize)
if cap(w.sortedKeyBuf) < totalKeySize {
w.sortedKeyBuf = make([]byte, totalKeySize)
}
buf := w.sortedKeyBuf[:0]
newKvs := make([]common.KvPair, len(kvs))
for i := 0; i < len(kvs); i++ {
buf = keyAdapter.Encode(buf[:0], kvs[i].Key, kvs[i].RowID)
buf = keyAdapter.Encode(buf, kvs[i].Key, kvs[i].RowID)
newKvs[i] = common.KvPair{Key: buf, Val: kvs[i].Val}
buf = buf[len(buf):]
}
Expand Down

0 comments on commit 12d58d9

Please sign in to comment.