Skip to content

perf: parallel walk and merge, byte-identical output (stacked on #61) - #63

Open
abhiapsunde wants to merge 2 commits into
leanprover:masterfrom
abhiapsunde:parallel-export
Open

abhiapsunde wants to merge 2 commits into
leanprover:masterfrom
abhiapsunde:parallel-export

Conversation

@abhiapsunde

Copy link
Copy Markdown

Stacked on #61: the first commit in this PR is #61's, by alonbase, and is not mine. Only 73d9ad9 is new. Rebase onto master once #61 merges. Problem statement and measurements are in #62.

Adds Export/Par.lean (ExportPar.run): producer thread plus one worker per root expression plus one ordered merge thread, so the output matches the sequential exporter byte for byte. Pipelines output formatting (--serial-output disables it) and makes M.run flush queued output. --serial-walk and --export-mdata keep the sequential walk.

Results: full Riemann export 27:35 vs 1:53:37 for #61 alone, same sha256 (5ea74b91…6be244e), peak RSS 88.8 vs 86.9 GB. Memory is not lower than #61, CPU time is higher. lake test passes with two new sequential-vs-parallel byte comparison tests, including the re-entrant literal case.

Not a request to merge before #61. Open question for maintainers: this repository or Lean core's exporter?

🤖 Generated with Claude Code

alonbase and others added 2 commits November 24, 2026 00:48
`visitedNames`, `visitedLevels` and `visitedExprs` were `Std.HashMap α Nat`s. At 43–53 bytes per distinct object they make up most of the exporter's own memory on large exports. They become `Export.IndexSet α`: the elements stored once in insertion order, in chunks, plus an open-addressing table of 4-byte indices kept at most half full, for 16–24 bytes per object. The numbering and the `BEq`/`Hashable` instances are unchanged, so the output is unchanged byte for byte, except in one case where it was unreadable before (below).

Exporting more than `2 ^ 32 - 1` distinct objects of one kind now fails with an error.

Exporting a string literal first exports `Char.ofNat` and `String.ofList`. If one of them contains the same literal, which is not the case with `Init` but can be with a custom prelude, the old code gave the literal's index to the next object as well, so the file bound one index twice. `Export.parseStream` overwrites the first binding, but a reader that checks for this, such as the one in Lean core, rejects the file. The literal is now written twice under two indices. A test in `Test.lean` covers this, next to a test of `IndexSet` against `Std.HashMap`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Adds Export/Par.lean (ExportPar.run). A producer thread follows the
sequential control flow and spawns one worker per root expression. Workers
record their distinct nodes in first-visit post-order. One merge thread
assigns the global indices in the original order, so the output is
byte-identical to the sequential exporter. Entries are formatted in
batches by tasks and written in order.

The first Nat or String literal still triggers the Nat / Char.ofNat /
String.ofList exports at the same point as before, including the
re-entrant case in Test.lean.

Export.lean: output formatting is pipelined (--serial-output turns it off)
and M.run flushes queued output before returning.
--serial-walk and --export-mdata keep the sequential walk.

Tests compare sequential and parallel output on the re-entrant literal
prelude and on a set of constants with literals, binders, let and
projections.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants