Skip to content

Commit f6efe14

Browse files
committed
chore: caching touchups
1 parent e0ff5ae commit f6efe14

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

Cache/Hashing.lean

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ Computes the root hash, which mixes the hashes of the content of:
9090
* `lean-toolchain`
9191
* `lake-manifest.json`
9292
93-
and the hash of `Lean.githash`.
93+
and the hash of `CacheM.Context.useLakeCache`.
9494
-/
9595
def getRootHash : CacheM UInt64 := do
9696
let mathlibDepPath := (← read).mathlibDepPath
@@ -100,7 +100,8 @@ def getRootHash : CacheM UInt64 := do
100100
mathlibDepPath / "lake-manifest.json"]
101101
let hashes ← rootFiles.mapM fun path =>
102102
hashFileContents <$> IO.FS.readFile path
103-
return hash (rootHashGeneration :: hashes)
103+
let lakeCacheHash := hash (← read).useLakeCache
104+
return hash (rootHashGeneration :: lakeCacheHash :: hashes)
104105

105106
/--
106107
Computes the hash of a file, which mixes:

Cache/IO.lean

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ structure CacheM.Context where
126126
/-- build directory for proofwidgets -/
127127
proofWidgetsBuildDir : FilePath
128128
/-- directory for the machine-local Lake artifact cache -/
129-
lakeArtifactDir? : Option FilePath
129+
lakeArtifactDir : FilePath
130130
/-- whether cache files should be packed/unpacked to the local Kake artifact cache -/
131131
useLakeCache : Bool
132132

@@ -151,27 +151,32 @@ def _root_.Lean.SearchPath.relativize (sp : SearchPath) : IO SearchPath := do
151151
let pwd' := pwd.toString ++ System.FilePath.pathSeparator.toString
152152
return sp.map fun x => ⟨if x = pwd then "." else x.toString.dropPrefix pwd' |>.copy⟩
153153

154+
/--- The directory in the workspace used for the Lake cache if no suitable system-wide cache exists. -/
155+
private def LAKECACHEDIR : FilePath :=
156+
".lake" / "cache"
157+
154158
/--
155159
Detects the directory Lake uses to cache artifacts.
156160
This is very sensitive to changes in `Lake.Env.compute`.
157161
-/
158-
def getLakeArtifactDir? : IO (Option FilePath) := do
162+
private def getLakeArtifactDir : IO FilePath := do
159163
let elan? ← Lake.findElanInstall?
160164
let toolchain ← Lake.Env.computeToolchain
161165
let some cache ← Lake.Env.computeCache? elan? toolchain
162-
| return none
166+
| return LAKECACHEDIR
163167
return cache.artifactDir
164168

165169
private def CacheM.getContext : IO CacheM.Context := do
166170
let sp ← (← getSrcSearchPath).relativize
167171
let mathlibSource ← CacheM.mathlibDepPath sp
168172
let useLakeCache := (← IO.getEnv "MATHLIB_CACHE_USE_LAKE").bind Lake.envToBool? |>.getD true
173+
let lakeArtifactDir ← getLakeArtifactDir
174+
IO.FS.createDirAll lakeArtifactDir
169175
return {
170176
mathlibDepPath := mathlibSource,
171177
srcSearchPath := sp,
172178
proofWidgetsBuildDir := LAKEPACKAGESDIR / "proofwidgets" / ".lake" / "build"
173-
lakeArtifactDir? := ← getLakeArtifactDir?,
174-
useLakeCache}
179+
lakeArtifactDir, useLakeCache}
175180

176181
/-- Run a `CacheM` in `IO` by loading the context from `LEAN_SRC_PATH`. -/
177182
def CacheM.run (f : CacheM α) : IO α := do ReaderT.run f (← getContext)
@@ -364,8 +369,7 @@ def mkBuildPaths (mod : Name) : CacheM (Option BuildPaths) := OptionT.run do
364369
let irPath := packageDir / IRDIR / path
365370
let libPath := packageDir / LIBDIR / path
366371
let trace := libPath.withExtension "trace"
367-
let artifactDir := (← read).lakeArtifactDir?.getD (lakeDir / "cache")
368-
IO.FS.createDirAll artifactDir
372+
let artifactDir := (← read).lakeArtifactDir
369373
-- Note: the `.olean`, `.olean.server`, `.olean.private` files must be consecutive,
370374
-- and in this order. The corresponding `.hash` files can come afterwards, in any order.
371375
if (← read).useLakeCache then
@@ -499,12 +503,13 @@ def unpackCache (hashMap : ModuleHashMap) (force : Bool) : CacheM Unit := do
499503
-/
500504
let isMathlibRoot ← isMathlibRoot
501505
let mathlibDepPath := (← read).mathlibDepPath.toString
502-
let artifactDir? := (← read).lakeArtifactDir?
506+
let artifactDir := (← read).lakeArtifactDir
507+
let useLakeCache := (← read).useLakeCache
503508
let config : Array Lean.Json := hashMap.fold (init := #[]) fun config mod hash =>
504509
let pathStr := s!"{CACHEDIR / hash.asLTar}"
505510
-- only mathlib files, when not in the mathlib4 repo, need to be redirected
506511
let localDir := if isMathlibRoot || !isFromMathlib mod then "." else mathlibDepPath
507-
let baseDirs := if let some dir := artifactDir? then #[localDir, dir.toString] else #[localDir]
512+
let baseDirs := if useLakeCache then #[localDir, artifactDir.toString] else #[localDir]
508513
config.push <| .mkObj [("file", pathStr), ("base", toJson baseDirs)]
509514
stdin.putStr <| Lean.Json.compress <| .arr config
510515
let exitCode ← child.wait

0 commit comments

Comments
 (0)