From eec07aec6825a0468b2af4287fa9cea176893358 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Mon, 23 Sep 2024 15:50:47 -0400 Subject: [PATCH] sim: avoid step looking for new actions every time Once it has a list of actions, it can perform them all. A disappointing optimisation at least in my test case, which it sped up by less than 1 second out of 12. But still it did make it faster. --- Annex/Sim.hs | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/Annex/Sim.hs b/Annex/Sim.hs index a5ce8e2938..eda3fcc1fa 100644 --- a/Annex/Sim.hs +++ b/Annex/Sim.hs @@ -491,22 +491,30 @@ handleStep :: Int -> Int -> SimState SimRepo -> Annex (SimState SimRepo) handleStep startn n st | n > 0 = do let (st', actions) = getactions unsyncactions st - (st'', nothingtodo) <- runoneaction actions st' - if nothingtodo + (st'', restactions) <- runoneaction actions st' + if null restactions then do let (st''', actions') = getactions [ActionSync] st'' - (st'''', stable) <- runoneaction actions' st''' - if stable + (st'''', restactions') <- runoneaction actions' st''' + if null restactions' then do showLongNote $ UnquotedString $ "Simulation has stabilized after " ++ show (startn - n) ++ " steps." return st'''' - else handleStep startn (pred n) st'''' - else handleStep startn (pred n) st'' + else runrest restactions' st'''' (pred n) + else runrest restactions st'' (pred n) | otherwise = return st where + runrest actions st' n' + | n > 0 = do + (st'', restactions) <- runoneaction actions st' + if null restactions + then handleStep startn n' st' + else runrest restactions st'' (pred n') + | otherwise = return st' + unsyncactions = [ ActionGetWanted , ActionSendWanted @@ -530,7 +538,7 @@ handleStep startn n st Right (Right st'') -> getcomponents c st'' as Right (Left (st'', cs)) -> getcomponents (cs:c) st'' as - runoneaction [] st' = return (st', True) + runoneaction [] st' = return (st', []) runoneaction actions st' = do let (idx, st'') = simRandom st' (randomR (0, length actions - 1)) @@ -539,7 +547,7 @@ handleStep startn n st let restactions = take idx actions ++ drop (idx+1) actions action st'' >>= \case (st''', False) -> runoneaction restactions st''' - (st''', True) -> return (st''', False) + (st''', True) -> return (st''', restactions) getSimActionComponents :: SimAction