Skip to content

Commit a96eba0

Browse files
keep cache and constraint store after recursion, revert state on exceptions (#3929)
Previously we were discarding any simplifications cached during recursive simplification. This is overly cautious - while the change flag and the term stack (for iteration count and loop detection) should be restored, the cache remains valid (we may only _gain_ new path conditions, and only in rare cases where equations have an `ensures` clause), and continues to be useful when the same or a similar recursive evaluation is needed. This change sped up a particular request extracted from an `mx-backend` proof from 5 minutes to 5 seconds. --------- Co-authored-by: rv-jenkins <[email protected]>
1 parent 50f4a48 commit a96eba0

File tree

4 files changed

+15
-38
lines changed

4 files changed

+15
-38
lines changed

booster/library/Booster/Pattern/ApplyEquations.hs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,15 @@ throw = EquationT . lift . throwE
8484
catch_ ::
8585
Monad io => EquationT io a -> (EquationFailure -> EquationT io a) -> EquationT io a
8686
catch_ (EquationT op) hdlr = EquationT $ do
87+
prior <- lift . lift $ get
8788
cfg <- ask
88-
lift (runReaderT op cfg `catchE` (\e -> let EquationT fallBack = hdlr e in runReaderT fallBack cfg))
89+
lift
90+
( runReaderT op cfg
91+
`catchE` ( \e -> do
92+
let EquationT fallBack = hdlr e
93+
runReaderT (lift (lift (put prior)) >> fallBack) cfg
94+
)
95+
)
8996

9097
data EquationFailure
9198
= IndexIsNone Term
@@ -965,5 +972,7 @@ simplifyConstraint' recurseIntoEvalBool = \case
965972
prior <- getState -- save prior state so we can revert
966973
eqState $ put prior{termStack = mempty, changed = False}
967974
result <- iterateEquations BottomUp PreferFunctions t
968-
eqState $ put prior
975+
-- reset change flag and term stack to prior values
976+
-- (keep the updated cache and added predicates, if any)
977+
eqState $ modify $ \s -> s{changed = prior.changed, termStack = prior.termStack}
969978
pure result

0 commit comments

Comments
 (0)