Skip to content

Commit df102bd

Browse files
committed
[ perf ] Monadize another bottleneck
~5 times faster now
1 parent 020eea7 commit df102bd

2 files changed

Lines changed: 14 additions & 13 deletions

File tree

elab-util-extra/src/Language/Reflection/Unify/Interface.idr

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import Deriving.Show
1111
import Language.Reflection
1212
import Language.Reflection.Expr
1313
import Language.Reflection.Syntax
14+
import Language.Reflection.Logging
1415

1516
%language ElabReflection
1617

@@ -292,14 +293,14 @@ emptyLeaves : (dg : DependencyGraph) -> FinSet dg.freeVars
292293
emptyLeaves dg = intersection dg.empties $ leaves dg
293294

294295
||| List all the free variables without a value in order of dependency
295-
flattenEmpties : (dg : DependencyGraph) -> SnocList $ Fin dg.freeVars
296+
flattenEmpties : Monad m => (dg : DependencyGraph) -> m $ SnocList $ Fin dg.freeVars
296297
flattenEmpties dg = flattenEmpties' dg [<]
297298
where
298-
flattenEmpties' : (dg : DependencyGraph) -> SnocList (Fin dg.freeVars) -> SnocList $ Fin dg.freeVars
299+
flattenEmpties' : (dg : DependencyGraph) -> SnocList (Fin dg.freeVars) -> m $ SnocList $ Fin dg.freeVars
299300
flattenEmpties' dg@(MkDG {freeVars, fvData, fvDeps, empties, nameToId, holeToId}) ctx = do
300-
let els = emptyLeaves dg
301+
els <- pure $ id $ emptyLeaves dg
301302
let False = null els
302-
| _ => ctx
303+
| _ => pure ctx
303304
-- Now els is a non-empty subset of dg.empties
304305
flattenEmpties'
305306
-- `assert_smaller dg` is a workaround for a non-working `assert_smaller empties`
@@ -324,12 +325,12 @@ filterEmpty = foldl myfun []
324325

325326
||| Calculate UnificationResult (var-to-value mappings and empty leaf dependency order)
326327
export
327-
finalizeDG : (task : UnificationTask) -> (dg : DependencyGraph) -> UnificationResult
328+
finalizeDG : Monad m => MonadLog m => (task : UnificationTask) -> (dg : DependencyGraph) -> m UnificationResult
328329
finalizeDG task dg = do
329-
let fvOrder = flattenEmpties dg
330-
let urList = filterEmpty dg.fvData
331-
let (lhsRL, rhsRL) = List.splitAt task.lfv urList
332-
MkUR
330+
fvOrder <- flattenEmpties dg
331+
urList <- pure $ id $ filterEmpty dg.fvData
332+
(lhsRL, rhsRL) <- pure $ id $ List.splitAt task.lfv urList
333+
pure $ MkUR
333334
{ task
334335
, uniDg = dg
335336
, lhsResult = fromList lhsRL

elab-util-extra/src/Language/Reflection/Unify/WithCompiler.idr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,10 @@ solveDG :
216216
(dg : DependencyGraph) ->
217217
m DependencyGraph
218218
solveDG dg = do
219-
let cs = canSub dg
219+
cs <- pure $ id $ canSub dg
220220
let False = null cs
221221
| _ => pure dg
222-
ds <- pure $ doSub dg cs
222+
ds <- pure $ id $ doSub dg cs
223223
-- DG <= DS because cs is non-empty, and every doSub may shrink the set of possibly substitutable variables
224224
-- If doSub can't shrink it, the dependency graph stays the same
225225
if ds == dg
@@ -356,7 +356,7 @@ unifyWithCompiler task = do
356356
let err = pure {f=Elab} $ Left $ Just CatastrophicError
357357
rr <- try ret err
358358
dg <- liftEither rr
359-
ur <- pure $ finalizeDG task dg
359+
ur <- finalizeDG task dg
360360
logPoint DetailedDebug "unifyWithCompiler" [] "Unification result: \{show ur}"
361361
pure ur
362362

@@ -369,7 +369,7 @@ unifyWithCompiler' :
369369
m $ UnificationResult
370370
unifyWithCompiler' task = do
371371
dg <- unify' task
372-
pure $ finalizeDG task dg
372+
finalizeDG task dg
373373

374374
export
375375
[UnifyWithCompiler]

0 commit comments

Comments
 (0)