Skip to content

Commit

Permalink
Preallocate buffers to avoid repeated allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
terencechain committed Dec 18, 2024
1 parent bca3668 commit 84d6500
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
20 changes: 14 additions & 6 deletions beacon-chain/core/altair/sync_committee.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,11 @@ func NextSyncCommitteeIndices(ctx context.Context, s state.BeaconState) ([]primi
cIndices := make([]primitives.ValidatorIndex, 0, syncCommitteeSize)
hashFunc := hash.CustomSHA256Hasher()

for i := primitives.ValidatorIndex(0); uint64(len(cIndices)) < params.BeaconConfig().SyncCommitteeSize; i++ {
// Preallocate buffers to avoid repeated allocations
seedBuffer := make([]byte, len(seed)+8)
copy(seedBuffer, seed[:])

for i := primitives.ValidatorIndex(0); uint64(len(cIndices)) < syncCommitteeSize; i++ {
if ctx.Err() != nil {
return nil, ctx.Err()
}
Expand All @@ -142,16 +146,20 @@ func NextSyncCommitteeIndices(ctx context.Context, s state.BeaconState) ([]primi
effectiveBal := v.EffectiveBalance()

if s.Version() >= version.Electra {
b := append(seed[:], bytesutil.Bytes8(uint64(i/16))...)
randomByte := hashFunc(b)
// Use the preallocated seed buffer
binary.LittleEndian.PutUint64(seedBuffer[len(seed):], uint64(i/16))
randomByte := hashFunc(seedBuffer)
offset := (i % 16) * 2
randomByteSlice := bytesutil.PadTo(randomByte[offset:offset+2], 8)
if effectiveBal*fieldparams.MaxRandomValueElectra >= cfg.MaxEffectiveBalanceElectra*binary.LittleEndian.Uint64(randomByteSlice) {
randomValue := binary.LittleEndian.Uint64(randomByteSlice)

if effectiveBal*fieldparams.MaxRandomValueElectra >= cfg.MaxEffectiveBalanceElectra*randomValue {
cIndices = append(cIndices, cIndex)
}
} else {
b := append(seed[:], bytesutil.Bytes8(uint64(i.Div(32)))...)
randomByte := hashFunc(b)[i%32]
// Use the preallocated seed buffer
binary.LittleEndian.PutUint64(seedBuffer[len(seed):], uint64(i/32))
randomByte := hashFunc(seedBuffer)[i%32]
if effectiveBal*fieldparams.MaxRandomValue >= cfg.MaxEffectiveBalance*uint64(randomByte) {
cIndices = append(cIndices, cIndex)
}
Expand Down
19 changes: 13 additions & 6 deletions beacon-chain/core/helpers/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,9 @@ func ComputeProposerIndex(bState state.ReadOnlyBeaconState, activeIndices []prim
return 0, errors.New("empty active indices list")
}
hashFunc := hash.CustomSHA256Hasher()
beaconConfig := params.BeaconConfig()
seedBuffer := make([]byte, len(seed)+8)
copy(seedBuffer, seed[:])

for i := uint64(0); ; i++ {
candidateIndex, err := ComputeShuffledIndex(primitives.ValidatorIndex(i%length), length, seed, true /* shuffle */)
Expand All @@ -390,17 +393,21 @@ func ComputeProposerIndex(bState state.ReadOnlyBeaconState, activeIndices []prim
}
effectiveBal := v.EffectiveBalance()
if bState.Version() >= version.Electra {
b := append(seed[:], bytesutil.Bytes8(i/16)...)
randomByte := hashFunc(b)
binary.LittleEndian.PutUint64(seedBuffer[len(seed):], i/16)
randomByte := hashFunc(seedBuffer)
offset := (i % 16) * 2

randomByteSlice := bytesutil.PadTo(randomByte[offset:offset+2], 8)
if effectiveBal*fieldparams.MaxRandomValueElectra >= params.BeaconConfig().MaxEffectiveBalanceElectra*binary.LittleEndian.Uint64(randomByteSlice) {
randomValue := binary.LittleEndian.Uint64(randomByteSlice)

if effectiveBal*fieldparams.MaxRandomValueElectra >= beaconConfig.MaxEffectiveBalanceElectra*randomValue {
return candidateIndex, nil
}
} else {
b := append(seed[:], bytesutil.Bytes8(i/32)...)
randomByte := hashFunc(b)[i%32]
if effectiveBal*fieldparams.MaxRandomValue >= params.BeaconConfig().MaxEffectiveBalance*uint64(randomByte) {
binary.LittleEndian.PutUint64(seedBuffer[len(seed):], i/32)
randomByte := hashFunc(seedBuffer)[i%32]

if effectiveBal*fieldparams.MaxRandomValue >= beaconConfig.MaxEffectiveBalance*uint64(randomByte) {
return candidateIndex, nil
}
}
Expand Down

0 comments on commit 84d6500

Please sign in to comment.