diff --git a/prototypes/ScheduledMerges.hs b/prototypes/ScheduledMerges.hs index 8369cb06c..8e33893f1 100644 --- a/prototypes/ScheduledMerges.hs +++ b/prototypes/ScheduledMerges.hs @@ -287,7 +287,8 @@ newMerge tr level mergepolicy mergelast rs = do mergeRunsSize = map Map.size rs } assert (length rs `elem` [4, 5]) $ - MergingRun mergepolicy mergelast <$> newSTRef (OngoingMerge debt rs r) + assert (mergeDebtLeft debt >= cost) $ + MergingRun mergepolicy mergelast <$> newSTRef (OngoingMerge debt rs r) where cost = sum (map Map.size rs) -- How much we need to discharge before the merge can be guaranteed @@ -299,7 +300,7 @@ newMerge tr level mergepolicy mergelast rs = do debt = newMergeDebt $ case mergepolicy of MergePolicyLevelling -> 4 * tieringRunSize (level-1) + levellingRunSize level - MergePolicyTiering -> 4 * tieringRunSize (level-1) + MergePolicyTiering -> length rs * tieringRunSize (level-1) -- deliberately lazy: r = case mergelast of MergeMidLevel -> (mergek rs) @@ -364,6 +365,10 @@ data MergeDebt = newMergeDebt :: Debt -> MergeDebt newMergeDebt d = MergeDebt 0 d +mergeDebtLeft :: MergeDebt -> Int +mergeDebtLeft (MergeDebt c d) = + assert (c < d) $ d - c + -- | As credits are paid, debt is reduced in batches when sufficient credits have accumulated. data MergeDebtPaydown = -- | This remaining merge debt is fully paid off with credits.