LCNF repeats closure setup on every call to a returned function

Aperta
#16,910 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
44/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Ambito
compilers

Direzione di ricerca

Start with the standalone Repro.lean example and run lean -Dtrace.Compiler.result=true -c Repro.c Repro.lean; inspect makeProbe and the specialized List.mapTR.loop in the trace. Compare the generated calls with the expected one-time setup, including the empty-map case. The issue says candidate patch #16908 reduces setup calls from three to one for three keys and keeps them at zero for an empty map.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Prerequisites
  • Checked for an open duplicate.
  • Reduced the issue to a standalone reproducer with no imports.
  • Reproduced on the latest nightly, nightly-2026-11-30.
Description

LCNF can move setup from a function that returns a closure into the closure's body. In this example, each probe recalculates the same index.

Context

#5908 reported the same pattern under the old compiler. The reproducer below shows it in the current LCNF pipeline.

Steps to Reproduce
  1. Save this as Repro.lean.

    @[noinline] def build (rows : List Nat) : Nat := rows.foldl (· + ·) 0
    
    @[noinline] def makeProbe (rows : List Nat) : Nat → Nat :=
      let index := build rows
      fun key => index + key
    
    def probeAll (rows keys : List Nat) : List Nat :=
      let probe := makeProbe rows
      keys.map probe
    
  2. Run lean -Dtrace.Compiler.result=true -c Repro.c Repro.lean.

  3. Inspect makeProbe and the specialized List.mapTR.loop in the trace.

Expected behavior. The first probe calls build once. Later probes reuse the index. An empty map skips setup.

Actual behavior. makeProbe takes both rows and key in the generated code. The specialized loop calls makeProbe rows key for each key, so build folds over rows each time.

Versions

Lean 4.35.0, 4.36.0, 4.38.0-nightly-2026-11-30 (fd149e0), and master at 6079107. Tested on macOS 27.2 (Apple silicon).

Additional Information

Adding -Dtrace.Compiler.init=true shows the two-argument makeProbe in the initial LCNF. Removing @[noinline] from the factory also reproduces the issue.

The candidate patch in #16908 reduces the setup count from three to one for a three-key map and keeps it at zero for an empty map.

Impact

With n rows and m keys, the repeated folds make the setup work grow with n × m. Add a 👍 reaction if this affects your code.

Lingua principale
Lean
Stelle
10.5k
Fork
1.2k
Merge medio
19h 36m
PR unite (30g)
323

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di leanprover/lean4

Tutte le issue di leanprover/lean4

Issue simili

Altre issue su Compilers

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.