Skip to content

Commit

Permalink
perf: improve avoidance of repeated Expr visits in unused variables l…
Browse files Browse the repository at this point in the history
…inter (leanprover#3076)

-43% linter run time in a big proof case
  • Loading branch information
Kha authored Dec 18, 2023
1 parent 78816a3 commit 3335b2a
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/Lean/Linter/UnusedVariables.lean
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,17 @@ def unusedVariables : Linter where
| _ =>
assignments)

-- collect fvars from mvar assignments
let tacticFVarUses : HashSet FVarId ←
tacticMVarAssignments.foldM (init := .empty) fun uses _ expr => do
let (_, s) ← StateT.run (s := uses) <| expr.forEach fun e => do if e.isFVar then modify (·.insert e.fvarId!)
return s
Elab.Command.liftIO <| -- no need to carry around other state here
StateT.run' (s := HashSet.empty) do
-- use one big cache for all `ForEachExpr.visit` invocations
MonadCacheT.run do
tacticMVarAssignments.forM fun _ e =>
ForEachExpr.visit (e := e) fun e => do
if e.isFVar then modify (·.insert e.fvarId!)
return e.hasFVar
get

-- collect ignore functions
let ignoreFns := (← getUnusedVariablesIgnoreFns)
Expand Down

0 comments on commit 3335b2a

Please sign in to comment.