Skip to content

feat: add Lean.CompactedRegion.save/read supporting cross-file object sharing#13185

Merged
Kha merged 2 commits into
leanprover:masterfrom
Kha:push-yzvtzvwsznml
May 16, 2026
Merged

feat: add Lean.CompactedRegion.save/read supporting cross-file object sharing#13185
Kha merged 2 commits into
leanprover:masterfrom
Kha:push-yzvtzvwsznml

Conversation

@Kha
Copy link
Copy Markdown
Member

@Kha Kha commented Mar 30, 2026

This PR adds new incremental module serialization functions that save/load a single module at a time with explicit sharing via dep regions and compactor state, generalizing the existing batch saveModuleDataParts API.

Two sharing mechanisms that can be mixed:

  • CompactedRegion dep regions for sharing with loaded regions
  • CompactorState for same-process chaining (pre-loaded m_obj_table)

@Kha
Copy link
Copy Markdown
Member Author

Kha commented Mar 30, 2026

!bench

@leanprover-radar
Copy link
Copy Markdown

leanprover-radar commented Mar 30, 2026

Benchmark results for 510303f against 4786e08 are in. Significant changes detected! @Kha

  • 🟥 build//instructions: +17.1G (+0.14%)
  • 🟥 other exited with code 2

Large changes (5🟥)

  • 🟥 size/Init/.olean.private//bytes: +42MiB (+17.46%)
  • 🟥 size/Init/.olean.server//bytes: +7MiB (+69.46%)
  • 🟥 size/all/.olean.private//bytes: +143MiB (+11.98%)
  • 🟥 size/all/.olean.server//bytes: +23MiB (+82.96%)
  • 🟥 size/install//bytes: +166MiB (+6.77%)

Small changes (51🟥)

Too many entries to display here. View the full report on radar instead.

@github-actions github-actions Bot added the toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN label Mar 30, 2026
@leanprover-bot
Copy link
Copy Markdown
Collaborator

leanprover-bot commented Mar 30, 2026

Reference manual CI status:

  • ❗ Reference manual CI can not be attempted yet, as the nightly-testing-2026-03-25 tag does not exist there yet. We will retry when you push more commits. If you rebase your branch onto nightly-with-manual, reference manual CI should run now. You can force reference manual CI using the force-manual-ci label. (2026-03-30 09:59:44)
  • ✅ Reference manual branch lean-pr-testing-13185 has successfully built against this PR. (2026-04-03 10:24:49) View Log
  • 🟡 Reference manual branch lean-pr-testing-13185 build against this PR didn't complete normally. (2026-04-03 10:25:50) View Log
  • ❗ Reference manual CI will not be attempted unless your PR branches off the nightly-with-manual branch. Try git rebase 8c82f9ed4ae09b165342b24a6461ccc097caae50 --onto ed0d50fcf0efa8c9a32e1ccb3603ce43edce6926. You can force reference manual CI using the force-manual-ci label. (2026-05-13 17:37:07)
  • ❗ Reference manual CI will not be attempted unless your PR branches off the nightly-with-manual branch. Try git rebase 8c82f9ed4ae09b165342b24a6461ccc097caae50 --onto 803553a556fd82fa1060efb0c43eda542130cb16. You can force reference manual CI using the force-manual-ci label. (2026-05-14 14:36:12)

mathlib-nightly-testing Bot pushed a commit to leanprover-community/batteries that referenced this pull request Mar 30, 2026
@github-actions github-actions Bot added the mathlib4-nightly-available A branch for this PR exists at leanprover-community/mathlib4-nightly-testing:lean-pr-testing-NNNN label Mar 30, 2026
mathlib-nightly-testing Bot pushed a commit to leanprover-community/mathlib4-nightly-testing that referenced this pull request Mar 30, 2026
@mathlib-lean-pr-testing mathlib-lean-pr-testing Bot added the breaks-mathlib This is not necessarily a blocker for merging: but there needs to be a plan label Mar 30, 2026
@mathlib-lean-pr-testing
Copy link
Copy Markdown

mathlib-lean-pr-testing Bot commented Mar 30, 2026

Mathlib CI status (docs):

  • ❌ Mathlib branch lean-pr-testing-13185 built against this PR, but linting failed. (2026-03-30 10:45:12) View Log
  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase 8c82f9ed4ae09b165342b24a6461ccc097caae50 --onto ed0d50fcf0efa8c9a32e1ccb3603ce43edce6926. You can force Mathlib CI using the force-mathlib-ci label. (2026-05-13 17:37:05)
  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase 8c82f9ed4ae09b165342b24a6461ccc097caae50 --onto 803553a556fd82fa1060efb0c43eda542130cb16. You can force Mathlib CI using the force-mathlib-ci label. (2026-05-14 14:36:09)

@Kha Kha force-pushed the push-yzvtzvwsznml branch from 510303f to 0f23528 Compare March 30, 2026 13:20
@Kha
Copy link
Copy Markdown
Member Author

Kha commented Mar 30, 2026

!bench

@leanprover-radar
Copy link
Copy Markdown

leanprover-radar commented Mar 30, 2026

Benchmark results for 0f23528 against 4786e08 are in. There are no significant changes. @Kha

  • 🟥 build//instructions: +98.6M (+0.00%)

Small changes (2🟥)

  • 🟥 build/module/Lean.Environment//instructions: +120.3M (+0.73%)
  • 🟥 elab/big_beq_rec//task-clock: +36ms (+2.76%)

mathlib-nightly-testing Bot pushed a commit to leanprover-community/batteries that referenced this pull request Mar 30, 2026
mathlib-nightly-testing Bot pushed a commit to leanprover-community/mathlib4-nightly-testing that referenced this pull request Mar 30, 2026
@mathlib-lean-pr-testing
Copy link
Copy Markdown

Mathlib CI status (docs):

@Kha Kha force-pushed the push-yzvtzvwsznml branch from 0f23528 to 752a5e0 Compare March 30, 2026 15:20
@Kha Kha added the release-ci Enable all CI checks for a PR, like is done for releases label Mar 30, 2026
mathlib-nightly-testing Bot pushed a commit to leanprover-community/batteries that referenced this pull request Mar 30, 2026
mathlib-nightly-testing Bot pushed a commit to leanprover-community/mathlib4-nightly-testing that referenced this pull request Mar 30, 2026
@mathlib-lean-pr-testing
Copy link
Copy Markdown

Mathlib CI status (docs):

@Kha Kha force-pushed the push-yzvtzvwsznml branch from 5d58fca to c82dd26 Compare March 30, 2026 19:05
mathlib-nightly-testing Bot pushed a commit to leanprover-community/batteries that referenced this pull request Mar 30, 2026
mathlib-nightly-testing Bot pushed a commit to leanprover-community/mathlib4-nightly-testing that referenced this pull request Mar 30, 2026
@mathlib-lean-pr-testing
Copy link
Copy Markdown

Mathlib CI status (docs):

@Kha Kha marked this pull request as ready for review March 31, 2026 09:28
mathlib-nightly-testing Bot pushed a commit to leanprover-community/batteries that referenced this pull request Mar 31, 2026
mathlib-nightly-testing Bot pushed a commit to leanprover-community/mathlib4-nightly-testing that referenced this pull request Mar 31, 2026
@mathlib-lean-pr-testing mathlib-lean-pr-testing Bot added builds-mathlib CI has verified that Mathlib builds against this PR and removed breaks-mathlib This is not necessarily a blocker for merging: but there needs to be a plan labels Mar 31, 2026
@Kha Kha force-pushed the push-yzvtzvwsznml branch from 360f75a to 0849a2b Compare April 1, 2026 08:33
mathlib-nightly-testing Bot pushed a commit to leanprover-community/batteries that referenced this pull request Apr 1, 2026
mathlib-nightly-testing Bot pushed a commit to leanprover-community/mathlib4-nightly-testing that referenced this pull request Apr 1, 2026
@mathlib-lean-pr-testing
Copy link
Copy Markdown

Mathlib CI status (docs):

@Kha Kha force-pushed the push-yzvtzvwsznml branch from 0849a2b to 91e27c1 Compare April 3, 2026 08:41
mathlib-nightly-testing Bot pushed a commit to leanprover-community/batteries that referenced this pull request Apr 3, 2026
leanprover-bot added a commit to leanprover/reference-manual that referenced this pull request Apr 3, 2026
mathlib-nightly-testing Bot pushed a commit to leanprover-community/mathlib4-nightly-testing that referenced this pull request Apr 3, 2026
@leanprover-bot leanprover-bot added the builds-manual CI has verified that the Lean Language Reference builds against this PR label Apr 3, 2026
@mathlib-lean-pr-testing
Copy link
Copy Markdown

Mathlib CI status (docs):

@Kha Kha force-pushed the push-yzvtzvwsznml branch 2 times, most recently from 17a86da to d14a467 Compare May 13, 2026 15:47
@Kha
Copy link
Copy Markdown
Member Author

Kha commented May 13, 2026

!bench

@leanprover-radar
Copy link
Copy Markdown

leanprover-radar commented May 13, 2026

Benchmark results for d14a467 against 8c82f9e are in. There are significant results. @Kha

  • 🟥 build//instructions: +3.7G (+0.03%)

Large changes (1🟥)

  • 🟥 size/compile/.out//bytes: +211MiB (+10.72%)

