Skip to content

Commit d090658

Browse files
yuzefovichmichae2
authored andcommitted
kvcoord: optimize txn write buffer for read-only txns
In the txn write buffer we need to merge the ScanResponse coming from the server with any overlapping writes that we've buffered. This requires us to allocate `respMerger.batchResponses` which is the "staging" area of the merged response. However, if we don't have any overlapping buffered writes, then we simply copy the server response into that staging area. This commit adds an optimization to avoid this extra copy in a special but common case when the buffer is empty (i.e. we're in a read-only txn). We could extend the optimization further but that is left as a TODO. Release note: None
1 parent ec2a260 commit d090658

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

pkg/kv/kvclient/kvcoord/txn_interceptor_write_buffer.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,6 +1162,15 @@ func (twb *txnWriteBuffer) mergeWithScanResp(
11621162
"with COL_BATCH_RESPONSE scan format")
11631163
}
11641164

1165+
if twb.buffer.Len() == 0 {
1166+
// If we haven't buffered any writes, then we can just return the server
1167+
// response unchanged.
1168+
// TODO(yuzefovich): we could take the optimization further by examining
1169+
// whether any buffered writes overlap with the Scan request and
1170+
// skipping the merge step if not.
1171+
return resp, nil
1172+
}
1173+
11651174
respIter := newScanRespIter(req, resp)
11661175
// First, calculate the size of the merged response. This then allows us to
11671176
// exactly pre-allocate the response slice when constructing the respMerger.
@@ -1186,6 +1195,15 @@ func (twb *txnWriteBuffer) mergeWithReverseScanResp(
11861195
"ReverseScanRequest with COL_BATCH_RESPONSE scan format")
11871196
}
11881197

1198+
if twb.buffer.Len() == 0 {
1199+
// If we haven't buffered any writes, then we can just return the server
1200+
// response unchanged.
1201+
// TODO(yuzefovich): we could take the optimization further by examining
1202+
// whether any buffered writes overlap with the ReverseScan request and
1203+
// skipping the merge step if not.
1204+
return resp, nil
1205+
}
1206+
11891207
respIter := newReverseScanRespIter(req, resp)
11901208
// First, calculate the size of the merged response. This then allows us to
11911209
// exactly pre-allocate the response slice when constructing the respMerger.

0 commit comments

Comments
 (0)