Skip to content

Commit

Permalink
kv: combine heap allocations for EndTxn requests
Browse files Browse the repository at this point in the history
This commit combines the 3 heap allocations incurred by a call to `kv.Txn.Commit`
or `kv.Txn.Rollback` when constructing the request into a single allocation.

```
name                   old time/op    new time/op    delta
KV/Scan/SQL/rows=1-10    95.3µs ± 9%    93.5µs ± 6%    ~     (p=0.497 n=10+9)

name                   old alloc/op   new alloc/op   delta
KV/Scan/SQL/rows=1-10    20.1kB ± 0%    20.1kB ± 0%    ~     (p=0.345 n=10+9)

name                   old allocs/op  new allocs/op  delta
KV/Scan/SQL/rows=1-10       245 ± 0%       243 ± 0%  -0.82%  (p=0.000 n=10+9)
```
  • Loading branch information
nvanbenschoten committed Dec 30, 2021
1 parent cc22c45 commit 79a5230
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions pkg/kv/txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -685,8 +685,8 @@ func (txn *Txn) commit(ctx context.Context) error {
// to reduce contention by releasing locks. In multi-tenant settings, it
// will be subject to admission control, and the zero CreateTime will give
// it preference within the tenant.
var ba roachpb.BatchRequest
ba.Add(endTxnReq(true /* commit */, txn.deadline(), txn.systemConfigTrigger))
et := endTxnReq(true /* commit */, txn.deadline(), txn.systemConfigTrigger)
ba := roachpb.BatchRequest{Requests: et.unionArr[:]}
_, pErr := txn.Send(ctx, ba)
if pErr == nil {
for _, t := range txn.commitTriggers {
Expand Down Expand Up @@ -741,7 +741,9 @@ func (txn *Txn) CommitInBatch(ctx context.Context, b *Batch) error {
if txn != b.txn {
return errors.Errorf("a batch b can only be committed by b.txn")
}
b.appendReqs(endTxnReq(true /* commit */, txn.deadline(), txn.systemConfigTrigger))
et := endTxnReq(true /* commit */, txn.deadline(), txn.systemConfigTrigger)
b.growReqs(1)
b.reqs[len(b.reqs)-1].Value = &et.union
b.initResult(1 /* calls */, 0, b.raw, nil)
return txn.Run(ctx, b)
}
Expand Down Expand Up @@ -859,8 +861,8 @@ func (txn *Txn) rollback(ctx context.Context) *roachpb.Error {
// order to reduce contention by releasing locks. In multi-tenant
// settings, it will be subject to admission control, and the zero
// CreateTime will give it preference within the tenant.
var ba roachpb.BatchRequest
ba.Add(endTxnReq(false /* commit */, nil /* deadline */, false /* systemConfigTrigger */))
et := endTxnReq(false /* commit */, nil /* deadline */, false /* systemConfigTrigger */)
ba := roachpb.BatchRequest{Requests: et.unionArr[:]}
_, pErr := txn.Send(ctx, ba)
if pErr == nil {
return nil
Expand All @@ -885,8 +887,8 @@ func (txn *Txn) rollback(ctx context.Context) *roachpb.Error {
// order to reduce contention by releasing locks. In multi-tenant
// settings, it will be subject to admission control, and the zero
// CreateTime will give it preference within the tenant.
var ba roachpb.BatchRequest
ba.Add(endTxnReq(false /* commit */, nil /* deadline */, false /* systemConfigTrigger */))
et := endTxnReq(false /* commit */, nil /* deadline */, false /* systemConfigTrigger */)
ba := roachpb.BatchRequest{Requests: et.unionArr[:]}
_ = contextutil.RunWithTimeout(ctx, "async txn rollback", asyncRollbackTimeout,
func(ctx context.Context) error {
if _, pErr := txn.Send(ctx, ba); pErr != nil {
Expand Down Expand Up @@ -920,19 +922,27 @@ func (txn *Txn) AddCommitTrigger(trigger func(ctx context.Context)) {
txn.commitTriggers = append(txn.commitTriggers, trigger)
}

func endTxnReq(commit bool, deadline *hlc.Timestamp, hasTrigger bool) roachpb.Request {
req := &roachpb.EndTxnRequest{
Commit: commit,
Deadline: deadline,
}
// endTxnReqAlloc is used to batch the heap allocations of an EndTxn request.
type endTxnReqAlloc struct {
req roachpb.EndTxnRequest
union roachpb.RequestUnion_EndTxn
unionArr [1]roachpb.RequestUnion
}

func endTxnReq(commit bool, deadline *hlc.Timestamp, hasTrigger bool) *endTxnReqAlloc {
alloc := new(endTxnReqAlloc)
alloc.req.Commit = commit
alloc.req.Deadline = deadline
if hasTrigger {
req.InternalCommitTrigger = &roachpb.InternalCommitTrigger{
alloc.req.InternalCommitTrigger = &roachpb.InternalCommitTrigger{
ModifiedSpanTrigger: &roachpb.ModifiedSpanTrigger{
SystemConfigSpan: true,
},
}
}
return req
alloc.union.EndTxn = &alloc.req
alloc.unionArr[0].Value = &alloc.union
return alloc
}

// AutoCommitError wraps a non-retryable error coming from auto-commit.
Expand Down

0 comments on commit 79a5230

Please sign in to comment.