Skip to content

Commit

Permalink
feat: reorganise extension node insertion to use seperate pointers fo…
Browse files Browse the repository at this point in the history
…r the child node
  • Loading branch information
h5law committed Mar 19, 2024
1 parent 4831b52 commit 6f0f1e6
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions smt.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,19 +178,36 @@ func (smt *SMT) update(
}
// We insert an "extension" representing multiple single-branch inner nodes
last := &node
var newInner *innerNode
if getPathBit(path, prefixlen) == left {
newInner = &innerNode{
leftChild: newLeaf,
rightChild: leaf,
}
} else {
newInner = &innerNode{
leftChild: leaf,
rightChild: newLeaf,
}
}
// Determine if we need to insert an extension or a branch
if depth < prefixlen {
// note: this keeps path slice alive - GC inefficiency?
if depth > 0xff {
panic("invalid depth")
}
ext := extensionNode{path: path, pathBounds: [2]byte{byte(depth), byte(prefixlen)}}
ext := extensionNode{
child: newInner,
path: path,
pathBounds: [2]byte{
byte(depth), byte(prefixlen),
},
}
// Dereference the last node to replace it with the extension node
*last = &ext
last = &ext.child
}
if getPathBit(path, prefixlen) == left {
*last = &innerNode{leftChild: newLeaf, rightChild: leaf}
} else {
*last = &innerNode{leftChild: leaf, rightChild: newLeaf}
// Dereference the last node to replace it with the new inner node
*last = newInner
}
return node, nil
}
Expand Down

0 comments on commit 6f0f1e6

Please sign in to comment.