Skip to content

Commit 8533536

Browse files
committed
kvcoord: pre-allocate requests and positions for truncate
Release note: None
1 parent 6d2a724 commit 8533536

File tree

1 file changed

+48
-12
lines changed

1 file changed

+48
-12
lines changed

pkg/kv/kvclient/kvcoord/batch.go

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -476,24 +476,42 @@ func (h *BatchTruncationHelper) Truncate(
476476
// non-trivial slowdown and increase in allocations, so we choose to duplicate
477477
// the code for performance.
478478
func (h *BatchTruncationHelper) truncateAsc(rs roachpb.RSpan) ([]kvpb.RequestUnion, []int, error) {
479-
var truncReqs []kvpb.RequestUnion
480-
var positions []int
479+
endIdx := len(h.positions) - 1
480+
fullyProcessed := 0
481481
for i := h.startIdx; i < len(h.positions); i++ {
482482
pos := h.positions[i]
483483
if pos < 0 {
484-
// This request has already been fully processed, so there is no
485-
// need to look at it.
484+
fullyProcessed++
486485
continue
487486
}
488487
header := h.headers[i]
489-
// rs.EndKey can't be local because it contains range split points,
490-
// which are never local.
491488
ek := rs.EndKey.AsRawKey()
492489
if ek.Compare(header.Key) <= 0 {
493490
// All of the remaining requests start after this range, so we're
494491
// done.
492+
endIdx = i - 1
495493
break
496494
}
495+
}
496+
497+
numReqs := endIdx - h.startIdx - fullyProcessed + 1
498+
if numReqs == 0 {
499+
return nil, nil, nil
500+
}
501+
truncReqs := make([]kvpb.RequestUnion, 0, numReqs)
502+
positions := make([]int, 0, numReqs)
503+
504+
for i := h.startIdx; i <= endIdx; i++ {
505+
pos := h.positions[i]
506+
if pos < 0 {
507+
// This request has already been fully processed, so there is no
508+
// need to look at it.
509+
continue
510+
}
511+
header := h.headers[i]
512+
// rs.EndKey can't be local because it contains range split points,
513+
// which are never local.
514+
ek := rs.EndKey.AsRawKey()
497515
if !h.isRange[i] {
498516
// This is a point request, and the key is contained within this
499517
// range, so we include the request as is and mark it as "fully
@@ -635,24 +653,42 @@ func (h *BatchTruncationHelper) truncateAsc(rs roachpb.RSpan) ([]kvpb.RequestUni
635653
// non-trivial slowdown and increase in allocations, so we choose to duplicate
636654
// the code for performance.
637655
func (h *BatchTruncationHelper) truncateDesc(rs roachpb.RSpan) ([]kvpb.RequestUnion, []int, error) {
638-
var truncReqs []kvpb.RequestUnion
639-
var positions []int
656+
endIdx := len(h.positions) - 1
657+
fullyProcessed := 0
640658
for i := h.startIdx; i < len(h.positions); i++ {
641659
pos := h.positions[i]
642660
if pos < 0 {
643-
// This request has already been fully processed, so there is no
644-
// need to look at it.
661+
fullyProcessed++
645662
continue
646663
}
647664
header := h.headers[i]
648-
// rs.Key can't be local because it contains range split points, which
649-
// are never local.
650665
sk := rs.Key.AsRawKey()
651666
if sk.Compare(header.EndKey) >= 0 {
652667
// All of the remaining requests end before this range, so we're
653668
// done.
669+
endIdx = i - 1
654670
break
655671
}
672+
}
673+
674+
numReqs := endIdx - h.startIdx - fullyProcessed + 1
675+
if numReqs == 0 {
676+
return nil, nil, nil
677+
}
678+
truncReqs := make([]kvpb.RequestUnion, 0, numReqs)
679+
positions := make([]int, 0, numReqs)
680+
681+
for i := h.startIdx; i <= endIdx; i++ {
682+
pos := h.positions[i]
683+
if pos < 0 {
684+
// This request has already been fully processed, so there is no
685+
// need to look at it.
686+
continue
687+
}
688+
header := h.headers[i]
689+
// rs.Key can't be local because it contains range split points, which
690+
// are never local.
691+
sk := rs.Key.AsRawKey()
656692
if !h.isRange[i] {
657693
// This is a point request, and the key is contained within this
658694
// range, so we include the request as is and mark it as "fully

0 commit comments

Comments
 (0)