Skip to content

Commit

Permalink
Caplin: reduced in MerkleTree's CopyInto (#12696)
Browse files Browse the repository at this point in the history
Reduced allocs by 58% overall
  • Loading branch information
Giulio2002 authored Nov 12, 2024
1 parent 159b74e commit 7dbb5f4
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
2 changes: 0 additions & 2 deletions cl/cltypes/solid/uint64slice_byte.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ func (arr *byteBasedUint64Slice) Clear() {

// CopyTo copies the slice to a target slice.
func (arr *byteBasedUint64Slice) CopyTo(target *byteBasedUint64Slice) {
target.Clear()
// TODO: implement CopyTo for MPT
target.c = arr.c
target.l = arr.l
if len(target.u) < len(arr.u) {
Expand Down
31 changes: 28 additions & 3 deletions cl/merkle_tree/merkle_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,16 +208,41 @@ 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) {
// reset the internal layers
for i := len(m.layers); i < len(other.layers); i++ {
other.layers[i] = other.layers[i][:0]
}
other.layers = other.layers[:len(m.layers)]
}

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]) {
other.layers[i] = make([]byte, len(m.layers[i]), (len(m.layers[i])*3)/2)
}
// 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 {
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
1 change: 0 additions & 1 deletion cl/phase1/network/services/aggregate_and_proof_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,6 @@ func (a *aggregateAndProofServiceImpl) loop(ctx context.Context) {
return true
}
if err := a.ProcessMessage(ctx, nil, job.aggregate); err != nil {
log.Trace("blob sidecar verification failed", "err", err)
return true
}

Expand Down

0 comments on commit 7dbb5f4

Please sign in to comment.