Kernel reduction of `Array.ofFn` is quadratic in its size
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
- 47/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Lĩnh vực
- performance
Hướng nghiên cứu
Start with the issue’s minimal Lean examples for Array.ofFn, Array.push, and the faster List.ofFn path, then read Init/Data/Array/OfFn.lean and the related lemmas mentioned in the issue. Compare kernel reduction of the array and list definitions at increasing sizes. Done when the kernel cost for Array.ofFn grows linearly while the existing examples and lemmas continue to work.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Prerequisites
- Check that your issue is not already filed:
https://github.com/leanprover/lean4/issues
#16012 (fixed by #16113) is the same cost forString.toList; #14989 madeArray.ofFnreduce in the kernel at all. I found no report of its cost. - Reduce the issue to a minimal, self-contained, reproducible test case.
Avoid dependencies to Mathlib or Batteries. - Test your test case against the latest nightly release, for example on
https://live.lean-lang.org/#project=lean-nightly
Tested onnightly-2026-11-26,v4.37.0-rc1andv4.36.0, with the same results. - When reporting a perceived soundness bug: Does it affect
lake check --paranoid?
This is not a soundness bug.
Description
When the kernel reduces Array.ofFn f for f : Fin n → α, it takes time and memory quadratic in n. The same array built from List.ofFn f takes almost no time.
The cause is the one #16012 describes for String.toList. Array.ofFn.go calls Array.push once per element, and in the kernel Array.push a v is List.concat a.toList v. The result is n nested List.concat calls. Each step of Array.size, a[i] or a comparison that walks the list must reduce through all of them again.
Array.range n is Array.ofFn fun i => i, and Vector.ofFn delegates to Array.ofFn, so both inherit the cost.
Context
We hit this with decide +kernel on a function that reads a table of 1024 entries built with Vector.ofFn. One evaluation of the function took about 16 s in the kernel, most of it in the table. Building the same table as ((List.range 1024).map f).toArray brought it to about 2 s.
Steps to Reproduce
-- About 13 s and 5 GB on nightly-2026-11-26.
theorem viaArrayOfFn : (Array.ofFn (n := 1024) fun i => i.val).size = 1024 := by
decide +kernel
-- About 0.1 s.
theorem viaListOfFn : (List.ofFn (n := 1024) fun i => i.val).toArray.size = 1024 := by
decide +kernel
Run each theorem in its own file with lean. Wall time and peak memory of viaArrayOfFn as n grows (each includes about 0.1 s of startup):
n |
time | memory |
|---|---|---|
| 256 | 0.73 s | 0.7 GB |
| 512 | 3.0 s | 1.6 GB |
| 1024 | 13.0 s | 5.0 GB |
A plain loop of Array.push costs the same, so the cost comes from push and not from the index arithmetic in Array.ofFn.go:
def pushN : Nat → Array Nat → Array Nat
| 0, a => a
| k + 1, a => pushN k (a.push k)
-- About 13 s and 5 GB.
theorem viaPush : (pushN 1024 #[])[3]! = 1020 := by decide +kernel
Expected behavior: reducing Array.ofFn f in the kernel takes time linear in n, as List.ofFn f does.
Actual behavior: time and memory grow quadratically in n.
Versions
Lean (version 4.38.0-nightly-2026-11-26, x86_64-unknown-linux-gnu, commit e1228b8b45918ece1a00d68e5fc5e6a685ca59ee, Release)
Ubuntu Linux, x86_64.
Additional Information
One possible fix follows #16113: give Array.ofFn the logical definition ⟨List.ofFn f⟩, which the kernel reduces in linear time, and keep the current loop of push as the compiled implementation. The lemmas in Init/Data/Array/OfFn.lean and Lemmas.lean that unfold Array.ofFn.go would then follow from the List.ofFn lemmas. I have not tried this change.
Impact
Add 👍 to issues you consider important. If others are impacted by this issue, please ask them to add 👍 to it.
🤖 Generated with Claude Code
- 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
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của leanprover/lean4
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
leanprover/lean4#16876 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
leanprover/lean4#16842 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
leanprover/lean4#16841 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 82/100
leanprover/lean4#16840 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
doc: stray "public" in Bool and HEq documentationCó thể làm lại được Pull request cho issue này đã bị đóng mà không được merge. Đang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 72/100
leanprover/lean4#16825 ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của leanprover/lean4
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
MPCoreDeveloper/SharpCoreDB#531 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 2 ngày
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
metasequoiaime/msime#12003 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
SessionStart boots the CLI a third time when `memsearch` is a mise shim (the dist-info version read fails)Có thể đã có người làm Có pull request liên kết đang mở hoặc đã được merge. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
zilliztech/memsearch#893 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
perf: the Evaluations leaderboard and the @ model menus download every model image at onceCó thể đã có người làm Có pull request liên kết đang mở hoặc đã được merge. Đang mởbug confirmed issue
Độ khó 2/5 Nửa ngày Mức phù hợp với người mới 78/100
open-webui/open-webui#35213 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày