Skip to content

Commit 037cd18

Browse files
committed
part: Fix MapTxn reusability
Now that txnIDs are the mechanism to detect if a node can be mutated or not we need to bump txnID in when committing to allow MapTxn to keep reusing part.Txn. Rename 'prevTxnID' to 'nextTxnID' and move incrementing the txnID from 'Tree.Txn()' into 'Txn.Commit()'. Signed-off-by: Jussi Maki <jussi@isovalent.com>
1 parent 46760ca commit 037cd18

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

part/tree.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type Tree[T any] struct {
1919
size int // the number of objects in the tree
2020
opts options
2121
prevTxn *atomic.Pointer[Txn[T]] // the previous txn for reusing the allocation
22-
prevTxnID uint64 // the transaction ID that produced this tree
22+
nextTxnID uint64 // the next transaction ID to use
2323
}
2424

2525
// New constructs a new tree.
@@ -74,7 +74,7 @@ func (t *Tree[T]) Txn() *Txn[T] {
7474
txn.rootWatch = t.rootWatch
7575
txn.size = t.size
7676
txn.prevTxn = t.prevTxn
77-
txn.txnID = t.prevTxnID + 1
77+
txn.txnID = t.nextTxnID
7878
return txn
7979
}
8080

part/txn.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func (txn *Txn[T]) Clone() Tree[T] {
6565
rootWatch: txn.rootWatch,
6666
size: txn.size,
6767
prevTxn: txn.prevTxn,
68-
prevTxnID: txn.txnID,
68+
nextTxnID: txn.txnID,
6969
}
7070
}
7171

@@ -185,13 +185,14 @@ func (txn *Txn[T]) Commit() Tree[T] {
185185
validateTree(txn.oldRoot, nil, nil, txn.txnID)
186186
validateTree(txn.root, nil, txn.watches, txn.txnID)
187187
}
188+
txn.txnID++
188189
t := Tree[T]{
189190
opts: txn.opts,
190191
root: txn.root,
191192
rootWatch: newRootWatch,
192193
size: txn.size,
193194
prevTxn: txn.prevTxn,
194-
prevTxnID: txn.txnID,
195+
nextTxnID: txn.txnID,
195196
}
196197
// Store this txn in the tree to reuse the allocation next time.
197198
t.prevTxn.Store(txn)

0 commit comments

Comments
 (0)