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:
parent
844491d365
commit
9992a98007
1 changed files with 9 additions and 8 deletions
|
@ -402,19 +402,20 @@ 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]
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
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]
|
account.Balances[prevTXOutput.AssetID] = account.Balances[prevTXOutput.AssetID][:balancesLen-1]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Process the underlying type of the TX.
|
// Process the underlying type of the TX.
|
||||||
switch t := tx.Data.(type) {
|
switch t := tx.Data.(type) {
|
||||||
|
|
Loading…
Reference in a new issue