Skip to content

Commit

Permalink
further optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
AdoAdoAdo committed Apr 25, 2024
1 parent 1559ccf commit 7386e5c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 32 deletions.
27 changes: 18 additions & 9 deletions process/block/postprocess/basePostProcess.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
}
}
22 changes: 13 additions & 9 deletions process/block/postprocess/intermediateResults.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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))
}
Expand Down
15 changes: 8 additions & 7 deletions process/block/postprocess/intermediateResults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down Expand Up @@ -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()
}

Expand Down Expand Up @@ -954,15 +955,15 @@ 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)
assert.Equal(t, tx, scrInfo.tx)
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])
Expand Down
18 changes: 11 additions & 7 deletions process/block/postprocess/oneMBPostProcessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{
Expand Down

0 comments on commit 7386e5c

Please sign in to comment.