Skip to content

Commit

Permalink
lil eth's suggestion (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Nov 12, 2021
1 parent bee1fac commit b26cb37
Showing 1 changed file with 11 additions and 28 deletions.
39 changes: 11 additions & 28 deletions lib/trie/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,45 +98,28 @@ func (l *leaf) setGeneration(generation uint64) {
func (b *branch) copy() node {
b.Lock()
defer b.Unlock()
cpy := &branch{
key: make([]byte, len(b.key)),
children: [16]node{},
value: nil,
return &branch{
key: b.key,
children: b.children,
value: b.value,
dirty: b.dirty,
hash: make([]byte, len(b.hash)),
encoding: make([]byte, len(b.encoding)),
hash: b.hash,
encoding: b.encoding,
generation: b.generation,
}
copy(cpy.key, b.key)
copy(cpy.children[:], b.children[:]) // copy interface pointers

// nil and []byte{} are encoded differently, watch out!
if b.value != nil {
cpy.value = make([]byte, len(b.value))
copy(cpy.value, b.value)
}

copy(cpy.hash, b.hash)
copy(cpy.encoding, b.encoding)
return cpy
}

func (l *leaf) copy() node {
l.Lock()
defer l.Unlock()
cpy := &leaf{
key: make([]byte, len(l.key)),
value: make([]byte, len(l.value)),
return &leaf{
key: l.key,
value: l.value,
dirty: l.dirty,
hash: make([]byte, len(l.hash)),
encoding: make([]byte, len(l.encoding)),
hash: l.hash,
encoding: l.encoding,
generation: l.generation,
}
copy(cpy.key, l.key)
copy(cpy.value, l.value)
copy(cpy.hash, l.hash)
copy(cpy.encoding, l.encoding)
return cpy
}

func (b *branch) setEncodingAndHash(enc, hash []byte) {
Expand Down

0 comments on commit b26cb37

Please sign in to comment.