Reduce LeanExport reader memory usage with dense index tables

Đang mở
#16,784 2 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
52/100
Loại issue
Tái cấu trúc
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Lĩnh vực
backend, performance

Hướng nghiên cứu

Start with src/LeanExport/Parse.lean, where the issue says the reader stores names, levels, and expressions in Std.HashMap Nat _; review the linked IdTable diff and the existing tests/elab/leanExportParseIndexOrder.lean test. Reproduce the leanchecker --from-export memory comparison using the documented steps and Lean export. Done means the dense-prefix tables reduce reader memory while preserving existing parsing behavior, including sparse and out-of-order indices.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

P-high
Prerequisites
  • Check that your issue is not already filed: searched on 26 Nov 2026, including the file lists of all open PRs. Related but separate: #16705 (the exporter) and #16769 (kernel caches).
  • Reduce the issue to a minimal, self-contained, reproducible test case: leanchecker --from-export on the output of leanexport Lean, from a plain build. No Mathlib is needed.
  • Test your test case against the latest nightly release: reproduced on the unmodified official nightly-2026-11-26 (e1228b8b45). Its leanexport Lean writes the same export as master 451d44ed52, byte for byte after the metadata line, and its leanchecker --from-export peaks at 1366 / 1365 MiB (sampled cgroup anon, two runs; ru_maxrss 1451 / 1447 MiB), as on master (1364 / 1364 MiB). src/LeanExport/Parse.lean is identical in both. The patched figures below come from builds of master 451d44ed52.
  • When reporting a perceived soundness bug: Does it affect lake check --paranoid? Not applicable.
Description

LeanExport.Parse, the export reader behind leanchecker --from-export, lake check --from-export and lake comparator, keeps every name, level and expression of an export in a Std.HashMap Nat _. LeanExport.Basic numbers each kind densely from 0, so these maps index a contiguous range, and a HashMap entry per object adds substantial memory overhead.

A small change to Parse.lean (+38/−6 lines, plus a test) adds a private IdTable:

  • the dense prefix of each kind goes in an Array;
  • any index that does not extend the prefix goes into the existing HashMap.

On the Lean export, this lowers the sampled peak anonymous memory of leanchecker --from-export from 1364 to 784 MiB (42.5% reduction). parseStream, ExportedEnv, the error messages and the order of checks are unchanged. The same table structure is used by con-leche and con-agua.

Context

Where the reader's memory shows up.

  • leanchecker --from-export. For the unpatched checker on the measured Lean and Std exports, the process's peak is the reader's peak, to within 5 MiB. With the patch, the peak moves later, presumably to replay.
  • lake check --from-export and lake comparator. Lake parses the export itself before it spawns the checker. The parse results are dead by then, but memory that Lake's allocator has not returned still counts while the checker runs. So the reader's footprint affects these runs too.

Compatibility. The sparse fallback keeps everything the reader accepts today, so inputs not written by leanexport keep working. leanprover/lean-kernel-arena (at cb0665b7) has tests/other/sparse-name-index.ndjson and tests/other/level-index-out-of-order.ndjson, both expected to be accepted, and both builds accept them. A pure Array would reject them. If the format is meant to guarantee dense in-order indices, the fallback can be dropped; that is a decision for the maintainers.

Correctness argument.

  • The invariant is that every key in the map is at least the array's size.
  • insert is only reached after contains returned false, and only addName, addLevel and addExpr write the tables.
  • So get? and contains answer as the old HashMap did, for every index, including indices beyond 2^64.
Steps to Reproduce
  1. git clone https://github.com/leanprover/lean4 && cd lean4 && git checkout 451d44ed52
  2. cmake --preset release && make -C build/release -j$(nproc)
  3. export PATH=$PWD/build/release/stage1/bin:$PATH && leanexport Lean > Lean.ndjson (823,583,439 bytes)
  4. /usr/bin/time -v leanchecker --from-export Lean.ndjson
  5. Apply the patch with git am, rebuild, and repeat step 4.

Expected behavior: The reader's tables cost little more than the objects they index, since leanexport writes dense indices.

Actual behavior: The tables add substantial memory overhead. Maximum resident set size is 1458 MiB without the patch and 878 MiB with it.

Versions

leanprover/lean4 master 451d44ed52 (4.38.0-pre). The patch also applies cleanly to master 6c625aaacc. Ubuntu 24.04 (WSL2, 6 GiB VM), x86-64. GCC 13.3 release build with mimalloc, plus Clang 23 from nightly-2026-11-24.

