Skip to content

Commit 1ed2fa1

Browse files
committed
spanset: do timestamp check first
All branches in this loop check timestamp at the end. This was unnecessarily verbose, error-prone, and led to a bug fixed in the previous commit. Check the timestamp at the beginning once instead. Epic: none Release note: none
1 parent 7329637 commit 1ed2fa1

File tree

1 file changed

+8
-30
lines changed

1 file changed

+8
-30
lines changed

pkg/kv/kvserver/spanset/merge.go

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -32,26 +32,22 @@ func mergeSpans(latches []Span) []Span {
3232

3333
for _, cur := range latches[1:] {
3434
prev := &r[len(r)-1]
35+
// Can only merge spans at the same timestamp.
36+
if cur.Timestamp != prev.Timestamp {
37+
r = append(r, cur)
38+
continue
39+
}
3540
if len(cur.EndKey) == 0 && len(prev.EndKey) == 0 {
3641
if cur.Key.Compare(prev.Key) != 0 {
3742
// [a, nil] merge [b, nil]
3843
r = append(r, cur)
39-
} else {
40-
// [a, nil] merge [a, nil]
41-
if cur.Timestamp != prev.Timestamp {
42-
r = append(r, cur)
43-
}
4444
}
4545
continue
4646
}
4747
if len(prev.EndKey) == 0 {
4848
if cur.Key.Compare(prev.Key) == 0 {
4949
// [a, nil] merge [a, b]
50-
if cur.Timestamp != prev.Timestamp {
51-
r = append(r, cur)
52-
} else {
53-
prev.EndKey = cur.EndKey
54-
}
50+
prev.EndKey = cur.EndKey
5551
} else {
5652
// [a, nil] merge [b, c]
5753
r = append(r, cur)
@@ -63,29 +59,11 @@ func mergeSpans(latches []Span) []Span {
6359
if cur.EndKey != nil {
6460
if prev.EndKey.Compare(cur.EndKey) < 0 {
6561
// [a, c] merge [b, d]
66-
if cur.Timestamp != prev.Timestamp {
67-
r = append(r, cur)
68-
} else {
69-
prev.EndKey = cur.EndKey
70-
}
71-
} else {
72-
// [a, c] merge [b, c]
73-
if cur.Timestamp != prev.Timestamp {
74-
r = append(r, cur)
75-
}
62+
prev.EndKey = cur.EndKey
7663
}
7764
} else if c == 0 {
7865
// [a, b] merge [b, nil]
79-
if cur.Timestamp != prev.Timestamp {
80-
r = append(r, cur)
81-
} else {
82-
prev.EndKey = cur.Key.Next()
83-
}
84-
} else {
85-
// [a, c] merge [b, nil]
86-
if cur.Timestamp != prev.Timestamp {
87-
r = append(r, cur)
88-
}
66+
prev.EndKey = cur.Key.Next()
8967
}
9068
continue
9169
}

0 commit comments

Comments
 (0)