From 9992a980076351e70fd094cd995243ebd6181826 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Fri, 6 Dec 2019 18:44:28 +0300 Subject: [PATCH] core: optimize balance utxo removal One of my samples had 8 (out of 30) seconds spent here, but values of 100ms are more typical. After this change it becomes invisible. --- pkg/core/blockchain.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/pkg/core/blockchain.go b/pkg/core/blockchain.go index 87d219125..bb84c083e 100644 --- a/pkg/core/blockchain.go +++ b/pkg/core/blockchain.go @@ -402,16 +402,17 @@ func (bc *Blockchain) storeBlock(block *Block) error { if balancesLen <= 1 { delete(account.Balances, prevTXOutput.AssetID) } else { - var gotTx bool - for index, balance := range account.Balances[prevTXOutput.AssetID] { - if !gotTx && balance.Tx.Equals(input.PrevHash) && balance.Index == input.PrevIndex { - gotTx = true - } - if gotTx && index+1 < balancesLen { - account.Balances[prevTXOutput.AssetID][index] = account.Balances[prevTXOutput.AssetID][index+1] + var index = -1 + for i, balance := range account.Balances[prevTXOutput.AssetID] { + if balance.Tx.Equals(input.PrevHash) && balance.Index == input.PrevIndex { + index = i + break } } - account.Balances[prevTXOutput.AssetID] = account.Balances[prevTXOutput.AssetID][:balancesLen-1] + if index >= 0 { + copy(account.Balances[prevTXOutput.AssetID][index:], account.Balances[prevTXOutput.AssetID][index+1:]) + account.Balances[prevTXOutput.AssetID] = account.Balances[prevTXOutput.AssetID][:balancesLen-1] + } } } }