Additional Information

Memory: Lean export, GCC build. Sampled peak of the run's cgroup anon counter, in MiB, two runs each:

unpatched patched
leanchecker --from-export 1364 / 1364 784 / 784
lake check --from-export 2347 / 2353 1335 / 1348
lake comparator (one export as both challenge and solution) 2169 / 2702 1353 / 1354
  • Exact high-water mark. The exact ru_maxrss of leanchecker (total RSS) is 1458 / 1457 → 878 / 875 MiB.
  • Clang. A Clang 23 pair of leanchecker builds, compiled from the same sources with the nightly-2026-11-24 toolchain, gives 1359 → 779 MiB. That figure is the sampled peak anonymous memory of the process tree (RssAnon, sampled about every 50 ms), not the cgroup counter. Its exact ru_maxrss is 1414 → 839 MiB.
  • Init and Std exports. −30 to −52 % per process.
  • Larger exports, such as Mathlib, were not measured.

Cost on layouts without a dense prefix.

  • What was measured. Three layouts, none of which leanexport writes: Init renumbered 3i+7, Init reversed, and 10 M one-node expressions in reverse order.
  • Memory stayed within ±2 % of the unpatched reader, the run-to-run spread.
  • Parsing took about 4–9 % more user CPU time: +4.3 %, +4.7 % and +9.1 % (mean of 2 runs; single pairings +2 to +11 %).
  • End to end, leanchecker was within noise on the two Init layouts and +6.8 % on the synthetic file.
  • Losing the saving. Once the array reaches an index that is already bound in the map, the array stops growing, and every later index goes to the map. A file whose indices start 1, 0, 2, 3, … therefore gets essentially no saving. leanexport does not write such files.
  • Limitations. 2 runs per build, on a non-idle WSL2 host, GCC only. No bound is claimed for other layouts.
  • Speed. No speed improvement is claimed for ordinary exports. The change is proposed for its memory reduction.

Validation.

  • Checker level. 889 exports give byte-identical leanchecker stdout, stderr and exit codes from both builds. They are 24 Kernel Arena fixtures, 21 hand-written adversarial ones, 144 renumbered variants and 700 random exports with out-of-order, duplicate, dangling and >2^64 indices.
  • Parser level.
    • 40,000 generated cases (two corpora, fixed seeds) were parsed in one process by the old reader and by the final commit's reader.
    • For each case, the harness compares either the full error string, or constOrder followed by the kind, name, levelParams, type and value of every constant, in printed form.
    • Every case matched: 0 mismatches.
    • The generated cases are small, at most a few dozen lines each, and many exercise error paths: 98.8 % of one corpus and 46.9 % of the other end in an error.
    • When the old copy is changed deliberately, the harness reports 1,819 and 10,074 mismatches.
    • This compares parse results only. The generators and harness come from an independent AI-agent review and reproduce byte for byte.
  • Mutation. The new test tests/elab/leanExportParseIndexOrder.lean kills 7 of 8 mutants of IdTable, including one that reduces indices mod 2^64. The survivor pre-binds name 0 in the map instead of the array. It is functionally equivalent but not performance-equivalent, since names then never use the array; a functional test cannot detect that. The test passes on the unpatched reader too.
  • Upstream tests. 13 of 13 pass on the patched build: lake/tests/check-*, lake/tests/comparator-*, pkg/leanchecker, elab/leanExportParse and elab/leanExportParseIndexOrder.
  • Not a proof. These tests show no difference on the inputs tried.

Related.

  • #16705 changes the exporter (leanexport); this changes the reader. They touch different files, each applies on its own, and they can be applied together.
  • #16769 bounds the kernel's reduction and definitional-equality caches during replay, whereas this reduces what the reader holds. Both lower leanchecker's peak by different means. They have not been measured together.

Links (pinned to commits):

AI assistance. The patch, the test, the measurement and fuzzing scripts and this text were prepared with Claude (Anthropic).

Impact

Add 👍 to issues you consider important. If others are impacted by this issue, please ask them to add 👍 to it.

Ngôn ngữ chính
Lean
Star
10.3k
Fork
1.1k
Merge trung bình
16 giờ 45 phút
Pull request đã merge (30 ngày)
361

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của leanprover/lean4

Tất cả issue của leanprover/lean4

Issue tương tự

Thêm issue về Backend & API Design

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.