Skip to content

Commit 8c564f1

Browse files
kim-emclaude
andcommitted
refactor(Cache): merge leanprover-community#34667 and address review comments
Merge skip-already-unpacked-cache (leanprover-community#34667) into pipeline-cache-decompression (leanprover-community#32987) and: - Remove duplicate hex parsing functions, use shared Char.hexDigitToNat? and String.parseHexToUInt64? from Cache/Lean.lean - Use FilePath instead of String for hashFromFileName and mathlibDepPath (call .toString only when needed for JSON) - Add docstring noting harvestDecompTask returns (successful, failed) tuple - Fix typo: print stdout not stderr for lake stdout 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 9da3c75 commit 8c564f1

File tree

1 file changed

+15
-29
lines changed

1 file changed

+15
-29
lines changed

Cache/Requests.lean

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -306,33 +306,18 @@ def downloadFile (repo : String) (hash : UInt64) : IO Bool := do
306306
IO.FS.removeFile partPath
307307
pure false
308308

309-
/-- Parse a hex digit character to its numeric value -/
310-
def hexDigitToNat (c : Char) : Option Nat :=
311-
if '0' ≤ c && c ≤ '9' then some (c.toNat - '0'.toNat)
312-
else if 'a' ≤ c && c ≤ 'f' then some (c.toNat - 'a'.toNat + 10)
313-
else if 'A' ≤ c && c ≤ 'F' then some (c.toNat - 'A'.toNat + 10)
314-
else none
315-
316-
/-- Parse a hex string to a Nat -/
317-
def parseHexString (s : String) : Option Nat :=
318-
s.foldl (init := some 0) fun acc c => do
319-
let prev ← acc
320-
let digit ← hexDigitToNat c
321-
some (prev * 16 + digit)
322-
323309
/-- Extract hash from filename (e.g., "/path/to/.cache/00012345.ltar" → 0x12345) -/
324-
def hashFromFileName (fn : String) : Option UInt64 := do
325-
-- Get the filename without path
326-
let some name := fn.splitOn "/" |>.getLast? | .none
327-
-- Remove .ltar extension (or .ltar.part for partial files)
328-
let stem := name.dropSuffix ".part"
329-
let stem := stem.dropSuffix ".ltar"
330-
-- Parse hex string as UInt64
331-
parseHexString stem.toString |>.map UInt64.ofNat
310+
def hashFromFileName (path : FilePath) : Option UInt64 := do
311+
-- Get the filename stem, handling .ltar.part files
312+
let some stem := path.fileStem | .none
313+
-- For .ltar.part files, fileStem gives "hash.ltar", so strip .ltar if present
314+
let stem := stem.dropSuffix ".ltar" |>.toString
315+
-- Parse the 16-character hex string to UInt64
316+
stem.parseHexToUInt64?
332317

333318
/-- Decompress a batch of files using a single leantar invocation -/
334319
def decompressBatch (files : Array (FilePath × Lean.Name))
335-
(force : Bool) (isMathlibRoot : Bool) (mathlibDepPath : String)
320+
(force : Bool) (isMathlibRoot : Bool) (mathlibDepPath : FilePath)
336321
: IO Unit := do
337322
if files.isEmpty then return
338323

@@ -341,7 +326,7 @@ def decompressBatch (files : Array (FilePath × Lean.Name))
341326
if isMathlibRoot || !IO.isFromMathlib mod then
342327
.str path.toString
343328
else
344-
.mkObj [("file", path.toString), ("base", mathlibDepPath)]
329+
.mkObj [("file", path.toString), ("base", mathlibDepPath.toString)]
345330

346331
-- Spawn leantar for this batch
347332
let args := (if force then #["-f"] else #[]) ++
@@ -370,7 +355,7 @@ structure DecompConfig where
370355
hashToMod : Std.HashMap UInt64 Lean.Name -- filename hash → module name
371356
force : Bool
372357
isMathlibRoot : Bool
373-
mathlibDepPath : String
358+
mathlibDepPath : FilePath
374359

375360
private structure TransferState where
376361
last : Nat
@@ -385,7 +370,8 @@ private structure TransferState where
385370
decompressed : Nat -- total files decompressed
386371
decompFailed : Nat -- total decompression failures
387372

388-
/-- Harvest the result of a completed decompression task, updating counters -/
373+
/-- Harvest the result of a completed decompression task, updating counters.
374+
Returns `(successful, failed)` counts. -/
389375
def harvestDecompTask (task : Task (Except IO.Error Unit)) (batchSize : Nat)
390376
(decompressed decompFailed : Nat) : Nat × Nat :=
391377
match task.get with
@@ -436,7 +422,7 @@ def monitorCurl (args : Array String) (size : Nat)
436422
IO.FS.rename fn finalPath
437423
-- Add to decompression queue if enabled
438424
if let some config := decompConfig then
439-
match hashFromFileName fn with
425+
match hashFromFileName ⟨fn⟩ with
440426
| some hash =>
441427
match config.hashToMod[hash]? with
442428
| some mod =>
@@ -502,7 +488,7 @@ def downloadFiles
502488
(repo : String) (hashMap : IO.ModuleHashMap)
503489
(forceDownload : Bool) (parallel : Bool) (warnOnMissing : Bool)
504490
(decompress : Bool := false) (forceUnpack : Bool := false)
505-
(isMathlibRoot : Bool) (mathlibDepPath : String): IO Unit := do
491+
(isMathlibRoot : Bool) (mathlibDepPath : FilePath) : IO Unit := do
506492
let hashMap ← if forceDownload then pure hashMap else hashMap.filterExists false
507493
let size := hashMap.size
508494
if size > 0 then
@@ -639,7 +625,7 @@ def getFiles
639625
unless isMathlibRoot do checkForToolchainMismatch
640626
getProofWidgets (← read).proofWidgetsBuildDir
641627

642-
let mathlibDepPath := (← read).mathlibDepPath.toString
628+
let mathlibDepPath := (← read).mathlibDepPath
643629

644630
if let some repo := repo? then
645631
downloadFiles repo hashMap forceDownload parallel (warnOnMissing := true)

0 commit comments

Comments
 (0)