Small changes (2✅, 3🟥)

  • 🟥 build/module/Lean.Environment//instructions: +84.3M (+0.57%)
  • 🟥 elab/bv_decide_large_aig//instructions: +248.1M (+0.62%)
  • misc/import Lean//instructions: -7.2M (-0.48%)
  • misc/import Std.Data.Internal.List.Associative//task-clock: -779ms (-7.18%)
  • 🟥 size/all/.cpp//lines: +90.0 (+0.31%)

@Kha Kha force-pushed the push-yzvtzvwsznml branch from d14a467 to 95e7047 Compare May 14, 2026 12:33
@Kha
Copy link
Copy Markdown
Member Author

Kha commented May 14, 2026

!bench

@leanprover-radar
Copy link
Copy Markdown

leanprover-radar commented May 14, 2026

Benchmark results for 95e7047 against 8c82f9e are in. There are significant results. @Kha

  • 🟥 build//instructions: +3.7G (+0.03%)

Large changes (1🟥)

  • 🟥 size/compile/.out//bytes: +211MiB (+10.72%)

Small changes (1✅, 3🟥)

  • 🟥 build/module/Lean.Environment//instructions: +111.0M (+0.75%)
  • 🟥 elab/bv_decide_large_aig//instructions: +227.7M (+0.57%)
  • elab/grind_bitvec2//task-clock: -2s (-6.66%)
  • 🟥 size/all/.cpp//lines: +90.0 (+0.31%)

@Kha Kha force-pushed the push-yzvtzvwsznml branch from 95e7047 to 59b9ac3 Compare May 15, 2026 12:12
@Kha Kha enabled auto-merge May 15, 2026 12:12
@Kha Kha changed the title feat: add saveModuleDataIncr/readModuleDataIncr with dep region sharing feat: add Lean.CompactedRegion.save/read supporting cross-file object sharing May 15, 2026
@Kha Kha force-pushed the push-yzvtzvwsznml branch from 59b9ac3 to fc26234 Compare May 15, 2026 12:19
Kha added 2 commits May 16, 2026 10:42
This PR adds `Lean.CompactedRegion.save` and `Lean.CompactedRegion.read` as the primary low-level olean save/load primitives, generic in the serialized data type. The existing `ModuleData`-typed functions (`saveModuleData`/`readModuleData` and the `*Parts` variants) become thin wrappers around them.

Two sharing mechanisms are supported:

* `CompactedRegion` dep regions: objects already present in a loaded dep region are emitted as cross-region pointers rather than re-serialized, and the loader's `fix_object_ptr` resolves them via a binary search over deps sorted by saved `base_addr`.
* A `Compactor` chaining handle: consecutive saves with the same `Compactor` reuse the prior call's pointer-identity and structural-sharing tables so objects shared across parts are emitted exactly once.

The reader maps oleans `MAP_PRIVATE | PROT_READ | PROT_WRITE` so the pointer-fixup walk can write the loaded buffer in place when needed (`MAP_PRIVATE` keeps unwritten pages shared with the page cache, so the write protection is free when no fixup happens). On Linux, `MAP_FIXED_NOREPLACE` is OR'd in when available (with a `#define` fallback for older glibc) to let the kernel atomically reject collisions at the saved `base_addr`; macOS doesn't expose the flag, and the post-check + cleanup `munmap` handles the same case there. The fast path in `compacted_region::read()` skips the full object walk when both the region itself and every dep region landed at their saved addresses; otherwise it falls through to the full fix-up walk. The `compacted_region` constructor rejects overlapping saved `base_addr` ranges to avoid silent translation through the wrong region.
… tests

`tests/compile/run_bench.sh` only emitted `<test>.measurements.jsonl` from the `if [[ -n $DO_COMPILE ]]` block, and the earlier `rm -f` removed any stale file. With `.no_compile` set, the file never existed, and `combine.py` later crashed with `FileNotFoundError` when reading the expected per-test jsonl. Truncate-or-create the file unconditionally before the block so an empty measurements file is always available.
@Kha Kha force-pushed the push-yzvtzvwsznml branch 2 times, most recently from 8100976 to e609d91 Compare May 16, 2026 11:17
fi

rm -f "$1.measurements.jsonl"
: > "$1.measurements.jsonl" # always create (possibly empty) so `combine.py` finds it
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI @Garmelon; not sure using .no_compile in compile/ really is the best approach, could be a new pile run/ instead?

@Kha Kha added this pull request to the merge queue May 16, 2026
Merged via the queue into leanprover:master with commit 895752d May 16, 2026
22 checks passed
@Kha Kha deleted the push-yzvtzvwsznml branch May 29, 2026 09:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

builds-manual CI has verified that the Lean Language Reference builds against this PR builds-mathlib CI has verified that Mathlib builds against this PR changelog-other mathlib4-nightly-available A branch for this PR exists at leanprover-community/mathlib4-nightly-testing:lean-pr-testing-NNNN release-ci Enable all CI checks for a PR, like is done for releases toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants