Skip to content

Commit d017ab3

Browse files
authored
perf: split Core.Context into hot and cold subobjects (#14962)
1 parent 482150a commit d017ab3

1 file changed

Lines changed: 17 additions & 7 deletions

File tree

src/Lean/CoreM.lean

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -213,12 +213,27 @@ structure State where
213213
snapshotTasks : Array (Language.SnapshotTask Language.SnapshotTree) := #[]
214214
deriving Nonempty
215215

216-
/-- Context for the CoreM monad. -/
217-
structure Context where
216+
/--
217+
The pointer-valued fields of `Core.Context` that are updated at most a handful of times per command.
218+
219+
`withReader` can never reuse the `Context` record, so every `withRef`, `withOptions` or
220+
`withIncRecDepth` pays one reference count increment per pointer field. Grouping these fields into a
221+
subobject makes them cost a single increment together.
222+
-/
223+
structure Context.Cold where
218224
/-- Name of the file being compiled. -/
219225
fileName : String
220226
/-- Auxiliary datastructure for converting `String.Pos` into Line/Column number. -/
221227
fileMap : FileMap
228+
quotContext : Name := .anonymous
229+
/-- If set, used to cancel elaboration from outside when results are not needed anymore. -/
230+
cancelTk? : Option IO.CancelToken := none
231+
/-- Cache of `Lean.inheritedTraceOptions`. -/
232+
inheritedTraceOptions : Std.HashSet Name := {}
233+
deriving Nonempty
234+
235+
/-- Context for the CoreM monad. -/
236+
structure Context extends Context.Cold where
222237
options : Options := {}
223238
currRecDepth : Nat := 0
224239
maxRecDepth : Nat := 1000
@@ -227,22 +242,17 @@ structure Context where
227242
openDecls : List OpenDecl := []
228243
initHeartbeats : Nat := 0
229244
maxHeartbeats : Nat := getMaxHeartbeats options
230-
quotContext : Name := .anonymous
231245
currMacroScope : MacroScope := firstFrontendMacroScope
232246
/--
233247
If `diag := true`, different parts of the system collect diagnostics.
234248
Use the `set_option diag true` to set it to true.
235249
-/
236250
diag : Bool := false
237-
/-- If set, used to cancel elaboration from outside when results are not needed anymore. -/
238-
cancelTk? : Option IO.CancelToken := none
239251
/--
240252
If set (when `showPartialSyntaxErrors` is not set and parsing failed), suppresses most elaboration
241253
errors; see also `logMessage` below.
242254
-/
243255
suppressElabErrors : Bool := false
244-
/-- Cache of `Lean.inheritedTraceOptions`. -/
245-
inheritedTraceOptions : Std.HashSet Name := {}
246256
deriving Nonempty
247257

248258
/-- CoreM is a monad for manipulating the Lean environment.

0 commit comments

Comments
 (0)