Skip to content

Commit

Permalink
perf: header linter performs fewer checks (#19260)
Browse files Browse the repository at this point in the history
  • Loading branch information
adomani committed Nov 22, 2024
1 parent be959a9 commit e03117d
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions Mathlib/Tactic/Linter/Header.lean
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,15 @@ def isInMathlib (modName : Name) : IO Bool := do
return (ml.map (·.module == modName)).any (·)
else return false

/-- `inMathlibRef` is
* `none` at initialization time;
* `some true` if the `header` linter has already discovered that the current file
is imported in `Mathlib.lean`;
* `some false` if the `header` linter has already discovered that the current file
is *not* imported in `Mathlib.lean`.
-/
initialize inMathlibRef : IO.Ref (Option Bool) ← IO.mkRef none

/--
The "header" style linter checks that a file starts with
```
Expand Down Expand Up @@ -290,9 +299,17 @@ def duplicateImportsCheck (imports : Array Syntax) : CommandElabM Unit := do
@[inherit_doc Mathlib.Linter.linter.style.header]
def headerLinter : Linter where run := withSetOptionIn fun stx ↦ do
let mainModule ← getMainModule
let inMathlib? := ← match ← inMathlibRef.get with
| some d => return d
| none => do
let val ← isInMathlib mainModule
-- We store the answer to the question "is this file in `Mathlib.lean`?" in `inMathlibRef`
-- to avoid recomputing its value on every command. This is a performance optimisation.
inMathlibRef.set (some val)
return val
-- The linter skips files not imported in `Mathlib.lean`, to avoid linting "scratch files".
-- However, it is active in the test file `MathlibTest.Header` for the linter itself.
unless (← isInMathlib mainModule) || mainModule == `MathlibTest.Header do return
-- It is however active in the test file `MathlibTest.Header` for the linter itself.
unless inMathlib? || mainModule == `MathlibTest.Header do return
unless Linter.getLinterValue linter.style.header (← getOptions) do
return
if (← get).messages.hasErrors then
Expand Down

0 comments on commit e03117d

Please sign in to comment.