From 7386e5cfcae833389cea4198a8d7aa905cb554db Mon Sep 17 00:00:00 2001 From: Adrian Dobrita Date: Thu, 25 Apr 2024 15:46:04 +0300 Subject: [PATCH] further optimizations --- process/block/postprocess/basePostProcess.go | 27 ++++++++++++------- .../block/postprocess/intermediateResults.go | 22 ++++++++------- .../postprocess/intermediateResults_test.go | 15 ++++++----- .../block/postprocess/oneMBPostProcessor.go | 18 ++++++++----- 4 files changed, 50 insertions(+), 32 deletions(-) diff --git a/process/block/postprocess/basePostProcess.go b/process/block/postprocess/basePostProcess.go index 5991702c913..3b026896290 100644 --- a/process/block/postprocess/basePostProcess.go +++ b/process/block/postprocess/basePostProcess.go @@ -29,8 +29,16 @@ type txInfo struct { *txShardInfo } +// TODO: maybe give the capacity as argument to InitProcessedResults +const defaultCapacity = 10000 + var log = logger.GetOrCreate("process/block/postprocess") +type processedResult struct { + mapProcessedResult map[string][][]byte + keys []string +} + type basePostProcessor struct { hasher hashing.Hasher marshalizer marshal.Marshalizer @@ -40,7 +48,7 @@ type basePostProcessor struct { mutInterResultsForBlock sync.Mutex interResultsForBlock map[string]*txInfo - mapProcessedResult map[string][][]byte + processedResult processedResult intraShardMiniBlock *block.MiniBlock economicsFee process.FeeHandler index uint32 @@ -79,7 +87,8 @@ func (bpp *basePostProcessor) CreateBlockStarted() { bpp.mutInterResultsForBlock.Lock() bpp.interResultsForBlock = make(map[string]*txInfo) bpp.intraShardMiniBlock = nil - bpp.mapProcessedResult = make(map[string][][]byte) + bpp.processedResult.mapProcessedResult = make(map[string][][]byte) + bpp.processedResult.keys = make([]string, 0) bpp.index = 0 bpp.mutInterResultsForBlock.Unlock() } @@ -171,7 +180,7 @@ func (bpp *basePostProcessor) RemoveProcessedResults(key []byte) [][]byte { bpp.mutInterResultsForBlock.Lock() defer bpp.mutInterResultsForBlock.Unlock() - txHashes, ok := bpp.mapProcessedResult[string(key)] + txHashes, ok := bpp.processedResult.mapProcessedResult[string(key)] if !ok { return nil } @@ -183,15 +192,13 @@ func (bpp *basePostProcessor) RemoveProcessedResults(key []byte) [][]byte { return txHashes } -// TODO: maybe give the capacity as argument to InitProcessedResults -const defaultCapacity = 10000 - // InitProcessedResults will initialize the processed results func (bpp *basePostProcessor) InitProcessedResults(key []byte) { bpp.mutInterResultsForBlock.Lock() defer bpp.mutInterResultsForBlock.Unlock() - bpp.mapProcessedResult[string(key)] = make([][]byte, 0, defaultCapacity) + bpp.processedResult.mapProcessedResult[string(key)] = make([][]byte, 0, defaultCapacity) + bpp.processedResult.keys = make([]string, 0, defaultCapacity) } func (bpp *basePostProcessor) splitMiniBlocksIfNeeded(miniBlocks []*block.MiniBlock) []*block.MiniBlock { @@ -285,7 +292,9 @@ func (bpp *basePostProcessor) addIntermediateTxToResultsForBlock( bpp.index++ bpp.interResultsForBlock[string(txHash)] = scrInfo - for key := range bpp.mapProcessedResult { - bpp.mapProcessedResult[key] = append(bpp.mapProcessedResult[key], txHash) + var mapValue [][]byte + for _, key := range bpp.processedResult.keys { + mapValue = bpp.processedResult.mapProcessedResult[key] + bpp.processedResult.mapProcessedResult[key] = append(mapValue, txHash) } } diff --git a/process/block/postprocess/intermediateResults.go b/process/block/postprocess/intermediateResults.go index 6b9020ca29d..63358f62cf8 100644 --- a/process/block/postprocess/intermediateResults.go +++ b/process/block/postprocess/intermediateResults.go @@ -12,11 +12,12 @@ import ( "github.com/multiversx/mx-chain-core-go/data/smartContractResult" "github.com/multiversx/mx-chain-core-go/hashing" "github.com/multiversx/mx-chain-core-go/marshal" + logger "github.com/multiversx/mx-chain-logger-go" + "github.com/multiversx/mx-chain-go/common" "github.com/multiversx/mx-chain-go/dataRetriever" "github.com/multiversx/mx-chain-go/process" "github.com/multiversx/mx-chain-go/sharding" - logger "github.com/multiversx/mx-chain-logger-go" ) var _ process.IntermediateTransactionHandler = (*intermediateResultsProcessor)(nil) @@ -78,13 +79,16 @@ func NewIntermediateResultsProcessor( } base := &basePostProcessor{ - hasher: args.Hasher, - marshalizer: args.Marshalizer, - shardCoordinator: args.Coordinator, - store: args.Store, - storageType: dataRetriever.UnsignedTransactionUnit, - mapProcessedResult: make(map[string][][]byte), - economicsFee: args.EconomicsFee, + hasher: args.Hasher, + marshalizer: args.Marshalizer, + shardCoordinator: args.Coordinator, + store: args.Store, + storageType: dataRetriever.UnsignedTransactionUnit, + processedResult: processedResult{ + mapProcessedResult: make(map[string][][]byte), + keys: make([]string, 0, defaultCapacity), + }, + economicsFee: args.EconomicsFee, } irp := &intermediateResultsProcessor{ @@ -255,7 +259,7 @@ func (irp *intermediateResultsProcessor) AddIntermediateTransactions(txs []data. } if log.GetLevel() == logger.LogTrace { - //spew.Sdump is very useful when debugging errors like `receipts hash mismatch` + // spew.Sdump is very useful when debugging errors like `receipts hash mismatch` log.Trace("scr added", "txHash", addScr.PrevTxHash, "hash", scrHash, "nonce", addScr.Nonce, "gasLimit", addScr.GasLimit, "value", addScr.Value, "dump", spew.Sdump(addScr)) } diff --git a/process/block/postprocess/intermediateResults_test.go b/process/block/postprocess/intermediateResults_test.go index 4213349ee6d..24f1a2e791b 100644 --- a/process/block/postprocess/intermediateResults_test.go +++ b/process/block/postprocess/intermediateResults_test.go @@ -12,6 +12,9 @@ import ( "github.com/multiversx/mx-chain-core-go/data/block" "github.com/multiversx/mx-chain-core-go/data/smartContractResult" "github.com/multiversx/mx-chain-core-go/data/transaction" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/multiversx/mx-chain-go/dataRetriever" "github.com/multiversx/mx-chain-go/process" "github.com/multiversx/mx-chain-go/process/mock" @@ -21,8 +24,6 @@ import ( "github.com/multiversx/mx-chain-go/testscommon/enableEpochsHandlerMock" "github.com/multiversx/mx-chain-go/testscommon/hashingMocks" "github.com/multiversx/mx-chain-go/testscommon/storage" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" ) const maxGasLimitPerBlock = uint64(1500000000) @@ -387,19 +388,19 @@ func TestIntermediateResultsProcessor_AddIntermediateTransactionsAddAndRevert(t err = irp.AddIntermediateTransactions(txs) assert.Nil(t, err) irp.mutInterResultsForBlock.Lock() - assert.Equal(t, len(irp.mapProcessedResult[string(key)]), len(txs)) + assert.Equal(t, len(irp.processedResult.mapProcessedResult[string(key)]), len(txs)) assert.Equal(t, len(txs), calledCount) irp.mutInterResultsForBlock.Unlock() irp.RemoveProcessedResults(key) irp.mutInterResultsForBlock.Lock() assert.Equal(t, len(irp.interResultsForBlock), 0) - assert.Equal(t, len(irp.mapProcessedResult[string(key)]), len(txs)) + assert.Equal(t, len(irp.processedResult.mapProcessedResult[string(key)]), len(txs)) irp.mutInterResultsForBlock.Unlock() irp.InitProcessedResults(key) irp.mutInterResultsForBlock.Lock() - assert.Equal(t, len(irp.mapProcessedResult[string(key)]), 0) + assert.Equal(t, len(irp.processedResult.mapProcessedResult[string(key)]), 0) irp.mutInterResultsForBlock.Unlock() } @@ -954,7 +955,7 @@ func TestIntermediateResultsProcessor_addIntermediateTxToResultsForBlock(t *test irp.addIntermediateTxToResultsForBlock(tx, txHash, sndShardID, rcvShardID) require.Equal(t, 1, len(irp.interResultsForBlock)) - require.Equal(t, 1, len(irp.mapProcessedResult)) + require.Equal(t, 1, len(irp.processedResult.mapProcessedResult)) scrInfo, ok := irp.interResultsForBlock[string(txHash)] require.True(t, ok) @@ -962,7 +963,7 @@ func TestIntermediateResultsProcessor_addIntermediateTxToResultsForBlock(t *test assert.Equal(t, sndShardID, scrInfo.senderShardID) assert.Equal(t, rcvShardID, scrInfo.receiverShardID) - intermediateResultsHashes, ok := irp.mapProcessedResult[string(key)] + intermediateResultsHashes, ok := irp.processedResult.mapProcessedResult[string(key)] require.True(t, ok) require.Equal(t, 1, len(intermediateResultsHashes)) assert.Equal(t, txHash, intermediateResultsHashes[0]) diff --git a/process/block/postprocess/oneMBPostProcessor.go b/process/block/postprocess/oneMBPostProcessor.go index 5c68c3b194b..3cd2a071fa0 100644 --- a/process/block/postprocess/oneMBPostProcessor.go +++ b/process/block/postprocess/oneMBPostProcessor.go @@ -10,6 +10,7 @@ import ( "github.com/multiversx/mx-chain-core-go/data/block" "github.com/multiversx/mx-chain-core-go/hashing" "github.com/multiversx/mx-chain-core-go/marshal" + "github.com/multiversx/mx-chain-go/dataRetriever" "github.com/multiversx/mx-chain-go/process" "github.com/multiversx/mx-chain-go/sharding" @@ -49,13 +50,16 @@ func NewOneMiniBlockPostProcessor( } base := &basePostProcessor{ - hasher: hasher, - marshalizer: marshalizer, - shardCoordinator: coordinator, - store: store, - storageType: storageType, - mapProcessedResult: make(map[string][][]byte), - economicsFee: economicsFee, + hasher: hasher, + marshalizer: marshalizer, + shardCoordinator: coordinator, + store: store, + storageType: storageType, + processedResult: processedResult{ + mapProcessedResult: make(map[string][][]byte), + keys: make([]string, 0, defaultCapacity), + }, + economicsFee: economicsFee, } opp := &oneMBPostProcessor{