Skip to content

first grind +suggestions / try? takes minutes with import Mathlib since v4.35.0-rc2 (#15159) #15392

Closed

Description

@marcelolynch

Prerequisites

Description

Since #15159 (v4.35.0-rc2) the two Sine Qua Non indexes (symbolFrequencyMap and sineQuaNonTriggerMap) are computed on first use in each process. The first query walks the type of every imported theorem twice (foldRelevantConstants, once per index) on one thread. The walk calls getFunInfo on every partial application and isProof on every subterm. As a result, the first grind +suggestions, simp? +suggestions, solve_by_elim +suggestions or try? in a file takes 12 s with import Lean and minutes with import Mathlib. Later queries in the same process are fast.

Context

Mathlib CI: the test mathlib step went from ~100–150 s to ~500–740 s at the bump to v4.35.0-rc2; MathlibTest/LibrarySuggestions/sineQuaNon.lean went from 15 s to ~520 s and MathlibTest/symbolFrequency.lean from 10 s to ~250 s.

The downstream build reports in leanprover/downstream-lean4 show the same step at 44–48 s on every adaptation PR before #15159, at 332 s on the adaptation PR for #15159 (leanprover/downstream-lean4#77), and at 319–333 s on every report after it. The build column did not change.

Steps to Reproduce

  1. Save the file below as SineQuaNonGrind.lean.
  2. lean +leanprover/lean4:v4.35.0-rc1 SineQuaNonGrind.lean
  3. lean +leanprover/lean4:v4.35.0-rc2 SineQuaNonGrind.lean
import Lean

set_library_suggestions Lean.LibrarySuggestions.sineQuaNonSelector

open Lean Elab Tactic in
elab "timed " t:tactic : tactic => do
  let t0 ← IO.monoNanosNow
  evalTactic t
  let t1 ← IO.monoNanosNow
  logInfo m!"{(t1 - t0).toFloat / 1e9} s"

example {x : Dyadic} {prec : Int} : x.roundDown prec ≤ x := by
  fail_if_success grind
  timed grind +suggestions

example {x : Dyadic} {prec : Int} : x.roundDown prec ≤ x := by
  timed grind +suggestions

Expected behavior: the first grind +suggestions takes about as long as the second one (0.1 s on rc1).

Actual behavior: the first grind +suggestions takes 12 s on rc2 and on current master. The second takes 0.08 s.

The following file times the two indexes separately (replace import Lean by import Mathlib for the Mathlib numbers):

import Lean

open Lean LibrarySuggestions SineQuaNon

def timeIt (label : String) (x : CoreM α) (summary : α → String) : CoreM α := do
  let t0 ← IO.monoNanosNow
  let a ← x
  let t1 ← IO.monoNanosNow
  IO.println s!"{label}: {(t1 - t0).toFloat / 1e9} s ({summary a})"
  return a

run_meta do
  IO.println s!"imported constants: {(← getEnv).constants.map₁.size}"
  discard <| timeIt "symbolFrequencyMap (cold)" symbolFrequencyMap
    fun m => s!"{m.size} symbols, Nat ↦ {m.getD `Nat 0}"
  discard <| timeIt "symbolFrequencyMap (warm)" symbolFrequencyMap
    fun m => s!"{m.size} symbols"
  discard <| timeIt "sineQuaNonTheorems (cold)" (sineQuaNonTheorems `HAppend.hAppend)
    fun l => s!"{l.length} theorems"
  discard <| timeIt "sineQuaNonTheorems (warm)" (sineQuaNonTheorems `HAppend.hAppend)
    fun l => s!"{l.length} theorems"

Measured on an Apple M5 Pro (medians of 10 interleaved runs):

v4.35.0-rc1 v4.35.0-rc2
symbolFrequencyMap, cold, import Lean (216k constants) 5.5 ms 6.2 s
sineQuaNonTheorems, cold, import Lean 30 ms 6.1 s
first grind +suggestions in a file with import Lean 0.11 s 12.1 s
second grind +suggestions 0.07 s 0.08 s
symbolFrequencyMap, cold, import Mathlib (792k constants, 304k theorems) – 157–159 s
sineQuaNonTheorems, cold, import Mathlib – 173 s (327 s of CPU and 17.3 GB for the process)

On current master (8dd5d44, local stage1 build, 210k constants) the two cold indexes take 4.8 s and 4.5 s.

Versions

Lean (version 4.35.0-rc2, arm64-apple-darwin24.6.0, commit 11acb17ec6b07a8f9e9173e6845197929540936b, Release), also reproduced with v4.35.0-rc3 and with master at 8dd5d44. Not present in v4.35.0-rc1. macOS 26.4 (Apple M5 Pro, 18 threads).

Additional Information

Profile (samply, rc2, import Lean): the two walks take 6.7 s and 5.7 s. Inside them, getFunInfo on partial applications is 45%, isProof 13%, inferType 22%, and binder and level instantiation 25%. NameMap inserts and the deny-list checks are under 4% together.

Cost of the stored indexes in rc1: 6.30 MB of the 1754 MB of core .olean files (all parts, 0.36%). Their computation at export took about 9–11 s of CPU over the whole core stage1 build (about 0.3% of the module build time, up to 2% for the largest Std.Data.*.Lemmas modules) and about 215 s of CPU over a full Mathlib build (24 ms per module on average, 1.5 s at most). The rc2 release oleans still contain the entries, because the stage0 binary that produced them predates #15159.

#15391 keeps the lazy design and builds both indexes from one parallel traversal that classifies arguments once per application head. With it, the numbers above become 0.49 s / 0.09 s / 0.63 s for import Lean and 8–9 s / 1.5 s (76 s of CPU, 7.6 GB) for import Mathlib. The results are identical.

Impact

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

Activity

  1. changed the title [-]first `grind +suggestions` / `try?` takes 12 s with `import Lean` and minutes with `import Mathlib` since v4.35.0-rc2 (#15159)[/-] [+]first `grind +suggestions` / `try?` takes minutes with `import Mathlib` since v4.35.0-rc2 (#15159)[/+] on Sep 29, 2026
  2. added a commit that references this issue on Oct 3, 2026
    193c358
  3. added a commit that references this issue on Oct 6, 2026
    8607127
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions