Skip to content

Commit

Permalink
Try scratch slice
Browse files Browse the repository at this point in the history
  • Loading branch information
ValarDragon authored and czarcas7ic committed Dec 29, 2023
1 parent 68270ab commit ebc35fb
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
7 changes: 4 additions & 3 deletions x/incentives/keeper/distribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ func (k Keeper) handleGroupPostDistribute(ctx sdk.Context, groupGauge types.Gaug
}

// getDistributeToBaseLocks takes a gauge along with cached period locks by denom and returns locks that must be distributed to
func (k Keeper) getDistributeToBaseLocks(ctx sdk.Context, gauge types.Gauge, cache map[string][]lockuptypes.PeriodLock) []*lockuptypes.PeriodLock {
func (k Keeper) getDistributeToBaseLocks(ctx sdk.Context, gauge types.Gauge, cache map[string][]lockuptypes.PeriodLock, scratchSlice *[]*lockuptypes.PeriodLock) []*lockuptypes.PeriodLock {
// if gauge is empty, don't get the locks
if gauge.Coins.Empty() {
return []*lockuptypes.PeriodLock{}
Expand All @@ -788,7 +788,7 @@ func (k Keeper) getDistributeToBaseLocks(ctx sdk.Context, gauge types.Gauge, cac
// get this from memory instead of hitting iterators / underlying stores.
// due to many details of cacheKVStore, iteration will still cause expensive IAVL reads.
allLocks := cache[distributeBaseDenom]
return FilterLocksByMinDuration(allLocks, gauge.DistributeTo.Duration)
return FilterLocksByMinDuration(allLocks, gauge.DistributeTo.Duration, scratchSlice)
}

// Distribute distributes coins from an array of gauges to all eligible locks and pools in the case of "NoLock" gauges.
Expand All @@ -799,10 +799,11 @@ func (k Keeper) Distribute(ctx sdk.Context, gauges []types.Gauge) (sdk.Coins, er

locksByDenomCache := make(map[string][]lockuptypes.PeriodLock)
totalDistributedCoins := sdk.NewCoins()
scratchSlice := make([]*lockuptypes.PeriodLock, 0, 10000)

for _, gauge := range gauges {
var gaugeDistributedCoins sdk.Coins
filteredLocks := k.getDistributeToBaseLocks(ctx, gauge, locksByDenomCache)
filteredLocks := k.getDistributeToBaseLocks(ctx, gauge, locksByDenomCache, &scratchSlice)
// send based on synthetic lockup coins if it's distributing to synthetic lockups
var err error
if lockuptypes.IsSyntheticDenom(gauge.DistributeTo.Denom) {
Expand Down
5 changes: 3 additions & 2 deletions x/incentives/keeper/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ func (k Keeper) FinishedGaugesIterator(ctx sdk.Context) sdk.Iterator {
}

// FilterLocksByMinDuration returns locks whose lock duration is greater than the provided minimum duration.
func FilterLocksByMinDuration(locks []lockuptypes.PeriodLock, minDuration time.Duration) []*lockuptypes.PeriodLock {
filteredLocks := make([]*lockuptypes.PeriodLock, 0, len(locks))
func FilterLocksByMinDuration(locks []lockuptypes.PeriodLock, minDuration time.Duration, scratchSlice *[]*lockuptypes.PeriodLock) []*lockuptypes.PeriodLock {
*scratchSlice = (*scratchSlice)[:0]
filteredLocks := *scratchSlice
for i := range locks {
if locks[i].Duration >= minDuration {
filteredLocks = append(filteredLocks, &locks[i])
Expand Down
3 changes: 2 additions & 1 deletion x/incentives/keeper/iterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ func TestFilterLocksByMinDuration(t *testing.T) {
}
}

filteredLocks := keeper.FilterLocksByMinDuration(locks, minDuration)
scratchSlice := []*lockuptypes.PeriodLock{}
filteredLocks := keeper.FilterLocksByMinDuration(locks, minDuration, &scratchSlice)

require.Equal(t, len(locks), len(filteredLocks))

Expand Down

0 comments on commit ebc35fb

Please sign in to comment.