sim: avoid step looking for new actions every time
authorJoey Hess <joeyh@joeyh.name>
Mon, 23 Sep 2024 19:50:47 +0000 (15:50 -0400)
committerJoey Hess <joeyh@joeyh.name>
Mon, 23 Sep 2024 19:50:47 +0000 (15:50 -0400)
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

index a5ce8e293802b534e87603069f2420247dd4f2f8..eda3fcc1fa9183b7d705dfb625703e1b2951bc07 100644 (file)
@@ -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