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.
This commit is contained in:
Roman Khimov 2019-12-06 18:44:28 +03:00
parent 844491d365
commit 9992a98007

View file

@ -402,16 +402,17 @@ func (bc *Blockchain) storeBlock(block *Block) error {
if balancesLen <= 1 { if balancesLen <= 1 {
delete(account.Balances, prevTXOutput.AssetID) delete(account.Balances, prevTXOutput.AssetID)
} else { } else {
var gotTx bool var index = -1
for index, balance := range account.Balances[prevTXOutput.AssetID] { for i, balance := range account.Balances[prevTXOutput.AssetID] {
if !gotTx && balance.Tx.Equals(input.PrevHash) && balance.Index == input.PrevIndex { if balance.Tx.Equals(input.PrevHash) && balance.Index == input.PrevIndex {
gotTx = true index = i
} break
if gotTx && index+1 < balancesLen {
account.Balances[prevTXOutput.AssetID][index] = account.Balances[prevTXOutput.AssetID][index+1]
} }
} }
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]
}
} }
} }
} }