diff --git a/iterator.go b/iterator.go index d124d77..aa32f14 100644 --- a/iterator.go +++ b/iterator.go @@ -51,8 +51,6 @@ func (i *Iterator[T]) Next() ([]byte, T, bool) { return nil, zero, false } - node.incrementLazyRefCount(1) - currentNode := node.(Node[T]) i.pos = currentNode @@ -126,7 +124,6 @@ func (i *Iterator[T]) Next() ([]byte, T, bool) { i.stack = newStack } } - node.incrementLazyRefCount(-1) } i.pos = nil return nil, zero, false @@ -158,10 +155,8 @@ func (i *Iterator[T]) SeekPrefixWatch(prefixKey []byte) (watch <-chan struct{}) for { // Check if the node matches the prefix - node.incrementLazyRefCount(1) if node.isLeaf() { - node.incrementLazyRefCount(-1) return watch } @@ -171,7 +166,6 @@ func (i *Iterator[T]) SeekPrefixWatch(prefixKey []byte) (watch <-chan struct{}) mismatchIdx := prefixMismatch[T](node, prefix, len(prefix), depth) if mismatchIdx < int(node.getPartialLen()) { // If there's a mismatch, set the node to nil to break the loop - node.incrementLazyRefCount(-1) if bytes.HasPrefix(node.getPartial(), prefix[depth:]) { i.node = node break @@ -187,7 +181,6 @@ func (i *Iterator[T]) SeekPrefixWatch(prefixKey []byte) (watch <-chan struct{}) child, _ := findChild[T](node, prefix[depth]) if child == nil { // If the child node doesn't exist, break the loop - node.incrementLazyRefCount(-1) node = nil i.node = nil break @@ -195,7 +188,6 @@ func (i *Iterator[T]) SeekPrefixWatch(prefixKey []byte) (watch <-chan struct{}) if depth == len(prefix) { // If the prefix is exhausted, break the loop - node.incrementLazyRefCount(-1) i.node = nil break } @@ -204,7 +196,6 @@ func (i *Iterator[T]) SeekPrefixWatch(prefixKey []byte) (watch <-chan struct{}) i.node = node // Move to the next level in the tree - node.incrementLazyRefCount(-1) parent = node watch = parent.getMutateCh() node = child @@ -274,7 +265,6 @@ func (i *Iterator[T]) SeekLowerBound(prefixKey []byte) { if node == nil { break } - node.incrementLazyRefCount(1) var prefixCmp int if int(node.getPartialLen()) < len(prefix) { @@ -341,7 +331,6 @@ func (i *Iterator[T]) SeekLowerBound(prefixKey []byte) { } // Move to the next level in the tree - node.incrementLazyRefCount(-1) node = node.getChild(idx) depth++ } diff --git a/tree.go b/tree.go index e268210..193b736 100644 --- a/tree.go +++ b/tree.go @@ -162,10 +162,8 @@ func (t *RadixTree[T]) Delete(key []byte) (*RadixTree[T], T, bool) { func (t *RadixTree[T]) iterativeSearch(key []byte) (T, bool, <-chan struct{}) { var zero T n := t.root - n.incrementLazyRefCount(1) watch := n.getMutateCh() if t.root == nil { - n.incrementLazyRefCount(-1) return zero, false, watch } var child Node[T] @@ -178,7 +176,6 @@ func (t *RadixTree[T]) iterativeSearch(key []byte) (T, bool, <-chan struct{}) { if isLeaf[T](n) { // Check if the expanded path matches if leafMatches(n.getKey(), key) == 0 { - n.incrementLazyRefCount(-1) return n.getValue(), true, watch } break @@ -188,29 +185,23 @@ func (t *RadixTree[T]) iterativeSearch(key []byte) (T, bool, <-chan struct{}) { if n.getPartialLen() > 0 { prefixLen := checkPrefix(n.getPartial(), int(n.getPartialLen()), key, depth) if prefixLen != min(maxPrefixLen, int(n.getPartialLen())) { - n.incrementLazyRefCount(-1) return zero, false, watch } depth += int(n.getPartialLen()) } if depth >= len(key) { - n.incrementLazyRefCount(-1) return zero, false, watch } // Recursively search child, _ = t.findChild(n, key[depth]) if child == nil { - n.incrementLazyRefCount(-1) return zero, false, watch } - n.incrementLazyRefCount(-1) n = child - n.incrementLazyRefCount(1) depth++ } - n.incrementLazyRefCount(-1) return zero, false, nil } diff --git a/txn.go b/txn.go index ef9666c..43f20dd 100644 --- a/txn.go +++ b/txn.go @@ -132,20 +132,13 @@ func (t *Txn[T]) recursiveInsert(node Node[T], key []byte, value T, depth int, o nodeKey := node.getKey() if len(key) == len(nodeKey) && bytes.Equal(nodeKey, key) { *old = 1 - doClone := node.getRefCount() > 1 if t.trackMutate { t.trackChannel(node) } - if doClone { - nl := t.makeLeaf(key, value) - node.incrementLazyRefCount(-1) - val := node.getValue() - return nl, val - } - oldV := node.getValue() - node.setValue(value) - node.processLazyRef() - return node, oldV + nl := t.makeLeaf(key, value) + node.incrementLazyRefCount(-1) + val := node.getValue() + return nl, val } // New value, we must split the leaf into a node4