Skip to content

Commit

Permalink
Optimize listCreatables per reviewer request. (algorand#1599)
Browse files Browse the repository at this point in the history
Avoid copying the array needlessly, and preallocate storage of array and map
  • Loading branch information
tsachiherman committed Oct 6, 2020
1 parent 965ece0 commit 953277b
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions ledger/acctupdates.go
Expand Up @@ -393,8 +393,8 @@ func (au *accountUpdates) listCreatables(maxCreatableIdx basics.CreatableIndex,
sort.Slice(keys, func(i, j int) bool { return keys[i] > keys[j] })

// Check for creatables that haven't been synced to disk yet.
var unsyncedCreatables []basics.CreatableLocator
deletedCreatables := make(map[basics.CreatableIndex]bool)
unsyncedCreatables := make([]basics.CreatableLocator, 0, len(keys))
deletedCreatables := make(map[basics.CreatableIndex]bool, len(keys))
for _, cidx := range keys {
delta := au.creatables[cidx]
if delta.created {
Expand All @@ -410,18 +410,14 @@ func (au *accountUpdates) listCreatables(maxCreatableIdx basics.CreatableIndex,
}
}

au.accountsMu.RUnlock()

// Check in-memory created creatables, which will always be newer than anything
// in the database
var res []basics.CreatableLocator
for _, loc := range unsyncedCreatables {
if uint64(len(res)) == maxResults {
au.accountsMu.RUnlock()
return res, nil
}
res = append(res, loc)
if uint64(len(unsyncedCreatables)) >= maxResults {
return unsyncedCreatables[:maxResults], nil
}

au.accountsMu.RUnlock()
res := unsyncedCreatables

// Fetch up to maxResults - len(res) + len(deletedCreatables) from the database,
// so we have enough extras in case creatables were deleted
Expand Down

0 comments on commit 953277b

Please sign in to comment.