Skip to content

Commit 0bfb4b6

Browse files
committed
chore: inline trace nodes
This extracts a `postCallback` helper so that only the actual callback is inlined.
1 parent 470498c commit 0bfb4b6

File tree

1 file changed

+39
-28
lines changed

1 file changed

+39
-28
lines changed

src/Lean/Util/Trace.lean

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -261,29 +261,35 @@ withTraceNode `isPosTrace (msg := (return m!"{ExceptToEmoji.toEmoji ·} checking
261261
262262
The `cls`, `collapsed`, and `tag` arguments are forwarded to the constructor of `TraceData`.
263263
-/
264+
@[inline]
264265
def withTraceNode [always : MonadAlwaysExcept ε m] [MonadLiftT BaseIO m] (cls : Name)
265266
(msg : Except ε α → m MessageData) (k : m α) (collapsed := true) (tag := "") : m α := do
266-
let _ := always.except
267267
let opts ← getOptions
268268
let clsEnabled ← isTracingEnabledFor cls
269269
unless clsEnabled || trace.profiler.get opts do
270270
return (← k)
271271
let oldTraces ← getResetTraces
272-
let (res, start, stop) ← withStartStop opts <| observing k
273-
let aboveThresh := trace.profiler.get opts &&
274-
stop - start > trace.profiler.threshold.unitAdjusted opts
275-
unless clsEnabled || aboveThresh do
276-
modifyTraces (oldTraces ++ ·)
277-
return (← MonadExcept.ofExcept res)
278-
let ref ← getRef
279-
let mut m ← try msg res catch _ => pure m!"<exception thrown while producing trace node message>"
280-
let mut data := { cls, collapsed, tag }
281-
if trace.profiler.get opts then
282-
data := { data with startTime := start, stopTime := stop }
283-
addTraceNode oldTraces data ref m
284-
MonadExcept.ofExcept res
272+
let resStartStop ← withStartStop opts <| let _ := always.except; observing k
273+
postCallback opts clsEnabled oldTraces msg resStartStop
274+
where
275+
postCallback (opts : Options) (clsEnabled oldTraces msg resStartStop) : m α := do
276+
let _ := always.except
277+
let (res, start, stop) := resStartStop
278+
let aboveThresh := trace.profiler.get opts &&
279+
stop - start > trace.profiler.threshold.unitAdjusted opts
280+
unless clsEnabled || aboveThresh do
281+
modifyTraces (oldTraces ++ ·)
282+
return (← MonadExcept.ofExcept res)
283+
let ref ← getRef
284+
let mut m ← try msg res catch _ => pure m!"<exception thrown while producing trace node message>"
285+
let mut data := { cls, collapsed, tag }
286+
if trace.profiler.get opts then
287+
data := { data with startTime := start, stopTime := stop }
288+
addTraceNode oldTraces data ref m
289+
MonadExcept.ofExcept res
285290

286291
/-- A version of `Lean.withTraceNode` which allows generating the message within the computation. -/
292+
@[inline]
287293
def withTraceNode' [MonadAlwaysExcept Exception m] [MonadLiftT BaseIO m] (cls : Name)
288294
(k : m (α × MessageData)) (collapsed := true) (tag := "") : m α :=
289295
let msg := fun
@@ -369,10 +375,10 @@ the result produced by `k` into an emoji (e.g., `💥️`, `✅️`, `❌️`).
369375
370376
TODO: find better name for this function.
371377
-/
378+
@[inline]
372379
def withTraceNodeBefore [MonadRef m] [AddMessageContext m] [MonadOptions m]
373380
[always : MonadAlwaysExcept ε m] [MonadLiftT BaseIO m] [ExceptToEmoji ε α] (cls : Name)
374-
(msg : Unit → m MessageData) (k : m α) (collapsed := true) (tag := "") : m α := do
375-
let _ := always.except
381+
(msg : Unit -> m MessageData) (k : m α) (collapsed := true) (tag := "") : m α := do
376382
let opts ← getOptions
377383
let clsEnabled ← isTracingEnabledFor cls
378384
unless clsEnabled || trace.profiler.get opts do
@@ -381,18 +387,23 @@ def withTraceNodeBefore [MonadRef m] [AddMessageContext m] [MonadOptions m]
381387
let ref ← getRef
382388
-- make sure to preserve context *before* running `k`
383389
let msg ← withRef ref do addMessageContext (← msg ())
384-
let (res, start, stop) ← withStartStop opts <| observing k
385-
let aboveThresh := trace.profiler.get opts &&
386-
stop - start > trace.profiler.threshold.unitAdjusted opts
387-
unless clsEnabled || aboveThresh do
388-
modifyTraces (oldTraces ++ ·)
389-
return (← MonadExcept.ofExcept res)
390-
let mut msg := m!"{ExceptToEmoji.toEmoji res} {msg}"
391-
let mut data := { cls, collapsed, tag }
392-
if trace.profiler.get opts then
393-
data := { data with startTime := start, stopTime := stop }
394-
addTraceNode oldTraces data ref msg
395-
MonadExcept.ofExcept res
390+
let resStartStop ← withStartStop opts <| let _ := always.except; observing k
391+
postCallback opts clsEnabled oldTraces ref msg resStartStop
392+
where
393+
postCallback (opts : Options) (clsEnabled oldTraces ref msg resStartStop) : m α := do
394+
let _ := always.except
395+
let (res, start, stop) := resStartStop
396+
let aboveThresh := trace.profiler.get opts &&
397+
stop - start > trace.profiler.threshold.unitAdjusted opts
398+
unless clsEnabled || aboveThresh do
399+
modifyTraces (oldTraces ++ ·)
400+
return (← MonadExcept.ofExcept res)
401+
let mut msg := m!"{ExceptToEmoji.toEmoji res} {msg}"
402+
let mut data := { cls, collapsed, tag }
403+
if trace.profiler.get opts then
404+
data := { data with startTime := start, stopTime := stop }
405+
addTraceNode oldTraces data ref msg
406+
MonadExcept.ofExcept res
396407

397408
def addTraceAsMessages [Monad m] [MonadRef m] [MonadLog m] [MonadTrace m] : m Unit := do
398409
if trace.profiler.output.get? (← getOptions) |>.isSome then

0 commit comments

Comments
 (0)