Skip to content

Commit

Permalink
Sort buffered transactions (#1882)
Browse files Browse the repository at this point in the history
Co-authored-by: Kirill <[email protected]>
Co-authored-by: IronGauntlets <[email protected]>
  • Loading branch information
3 people authored Aug 2, 2024
1 parent 10ba647 commit cdc43f6
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions core/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,11 @@ func (s *State) updateStorageBuffered(contractAddr *felt.Felt, updateDiff map[fe
func (s *State) updateContractStorages(stateTrie *trie.Trie, diffs map[felt.Felt]map[felt.Felt]*felt.Felt,
blockNumber uint64, logChanges bool,
) error {
type bufferedTransactionWithAddress struct {
txn *db.BufferedTransaction
addr *felt.Felt
}

// make sure all noClassContracts are deployed
for addr := range diffs {
if _, ok := noClassContracts[addr]; !ok {
Expand Down Expand Up @@ -400,12 +405,15 @@ func (s *State) updateContractStorages(stateTrie *trie.Trie, diffs map[felt.Felt
})

// update per-contract storage Tries concurrently
contractUpdaters := pool.NewWithResults[*db.BufferedTransaction]().WithErrors().WithMaxGoroutines(runtime.GOMAXPROCS(0))
contractUpdaters := pool.NewWithResults[*bufferedTransactionWithAddress]().WithErrors().WithMaxGoroutines(runtime.GOMAXPROCS(0))
for _, key := range keys {
conractAddr := key
updateDiff := diffs[conractAddr]
contractUpdaters.Go(func() (*db.BufferedTransaction, error) {
return s.updateStorageBuffered(&conractAddr, updateDiff, blockNumber, logChanges)
contractAddr := key
contractUpdaters.Go(func() (*bufferedTransactionWithAddress, error) {
bufferedTxn, err := s.updateStorageBuffered(&contractAddr, diffs[contractAddr], blockNumber, logChanges)
if err != nil {
return nil, err
}
return &bufferedTransactionWithAddress{txn: bufferedTxn, addr: &contractAddr}, nil
})
}

Expand All @@ -414,9 +422,14 @@ func (s *State) updateContractStorages(stateTrie *trie.Trie, diffs map[felt.Felt
return err
}

// we sort bufferedTxns in ascending contract address order to achieve an additional speedup
sort.Slice(bufferedTxns, func(i, j int) bool {
return bufferedTxns[i].addr.Cmp(bufferedTxns[j].addr) < 0
})

// flush buffered txns
for _, bufferedTxn := range bufferedTxns {
if err = bufferedTxn.Flush(); err != nil {
for _, txnWithAddress := range bufferedTxns {
if err := txnWithAddress.txn.Flush(); err != nil {
return err
}
}
Expand Down

0 comments on commit cdc43f6

Please sign in to comment.