Skip to content

Commit 13893c7

Browse files
committed
Optimize by pre allocate slices to avoid runtime growslice
1 parent b4a66a0 commit 13893c7

File tree

6 files changed

+40
-9
lines changed

6 files changed

+40
-9
lines changed

beacon-chain/core/helpers/randao.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,13 @@ func Seed(state state.ReadOnlyBeaconState, epoch primitives.Epoch, domain [bls.D
2929
if err != nil {
3030
return [32]byte{}, err
3131
}
32-
seed := append(domain[:], bytesutil.Bytes8(uint64(epoch))...)
33-
seed = append(seed, randaoMix...)
3432

35-
seed32 := hash.Hash(seed)
33+
var seed [bls.DomainByteLength + 8 + 32]byte
34+
copy(seed[0:], domain[:])
35+
copy(seed[bls.DomainByteLength:], bytesutil.Bytes8(uint64(epoch)))
36+
copy(seed[bls.DomainByteLength+8:], randaoMix[:])
37+
38+
seed32 := hash.Hash(seed[:])
3639

3740
return seed32, nil
3841
}

beacon-chain/core/signing/signing_root.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,9 @@ func ComputeDomain(domainType [DomainByteLength]byte, forkVersion, genesisValida
247247

248248
// This returns the bls domain given by the domain type and fork data root.
249249
func domain(domainType [DomainByteLength]byte, forkDataRoot []byte) []byte {
250-
var b []byte
251-
b = append(b, domainType[:4]...)
252-
b = append(b, forkDataRoot[:28]...)
250+
b := make([]byte, 32)
251+
copy(b[0:4], domainType[:4])
252+
copy(b[4:], forkDataRoot[:28])
253253
return b
254254
}
255255

beacon-chain/state/stateutil/participation_bit_root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func ParticipationBitsRoot(bits []byte) ([32]byte, error) {
3232
// it does not have length bytes per chunk.
3333
func packParticipationBits(bytes []byte) ([][32]byte, error) {
3434
numItems := len(bytes)
35-
chunks := make([][32]byte, 0, numItems/32)
35+
chunks := make([][32]byte, 0, (numItems+31)/32)
3636
for i := 0; i < numItems; i += 32 {
3737
j := i + 32
3838
// We create our upper bound index of the chunk, if it is greater than numItems,

beacon-chain/sync/validate_aggregate_proof.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,9 @@ func (s *Service) hasSeenAggregatorIndexEpoch(epoch primitives.Epoch, aggregator
265265
func (s *Service) setAggregatorIndexEpochSeen(epoch primitives.Epoch, aggregatorIndex primitives.ValidatorIndex) {
266266
s.seenAggregatedAttestationLock.Lock()
267267
defer s.seenAggregatedAttestationLock.Unlock()
268-
b := append(bytesutil.Bytes32(uint64(epoch)), bytesutil.Bytes32(uint64(aggregatorIndex))...)
268+
b := make([]byte, 64)
269+
copy(b[0:32], bytesutil.Bytes32(uint64(epoch)))
270+
copy(b[32:], bytesutil.Bytes32(uint64(aggregatorIndex)))
269271
s.seenAggregatedAttestationCache.Add(string(b), true)
270272
}
271273

changelog/tt_bread.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
### Ignored
2+
3+
- Pre allocate slices if we can to avoid grow slice.

crypto/bls/signature_batch.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,31 @@ func NewSet() *SignatureBatch {
3030
}
3131
}
3232

33-
// Join merges the provided signature batch to out current one.
3433
func (s *SignatureBatch) Join(set *SignatureBatch) *SignatureBatch {
34+
total := len(s.Signatures) + len(set.Signatures)
35+
36+
// Preallocate capacity if needed
37+
if cap(s.Signatures) < total {
38+
newSigs := make([][]byte, len(s.Signatures), total)
39+
copy(newSigs, s.Signatures)
40+
s.Signatures = newSigs
41+
}
42+
if cap(s.PublicKeys) < total {
43+
newPKs := make([]PublicKey, len(s.PublicKeys), total)
44+
copy(newPKs, s.PublicKeys)
45+
s.PublicKeys = newPKs
46+
}
47+
if cap(s.Messages) < total {
48+
newMsgs := make([][32]byte, len(s.Messages), total)
49+
copy(newMsgs, s.Messages)
50+
s.Messages = newMsgs
51+
}
52+
if cap(s.Descriptions) < total {
53+
newDescs := make([]string, len(s.Descriptions), total)
54+
copy(newDescs, s.Descriptions)
55+
s.Descriptions = newDescs
56+
}
57+
3558
s.Signatures = append(s.Signatures, set.Signatures...)
3659
s.PublicKeys = append(s.PublicKeys, set.PublicKeys...)
3760
s.Messages = append(s.Messages, set.Messages...)

0 commit comments

Comments
 (0)