Skip to content

Commit

Permalink
perf: lake: fix log replay over-inlining
Browse files Browse the repository at this point in the history
  • Loading branch information
tydeu committed Jun 5, 2024
1 parent 81f5b07 commit 656fbce
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 11 deletions.
17 changes: 10 additions & 7 deletions src/lake/Lake/CLI/Main.lean
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,11 @@ def CliM.run (self : CliM α) (args : List String) : BaseIO ExitCode := do
let main := main.run >>= fun | .ok a => pure a | .error e => error e.toString
main.run

instance : MonadLift LogIO CliStateM :=
fun x => do MainM.runLogIO x (← get).verbosity.minLogLv (← get).ansiMode⟩
@[inline] def CliStateM.runLogIO (x : LogIO α) : CliStateM α := do
let opts ← get
MainM.runLogIO x opts.verbosity.minLogLv opts.ansiMode

instance (priority := low+1) : MonadLift LogIO CliStateM := ⟨CliStateM.runLogIO⟩

/-! ## Argument Parsing -/

Expand Down Expand Up @@ -273,7 +276,7 @@ protected def doc : CliM PUnit := do
| none => throw <| CliError.missingScriptDoc script.name

protected def help : CliM PUnit := do
IO.println <| helpScript <| (← takeArg?).getD ""
IO.println <| helpScript <| ← takeArgD ""

end script

Expand All @@ -290,14 +293,14 @@ protected def new : CliM PUnit := do
processOptions lakeOption
let opts ← getThe LakeOptions
let name ← takeArg "package name"
let (tmp, lang) ← parseTemplateLangSpec <| (← takeArg?).getD ""
let (tmp, lang) ← parseTemplateLangSpec <| ← takeArgD ""
noArgsRem do new name tmp lang (← opts.computeEnv) opts.rootDir

protected def init : CliM PUnit := do
processOptions lakeOption
let opts ← getThe LakeOptions
let name := (← takeArg?).getD "."
let (tmp, lang) ← parseTemplateLangSpec <| (← takeArg?).getD ""
let name := ← takeArgD "."
let (tmp, lang) ← parseTemplateLangSpec <| ← takeArgD ""
noArgsRem do init name tmp lang (← opts.computeEnv) opts.rootDir

protected def build : CliM PUnit := do
Expand Down Expand Up @@ -480,7 +483,7 @@ protected def selfCheck : CliM PUnit := do
noArgsRem do verifyInstall (← getThe LakeOptions)

protected def help : CliM PUnit := do
IO.println <| help <| (← takeArg?).getD ""
IO.println <| help <| ← takeArgD ""

end lake

Expand Down
4 changes: 4 additions & 0 deletions src/lake/Lake/Util/Cli.lean
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ variable [Monad m] [MonadStateOf ArgList m]
@[inline] def takeArg? : m (Option String) :=
modifyGet fun | [] => (none, []) | arg :: args => (some arg, args)

/-- Take the head of the remaining argument list (or `default` if none). -/
@[inline] def takeArgD (default : String) : m String :=
modifyGet fun | [] => (default, []) | arg :: args => (arg, args)

/-- Take the remaining argument list, leaving only an empty list. -/
@[inline] def takeArgs : m (List String) :=
modifyGet fun args => (args, [])
Expand Down
9 changes: 8 additions & 1 deletion src/lake/Lake/Util/Log.lean
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,13 @@ Prints log entries of at least `minLv` to `out`.
@[inline] def toBaseIO (self : LogIO α)
(minLv := LogLevel.info) (ansiMode := AnsiMode.auto) (out := OutStream.stderr)
: BaseIO (Option α) := do
self.replayLog? (logger := ← out.getLogger minLv ansiMode)
let logger ← out.getLogger minLv ansiMode
let (a?, log) ← self.run? {}
replay log logger
return a?
where
-- avoid specialize of this call at each call site
replay (log : Log) (logger : MonadLog BaseIO) : BaseIO Unit :=
log.replay (logger := logger)

end LogIO
13 changes: 10 additions & 3 deletions src/lake/Lake/Util/MainM.lean
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ instance : MonadLift IO MainM := ⟨MonadError.runIO⟩
@[inline] def runLogIO (x : LogIO α)
(minLv := LogLevel.info) (ansiMode := AnsiMode.auto) (out := OutStream.stderr)
: MainM α := do
x.replayLog (logger := ← out.getLogger minLv ansiMode)

instance : MonadLift LogIO MainM := ⟨runLogIO⟩
let logger ← out.getLogger minLv ansiMode
match (← x {}) with
| .ok a log => replay log logger; return a
| .error _ log => replay log logger; exit 1
where
-- avoid specialize of this call at each call site
replay (log : Log) (logger : MonadLog BaseIO) : BaseIO Unit :=
log.replay (logger := logger)

instance (priority := low) : MonadLift LogIO MainM := ⟨runLogIO⟩

0 comments on commit 656fbce

Please sign in to comment.