From d5a17c2285800ccf7be826b9fbf0872d17110487 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Gamenik?= Date: Tue, 24 Jun 2025 17:00:14 +0200 Subject: [PATCH] capacity at append in for, where it van precalculated --- core/blockchain.go | 4 ++-- core/rawdb/ancient_utils.go | 3 ++- core/rawdb/chain_iterator.go | 2 +- core/rawdb/freezer_table.go | 2 +- core/txpool/blobpool/blobpool.go | 10 +++++----- core/vm/contracts.go | 9 +++++---- core/vm/eips.go | 2 +- core/vm/interpreter.go | 3 ++- 8 files changed, 19 insertions(+), 16 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index 3bb681d7c3..aee4ceb35b 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -1352,7 +1352,7 @@ const ( // will be directly stored in the ancient, getting rid of the chain migration. func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain []rlp.RawValue, ancientLimit uint64) (int, error) { // Verify the supplied headers before insertion without lock - var headers []*types.Header + headers := make([]*types.Header, 0, len(blockChain)) for _, block := range blockChain { headers = append(headers, block.Header()) // Here we also validate that blob transactions in the block do not @@ -2210,7 +2210,7 @@ func (bc *BlockChain) insertSideChain(block *types.Block, it *insertIterator, ma } // Import all the pruned blocks to make the state available var ( - blocks []*types.Block + blocks = make([]*types.Block, 0, len(hashes)) memory uint64 ) for i := len(hashes) - 1; i >= 0; i-- { diff --git a/core/rawdb/ancient_utils.go b/core/rawdb/ancient_utils.go index f4909d86e7..e474d1a212 100644 --- a/core/rawdb/ancient_utils.go +++ b/core/rawdb/ancient_utils.go @@ -53,6 +53,7 @@ func (info *freezerInfo) size() common.StorageSize { func inspect(name string, order map[string]freezerTableConfig, reader ethdb.AncientReader) (freezerInfo, error) { info := freezerInfo{name: name} + info.sizes = make([]tableSize, 0, len(order)) for t := range order { size, err := reader.AncientSize(t) if err != nil { @@ -78,7 +79,7 @@ func inspect(name string, order map[string]freezerTableConfig, reader ethdb.Anci // inspectFreezers inspects all freezers registered in the system. func inspectFreezers(db ethdb.Database) ([]freezerInfo, error) { - var infos []freezerInfo + var infos []freezerInfo = make([]freezerInfo, 0, len(freezers)) for _, freezer := range freezers { switch freezer { case ChainFreezerName: diff --git a/core/rawdb/chain_iterator.go b/core/rawdb/chain_iterator.go index ecbc44e1f1..e7594657fb 100644 --- a/core/rawdb/chain_iterator.go +++ b/core/rawdb/chain_iterator.go @@ -148,7 +148,7 @@ func iterateTransactions(db ethdb.Database, from uint64, to uint64, reverse bool log.Warn("Failed to decode block body", "block", data.number, "error", err) return } - var hashes []common.Hash + var hashes []common.Hash = make([]common.Hash, 0, len(body.Transactions)) for _, tx := range body.Transactions { hashes = append(hashes, tx.Hash()) } diff --git a/core/rawdb/freezer_table.go b/core/rawdb/freezer_table.go index 3900b5d558..dd31c1f978 100644 --- a/core/rawdb/freezer_table.go +++ b/core/rawdb/freezer_table.go @@ -936,7 +936,7 @@ func (t *freezerTable) getIndices(from, count uint64) ([]*indexEntry, error) { return nil, err } var ( - indices []*indexEntry + indices = make([]*indexEntry, 0, count+1) offset int ) for i := from; i <= from+count; i++ { diff --git a/core/txpool/blobpool/blobpool.go b/core/txpool/blobpool/blobpool.go index 99f7230786..3196ef766f 100644 --- a/core/txpool/blobpool/blobpool.go +++ b/core/txpool/blobpool/blobpool.go @@ -521,7 +521,7 @@ func (p *BlobPool) parseTransaction(id uint64, size uint32, blob []byte) error { } // recheck verifies the pool's content for a specific account and drops anything -// that does not fit anymore (dangling or filled nonce, overdraft). +// that does not fit anymore (dangling or filled nonce, overdraft). // TODO split long method in smaller parts func (p *BlobPool) recheck(addr common.Address, inclusions map[common.Hash]uint64) { // Sort the transactions belonging to the account so reinjects can be simpler txs := p.index[addr] @@ -541,8 +541,8 @@ func (p *BlobPool) recheck(addr common.Address, inclusions map[common.Hash]uint6 ) if gapped || filled { var ( - ids []uint64 - nonces []uint64 + ids []uint64 = make([]uint64, 0, len(txs)) + nonces []uint64 = make([]uint64, 0, len(txs)) ) for i := 0; i < len(txs); i++ { ids = append(ids, txs[i].id) @@ -659,8 +659,8 @@ func (p *BlobPool) recheck(addr common.Address, inclusions map[common.Hash]uint6 } // Otherwise if there's a nonce gap evict all later transactions var ( - ids []uint64 - nonces []uint64 + ids []uint64 = make([]uint64, 0, len(txs)) + nonces []uint64 = make([]uint64, 0, len(txs)) ) for j := i; j < len(txs); j++ { ids = append(ids, txs[j].id) diff --git a/core/vm/contracts.go b/core/vm/contracts.go index 92a4e7d016..2e561bbce8 100644 --- a/core/vm/contracts.go +++ b/core/vm/contracts.go @@ -645,9 +645,10 @@ func runBn256Pairing(input []byte) ([]byte, error) { return nil, errBadPairingInput } // Convert the input into a set of coordinates + howMuch := len(input) / 192 var ( - cs []*bn256.G1 - ts []*bn256.G2 + cs []*bn256.G1 = make([]*bn256.G1, 0, howMuch) + ts []*bn256.G2 = make([]*bn256.G2, 0, howMuch) ) for i := 0; i < len(input); i += 192 { c, err := newCurvePoint(input[i : i+64]) @@ -976,8 +977,8 @@ func (c *bls12381Pairing) Run(input []byte) ([]byte, error) { } var ( - p []bls12381.G1Affine - q []bls12381.G2Affine + p []bls12381.G1Affine = make([]bls12381.G1Affine, 0, k) + q []bls12381.G2Affine = make([]bls12381.G2Affine, 0, k) ) // Decode pairs diff --git a/core/vm/eips.go b/core/vm/eips.go index 79fd24d13e..7e40118df5 100644 --- a/core/vm/eips.go +++ b/core/vm/eips.go @@ -60,7 +60,7 @@ func ValidEip(eipNum int) bool { return ok } func ActivateableEips() []string { - var nums []string + nums := make([]string, 0, len(activators)) for k := range activators { nums = append(nums, fmt.Sprintf("%d", k)) } diff --git a/core/vm/interpreter.go b/core/vm/interpreter.go index d0e5967e6e..090d829a9d 100644 --- a/core/vm/interpreter.go +++ b/core/vm/interpreter.go @@ -136,11 +136,12 @@ func NewEVMInterpreter(evm *EVM) *EVMInterpreter { default: table = &frontierInstructionSet } - var extraEips []int + extraEips := make([]int, 0, len(evm.Config.ExtraEips)) if len(evm.Config.ExtraEips) > 0 { // Deep-copy jumptable to prevent modification of opcodes in other tables table = copyJumpTable(table) } + for _, eip := range evm.Config.ExtraEips { if err := EnableEIP(eip, table); err != nil { // Disable it, so caller can check if it's activated or not