Skip to content

Commit 8904e5c

Browse files
authored
feat: lake: builtin facet memoize toggle (#7738)
This PR makes memoization of built-in facets toggleable through a `memoize` option on the facet configuration. Built-in facets which are essentially aliases (e.g., `default`, `o`) have had memoization disabled.
1 parent ef9094d commit 8904e5c

File tree

5 files changed

+29
-19
lines changed

5 files changed

+29
-19
lines changed

src/lake/Lake/Build/Executable.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def LeanExe.recBuildDefault (lib : LeanExe) : FetchM (Job FilePath) :=
5050

5151
/-- The facet configuration for the builtin `ExternLib.dynlibFacet`. -/
5252
def LeanExe.defaultFacetConfig : LeanExeFacetConfig defaultFacet :=
53-
mkFacetJobConfig recBuildDefault
53+
mkFacetJobConfig recBuildDefault (memoize := false)
5454

5555
/--
5656
A name-configuration map for the initial set of

src/lake/Lake/Build/ExternLib.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def ExternLib.recBuildDefault (lib : ExternLib) : FetchM (Job FilePath) :=
8181

8282
/-- The facet configuration for the builtin `ExternLib.dynlibFacet`. -/
8383
def ExternLib.defaultFacetConfig : ExternLibFacetConfig defaultFacet :=
84-
mkFacetJobConfig recBuildDefault
84+
mkFacetJobConfig recBuildDefault (memoize := false)
8585

8686
/--
8787
A name-configuration map for the initial set of

src/lake/Lake/Build/Index.lean

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,37 @@ open System (FilePath)
2222
namespace Lake
2323

2424
/-- Recursive build function for anything in the Lake build index. -/
25-
def recBuildWithIndex (info : BuildInfo) : FetchM (Job (BuildData info.key)) :=
25+
def recBuildWithIndex (info : BuildInfo) : FetchM (Job (BuildData info.key)) := do
2626
match info with
27-
| .target pkg target => do
27+
| .target pkg target =>
2828
if let some decl := pkg.findTargetDecl? target then
2929
if h : decl.kind.isAnonymous then
30-
(decl.targetConfig h).fetchFn pkg
30+
let key := BuildKey.packageTarget pkg.name target
31+
fetchOrCreate key do (decl.targetConfig h).fetchFn pkg
3132
else
3233
let kind := ⟨decl.kind, by simp [decl.target_eq_type h]⟩
3334
let job := Job.pure (kind := kind) <| decl.mkConfigTarget pkg
3435
return cast (by simp [decl.data_eq_target h]) job
3536
else
3637
error s!"invalid target '{info}': target not found in package"
37-
| .facet _ kind data facet => do
38+
| .facet target kind data facet =>
3839
if let some config := (← getWorkspace).findFacetConfig? facet then
3940
if h : config.kind = kind then
40-
config.fetchFn <| cast (by simp [h]) data
41+
let recFetch := config.fetchFn <| cast (by simp [h]) data
42+
if config.memoize then
43+
let key := BuildKey.facet target facet
44+
fetchOrCreate key recFetch
45+
else
46+
recFetch
4147
else
42-
error s!"invalid target '{info}': target is of kind '{kind}', but facet expects '{config.kind}'"
48+
error s!"invalid target '{info}': \
49+
input target is of kind '{kind}', but facet expects '{config.kind}'"
4350
else
4451
error s!"invalid target '{info}': unknown facet '{facet}'"
4552

4653
/-- Recursive build function with memoization. -/
4754
def recFetchWithIndex : (info : BuildInfo) → RecBuildM (Job (BuildData info.key)) :=
48-
inline <| recFetchMemoize (β := (Job <| BuildData ·)) BuildInfo.key recBuildWithIndex
55+
inline <| recFetchAcyclic (β := (Job <| BuildData ·.key)) BuildInfo.key recBuildWithIndex
4956

5057
/--
5158
Run a recursive Lake build using the Lake build index

src/lake/Lake/Build/Module.lean

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ def Module.recBuildLean (mod : Module) : FetchM (Job Unit) := do
291291

292292
/-- The `ModuleFacetConfig` for the builtin `leanArtsFacet`. -/
293293
def Module.leanArtsFacetConfig : ModuleFacetConfig leanArtsFacet :=
294-
mkFacetJobConfig (·.recBuildLean)
294+
mkFacetJobConfig recBuildLean
295295

296296
/-- The `ModuleFacetConfig` for the builtin `oleanFacet`. -/
297297
def Module.oleanFacetConfig : ModuleFacetConfig oleanFacet :=
@@ -377,11 +377,11 @@ def Module.recBuildLeanCToONoExport (self : Module) : FetchM (Job FilePath) := d
377377

378378
/-- The `ModuleFacetConfig` for the builtin `coNoExportFacet`. -/
379379
def Module.coNoExportFacetConfig : ModuleFacetConfig coNoExportFacet :=
380-
mkFacetJobConfig Module.recBuildLeanCToONoExport
380+
mkFacetJobConfig recBuildLeanCToONoExport
381381

382382
/-- The `ModuleFacetConfig` for the builtin `coFacet`. -/
383383
def Module.coFacetConfig : ModuleFacetConfig coFacet :=
384-
mkFacetJobConfig fun mod =>
384+
mkFacetJobConfig (memoize := false) fun mod =>
385385
if Platform.isWindows then mod.coNoExport.fetch else mod.coExport.fetch
386386

387387
/-- Recursively build the module's object file from its bitcode file produced by `lean`. -/
@@ -392,25 +392,25 @@ def Module.recBuildLeanBcToO (self : Module) : FetchM (Job FilePath) := do
392392

393393
/-- The `ModuleFacetConfig` for the builtin `bcoFacet`. -/
394394
def Module.bcoFacetConfig : ModuleFacetConfig bcoFacet :=
395-
mkFacetJobConfig Module.recBuildLeanBcToO
395+
mkFacetJobConfig recBuildLeanBcToO
396396

397397
/-- The `ModuleFacetConfig` for the builtin `oExportFacet`. -/
398398
def Module.oExportFacetConfig : ModuleFacetConfig oExportFacet :=
399-
mkFacetJobConfig fun mod =>
399+
mkFacetJobConfig (memoize := false) fun mod =>
400400
match mod.backend with
401401
| .default | .c => mod.coExport.fetch
402402
| .llvm => mod.bco.fetch
403403

404404
/-- The `ModuleFacetConfig` for the builtin `oNoExportFacet`. -/
405405
def Module.oNoExportFacetConfig : ModuleFacetConfig oNoExportFacet :=
406-
mkFacetJobConfig fun mod =>
406+
mkFacetJobConfig (memoize := false) fun mod =>
407407
match mod.backend with
408408
| .default | .c => mod.coNoExport.fetch
409409
| .llvm => error "the LLVM backend only supports exporting Lean symbols"
410410

411411
/-- The `ModuleFacetConfig` for the builtin `oFacet`. -/
412412
def Module.oFacetConfig : ModuleFacetConfig oFacet :=
413-
mkFacetJobConfig fun mod =>
413+
mkFacetJobConfig (memoize := false) fun mod =>
414414
match mod.backend with
415415
| .default | .c => mod.co.fetch
416416
| .llvm => mod.bco.fetch
@@ -444,7 +444,7 @@ def Module.recBuildDynlib (mod : Module) : FetchM (Job Dynlib) :=
444444

445445
/-- The `ModuleFacetConfig` for the builtin `dynlibFacet`. -/
446446
def Module.dynlibFacetConfig : ModuleFacetConfig dynlibFacet :=
447-
mkFacetJobConfig Module.recBuildDynlib
447+
mkFacetJobConfig recBuildDynlib
448448

449449
/--
450450
A name-configuration map for the initial set of

src/lake/Lake/Config/FacetConfig.lean

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ structure FacetConfig (name : Name) : Type where
2222
buildable : Bool := true
2323
/-- Format this facet's output (e.g., for `lake query`). -/
2424
format : OutFormat → FacetOut name → String
25+
/-- Whether the fetch of this facet should be cached in the Lake build store. -/
26+
memoize : Bool := true
2527
deriving Inhabited
2628

2729
protected abbrev FacetConfig.name (_ : FacetConfig name) := name
@@ -54,9 +56,10 @@ def FacetConfig.toKind? (kind : Name) (self : FacetConfig name) : Option (KFacet
5456
[outKind : OptDataKind β]
5557
[i : FamilyOut DataType kind α]
5658
[o : FamilyOut FacetOut facet β]
57-
(build : α → FetchM (Job β)) (buildable := true)
59+
(build : α → FetchM (Job β))
60+
(buildable := true) (memoize := true)
5861
: KFacetConfig kind facet where
59-
buildable
62+
buildable; memoize
6063
outKind := o.fam_eq ▸ outKind
6164
fetchFn := i.fam_eq ▸ o.fam_eq ▸ build
6265
format := o.fam_eq ▸ formatQuery

0 commit comments

Comments
 (0)