Skip to content

Commit 7723d00

Browse files
committed
perf(Tactic/StacksAttribute): avoid initialization cost (leanprover-community#34679)
This PR improves the performace by removing the initialization cost of the `stacks` attribute. Previously, a `HashSet` of stacks tags was being created upon initialization of any file. See [#mathlib4 > Performance cost of environment extensions](https://leanprover.zulipchat.com/#narrow/channel/287929-mathlib4/topic/Performance.20cost.20of.20environment.20extensions/with/571319366)
1 parent 6a1be36 commit 7723d00

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

Mathlib/Tactic/StacksAttribute.lean

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ structure Tag where
4444
comment : String
4545
deriving BEq, Hashable
4646

47-
/-- Defines the `tagExt` extension for adding a `HashSet` of `Tag`s
48-
to the environment. -/
49-
initialize tagExt : SimplePersistentEnvExtension Tag (Std.HashSet Tag) ←
47+
/-- Defines the `tagExt` extension for storing all `Tag`s in the environment.
48+
`addImportedFn` is a constant function to avoid a performance overhead during initialization. -/
49+
initialize tagExt : SimplePersistentEnvExtension Tag (Array (Array Tag)) ←
5050
registerSimplePersistentEnvExtension {
51-
addImportedFn := fun as => as.foldl Std.HashSet.insertMany {}
52-
addEntryFn := .insert
51+
addImportedFn tags := tags
52+
addEntryFn tags _ := tags
5353
}
5454

5555
/--
@@ -171,7 +171,8 @@ end Mathlib.StacksTag
171171
`getSortedStackProjectTags env` returns the array of `Tags`, sorted by alphabetical order of tag.
172172
-/
173173
private def Lean.Environment.getSortedStackProjectTags (env : Environment) : Array Tag :=
174-
tagExt.getState env |>.toArray.qsort (·.tag < ·.tag)
174+
let tags := PersistentEnvExtension.getState tagExt env
175+
tags.2.flatten.appendList tags.1 |>.qsort (·.tag < ·.tag)
175176

176177
/--
177178
`getSortedStackProjectDeclNames env tag` returns the array of declaration names of results

0 commit comments

Comments
 (0)