Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Caplin: reduced in MerkleTree's CopyInto #12696

Merged
merged 6 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions cl/merkle_tree/merkle_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,16 +208,42 @@ func (m *MerkleTree) CopyInto(other *MerkleTree) {
defer m.mu.RUnlock()
defer other.mu.Unlock()
other.computeLeaf = m.computeLeaf
other.layers = make([][]byte, len(m.layers))
if len(other.layers) > len(m.layers) {
for i := len(m.layers); i < len(other.layers); i++ {
other.layers[i] = other.layers[i][:0]
Giulio2002 marked this conversation as resolved.
Show resolved Hide resolved
}
other.layers = m.layers[:0]
Giulio2002 marked this conversation as resolved.
Show resolved Hide resolved
}

if len(m.layers) > len(other.layers) {
for len(other.layers) != len(m.layers) {
idx := len(other.layers)
other.layers = append(other.layers, make([]byte, len(m.layers[idx]), (len(m.layers[idx])*3)/2))
}
}

for i := 0; i < len(m.layers); i++ {
other.layers[i] = make([]byte, len(m.layers[i]))
// If the destination buffer is too short, extend it
if len(m.layers[i]) > cap(other.layers[i]) {
tmp := other.layers[i]
other.layers[i] = make([]byte, len(m.layers[i]), (len(m.layers[i])*3)/2)
copy(other.layers[i], tmp)
shotasilagadze marked this conversation as resolved.
Show resolved Hide resolved
}
// Normalizr the destination length
other.layers[i] = other.layers[i][:len(m.layers[i])]

// Now that the 2 slices are of equal length we can do a simple memcopy
copy(other.layers[i], m.layers[i])
}

other.leavesCount = m.leavesCount
other.limit = m.limit
other.dirtyLeaves = make([]atomic.Bool, len(m.dirtyLeaves))
//other.dirtyLeaves = make([]atomic.Bool, len(m.dirtyLeaves))

for i := 0; i < len(m.dirtyLeaves); i++ {
if i >= len(other.dirtyLeaves) {
other.dirtyLeaves = append(other.dirtyLeaves, atomic.Bool{})
}
other.dirtyLeaves[i].Store(m.dirtyLeaves[i].Load())
}
}
Expand Down
8 changes: 7 additions & 1 deletion cl/phase1/core/state/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@ func (b *CachingBeaconState) copyCachesInto(bs *CachingBeaconState) error {
if bs.publicKeyIndicies == nil {
bs.publicKeyIndicies = make(map[[48]byte]uint64)
}
for k := range bs.publicKeyIndicies {
for k, idx := range bs.publicKeyIndicies {
Giulio2002 marked this conversation as resolved.
Show resolved Hide resolved
if otherIdx, ok := b.publicKeyIndicies[k]; ok {
if idx != otherIdx {
delete(bs.publicKeyIndicies, k)
}
continue
}
delete(bs.publicKeyIndicies, k)
}
for pk, index := range b.publicKeyIndicies {
Expand Down
Loading