Skip to content

Commit

Permalink
Use maps.Clear
Browse files Browse the repository at this point in the history
  • Loading branch information
jannotti committed Jun 18, 2023
1 parent 2229778 commit 592d035
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 36 deletions.
9 changes: 3 additions & 6 deletions ledger/eval/cow.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/algorand/go-algorand/data/transactions"
"github.com/algorand/go-algorand/ledger/ledgercore"
"github.com/algorand/go-algorand/protocol"
"golang.org/x/exp/maps"
)

// ___________________
Expand Down Expand Up @@ -336,13 +337,9 @@ func (cb *roundCowState) reset() {
cb.proto = config.ConsensusParams{}
cb.mods.Reset()
cb.txnCount = 0
for addr := range cb.sdeltas {
delete(cb.sdeltas, addr)
}
maps.Clear(cb.sdeltas)
cb.compatibilityMode = false
for addr := range cb.compatibilityGetKeyCache {
delete(cb.compatibilityGetKeyCache, addr)
}
maps.Clear(cb.compatibilityGetKeyCache)
cb.prevTotals = ledgercore.AccountTotals{}
}

Expand Down
40 changes: 10 additions & 30 deletions ledger/ledgercore/statedelta.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,38 +295,24 @@ func (ad *AccountDeltas) Dehydrate() {
if ad.acctsCache == nil {
ad.acctsCache = make(map[basics.Address]int)
}
for key := range ad.acctsCache {
delete(ad.acctsCache, key)
}
maps.Clear(ad.acctsCache)
if ad.appResourcesCache == nil {
ad.appResourcesCache = make(map[AccountApp]int)
}
for key := range ad.appResourcesCache {
delete(ad.appResourcesCache, key)
}
maps.Clear(ad.appResourcesCache)
if ad.assetResourcesCache == nil {
ad.assetResourcesCache = make(map[AccountAsset]int)
}
for key := range ad.assetResourcesCache {
delete(ad.assetResourcesCache, key)
}
maps.Clear(ad.assetResourcesCache)
}

// Reset resets the StateDelta for re-use with sync.Pool
func (sd *StateDelta) Reset() {
sd.Accts.reset()
for txid := range sd.Txids {
delete(sd.Txids, txid)
}
for txLease := range sd.Txleases {
delete(sd.Txleases, txLease)
}
for creatableIndex := range sd.Creatables {
delete(sd.Creatables, creatableIndex)
}
for key := range sd.KvMods {
delete(sd.KvMods, key)
}
maps.Clear(sd.Txids)
maps.Clear(sd.Txleases)
maps.Clear(sd.Creatables)
maps.Clear(sd.KvMods)
sd.Totals = AccountTotals{}

// these fields are going to be populated on next use but resetting them anyway for safety.
Expand All @@ -344,15 +330,9 @@ func (ad *AccountDeltas) reset() {
ad.AssetResources = ad.AssetResources[:0]

// reset the maps
for address := range ad.acctsCache {
delete(ad.acctsCache, address)
}
for aApp := range ad.appResourcesCache {
delete(ad.appResourcesCache, aApp)
}
for aAsset := range ad.assetResourcesCache {
delete(ad.assetResourcesCache, aAsset)
}
maps.Clear(ad.acctsCache)
maps.Clear(ad.appResourcesCache)
maps.Clear(ad.assetResourcesCache)
}

// notAllocated returns true if any of the fields allocated by MakeAccountDeltas is nil
Expand Down

0 comments on commit 592d035

Please sign in to comment.