`getFunInfo` takes exponential time when a parameter type has shared subterms
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 48/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Domain
- performance
Research direction
Start with Lean/Meta/FunInfo.lean at collectDeps, then read Lean/Util/CollectFVars.lean and the getFunInfo call in Lean/Meta/ExprDefEq.lean. Run the supplied tower% reproducer with increasing depths and compare runtimes. Done when the traversal scales with distinct DAG nodes and the reproducer no longer shows exponential growth.
Written by the indexing model from the issue text.
Description
🤖 Claude says:
Prerequisites
- Check that your issue is not already filed:
https://github.com/leanprover/lean4/issues - 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
(You can also use the settings there to switch to “Lean nightly”) - When reporting a perceived soundness bug: Does it affect
lake check --paranoid?
If in doubt, please reach out on [email protected] for responsible disclosure.
Description
collectDeps in Lean.Meta.FunInfo walks a parameter type as a tree, with no visited set. When
that type is small as a DAG but large as a tree (as produced by chains of letI/haveI, which
substitute their values), getFunInfo takes time exponential in the sharing depth. isDefEqArgs
calls getFunInfoNArgs on any application head, including local functions, so ordinary
unification hits this.
Context
Found in TauCeti (Mathlib-based), in the Hamiltonian reduction-in-stages modules
(TauCeti/Geometry/Symplectic/Hamiltonian/Reduction/Stages/{ClosedNormal,ReducedSpace,FixedValue}).
The types of the two-forms there carry long letI/haveI instance towers: 8.7k to 47.6k distinct
Expr nodes, over 10^8 as trees. These modules took 668 s, 1047 s and 1229 s. perf on one
statement puts 97% of samples in Lean.Meta.collectDeps.visit under isDefEqArgs. The slow path
is reached because the module system hides the bodies of two definitions, so isDefEq compares
Submodule coercion projections instead of unfolding, and calls getFunInfo on non-constant heads
whose types contain the towers. import all of the two defining modules brings the files to
143 to 234 s; exposing the bodies brings them to 48 to 254 s.
Steps to Reproduce
/-- `tower% n f x` is a perfect binary tree of applications of `f`, of depth `n`, over `x`.
`letI` substitutes its value, so the term has `O(n)` distinct subterms but `2 ^ n - 1`
applications of `f` when read as a tree. -/
syntax "tower% " num term:max term:max : term
macro_rules
| `(tower% $n $f $x) => do
let mut body ← `(a)
for _ in [:n.getNat] do
body ← `(letI a := $f a a; $body)
`(letI a := $x; $body)
theorem t (f : Nat → Nat → Nat) (g : (x : Nat) → tower% 26 f x = x → Nat)
(x : Nat) (h₁ h₂ : tower% 26 f x = x) : g x h₁ = g x h₂ := rfl
Run lean on the file and vary 26. Wall time (best of two runs, lean on the command line,
v4.37.0-rc1, loaded 96-core Linux host):
n |
time |
|---|---|
| 20 | 0.33 s |
| 22 | 0.44 s |
| 24 | 0.88 s |
| 25 | 1.48 s |
| 26 | 2.70 s |
| 27 | 5.19 s |
| 28 | 9.80 s |
On nightly-2026-11-26: 0.44 s at n = 22, 0.96 s at n = 24, 2.70 s at n = 26, 5.12 s at n = 27.
perf at n = 26 (whole process): the top self-time symbols are Lean.Meta.collectDeps.visit
(37.5%), lean_expr_eqv (18%) and Array.idxOf? specialised for collectDeps.visit (15%, the
linear fvars.idxOf? lookup at each free variable occurrence).
Expected behavior: time linear in the size of the term as a DAG.
Actual behavior: time doubles with each level of sharing.
Versions
Lean (version 4.37.0-rc1) and Lean (version 4.38.0-nightly-2026-11-26, commit e1228b8b45),
x86_64 Linux. The code is unchanged on master (5e756ba, 1 Dec 2026).
Additional Information
private def collectDeps (fvars : Array Expr) (e : Expr) : Array Nat :=
let rec visit (e : Expr) (deps : Array Nat) : Array Nat :=
match e with
| .app f a => whenHasVar e deps (visit a ∘ visit f)
...
The only pruning is hasFVar, and a parameter type always contains the earlier telescope
variables, so every shared subterm is revisited once per path to it. It is called for each
parameter type and for the result type
(https://github.com/leanprover/lean4/blob/5e756ba2526bd88b18fa8b01588964fe848e0f91/src/Lean/Meta/FunInfo.lean#L85
and #L133), and reached from isDefEqArgs
(https://github.com/leanprover/lean4/blob/5e756ba2526bd88b18fa8b01588964fe848e0f91/src/Lean/Meta/ExprDefEq.lean#L478).
The decl.type.find? on L88 has the same shape, but only runs when there are higher-order
out-params.
Suggested fix: compute the dependencies with collectFVars (which already keeps a pointer-keyed
visited set, Lean/Util/CollectFVars.lean) and map the resulting FVarIds to indices, or add a
pointer-keyed visited set to visit. This is the approach of #15639 for
SynthInstance.removeUnusedArguments?.
The same pattern in linter.unusedSectionVars is already reported in the comments on #15638:
https://github.com/leanprover/lean4/blob/5e756ba2526bd88b18fa8b01588964fe848e0f91/src/Lean/Elab/MutualDef.lean#L565-L571
calls header.type.containsFVar v || val.containsFVar v once per automatically included section
variable, and Expr.hasAnyFVar has no visited set either. Each unused variable costs a full tree
walk. A reproducer in the same style:
-- `tower%` as above
class C (i : Nat) (α : Type) where
variable {α : Type} [C 0 α] [C 1 α] [C 2 α] [C 3 α] [C 4 α] [C 5 α] [C 6 α] [C 7 α] [C 8 α] [C 9 α]
(f : α → α → α) (x : α)
theorem t (_h : tower% 26 f x = x) : True := trivial
n |
linter on | -Dlinter.unusedSectionVars=false |
|---|---|---|
| 20 | 0.49 s | 0.28 s |
| 22 | 1.13 s | 0.28 s |
| 24 | 4.72 s | 0.23 s |
| 25 | 6.66 s | 0.34 s |
| 26 | 12.84 s | 0.24 s |
| 27 | 24.34 s | 0.21 s |
| 28 | 48.21 s | 0.28 s |
On nightly-2026-11-26 (linter on / off): 1.03 s / 0.31 s at n = 22, 3.45 s / 0.29 s at n = 24, 13.07 s / 0.31 s at n = 26, 29.16 s / 0.24 s at n = 27.
Time is also linear in the number of instance variables (at n = 24: 0.70 s with one, 2.03 s
with five, 6.49 s with twenty). In TauCeti, after the collectDeps path was avoided, perf put
81% of ReducedSpace in containsFVar from this linter; turning it off takes ReducedSpace from
132 s to 17 s, FixedValue from 106 s to 25 s and ClosedNormal from 48 s to 21 s. Most of the
costly variables are anonymous instance binders, so include/omit does not help. Collecting the
free variables of header.type and val once with collectFVars and testing membership per
variable would make this linear.
Impact
Add 👍 to issues you consider important. If others are impacted by this issue, please ask them to add 👍 to it.
- Dominant language
- Lean
- Stars
- 10.5k
- Forks
- 1.2k
- Avg merge
- 19h 36m
- Merged PRs (30d)
- 323
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from leanprover/lean4
-
P-medium
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
leanprover/lean4#16958 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
leanprover/lean4#16897 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
leanprover/lean4#16892 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 1-3 hours Newbie friendliness 86/100
leanprover/lean4#16886 · 1 comment ·
Maintainers usually reply within 1 day
-
bug
Difficulty 1/5 Under an hour Newbie friendliness 78/100
leanprover/lean4#16820 ·
Maintainers usually reply within 1 day
All issues in leanprover/lean4
Similar issues
-
bot-triaged module: inductor module: performance oncall: pt2 triaged
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
Maintainers usually reply within 1 day
-
area: ui bug priority: low
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
tetsuji16/microProject#1893 ·
Maintainers usually reply within 1 day
-
A JSON chat answer blocks the backend event loop for the whole RAG runPossibly taken @NopeCharon claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
The-OpenROAD-Project/ORAssistant#656 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
domattioli/agents-inc#257 ·
Maintainers usually reply within 1 day
-
Trial runs the agent's `populate_context_post_run` on the event loop, so trials that end together stall the orchestrator for secondsPossibly taken A pull request linked to this issue is open or already merged. Open
Difficulty 2/5 1-3 hours Newbie friendliness 77/100
harbor-framework/harbor#4652 ·
Maintainers usually reply within 1 day