`cbv`: a `match` falling through a `String` pattern costs ~600ms
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 52/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Active
- Domain
- compilers
Research direction
Start by reading cbv's tryMatcher, dischargeNone, and reduceRecMatcher paths described in the report, then reproduce the timing comparison with the supplied Lean examples. Trace how the later alternative's overlap hypothesis reaches the auxiliary matcher and why whnf unfolds the string literals. Done when the falling-through String case avoids the reported slowdown while the provided cases still evaluate correctly.
Written by the indexing model from the issue text.
Description
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
When cbv evaluates a match whose discriminant does not match a String literal pattern and falls through to a later alternative, it takes about 600ms, compared to about 1ms for the equivalent if. Nat and Char patterns are not affected.
cbv's tryMatcher first rewrites with the match equations using dischargeNone. The equation of a later alternative carries an overlap hypothesis, such as x = "registered_name" → False, which is not discharged,
so cbv falls back to reduceRecMatcher. That reduces the matcher with whnf, which decides the dite inside the auxiliary match declaration by unfolding both string literals into UTF-8 byte arrays.
Steps to Reproduce
def onString : String → Nat
| "registered_name" => 1
| _ => 2
def onNat : Nat → Nat
| 1234567 => 1
| _ => 2
def onChar : Char → Nat
| 'r' => 1
| _ => 2
set_option profiler true
set_option profiler.threshold 1
theorem string_falls : onString "trap_exit" = 2 := by cbv -- ~598ms
theorem nat_falls : onNat 7654321 = 2 := by cbv -- ~6ms
theorem char_falls : onChar 't' = 2 := by cbv -- ~7ms
theorem string_ite : (if "trap_exit" = "registered_name" then 1 else 2) = 2 := by cbv -- ~1ms
When the first alternative matches, for example onString "registered_name" = 1,
cbv uses match_1.eq_1 and takes about 26ms. The same applies when the
literal is nested inside a constructor pattern, such as | .atom "registered_name"
on an inductive with a String field.
Versions
Measured on v4.35.0, v4.36.0-rc1, and nightly-2026-10-17, with the same timings.
- Dominant language
- Lean
- Stars
- 10.3k
- Forks
- 1.1k
- Avg merge
- 19h 8m
- Merged PRs (30d)
- 421
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
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
leanprover/lean4#16681 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
leanprover/lean4#16305 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
leanprover/lean4#16299 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
leanprover/lean4#16257 ·
Maintainers usually reply within 1 day
-
`IO.FS.realPath` reports every failure as `noFileOrDirectory` instead of classifying by `errno`Openbug
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
leanprover/lean4#15683 ·
Maintainers usually reply within 1 day
All issues in leanprover/lean4
Similar issues
-
bug fixed form parser medium priority tokenizer
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
swc-project/swc-next-issues#817 ·
-
Magma mode: div:=, mod:= and the other keyword assignments (join:=, meet:=, diff:=, sdiff:=, and:=, or:=, xor:=) are refusedPossibly taken A pull request linked to this issue is open or already merged. Open
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
sagemathinc/sagebrush#1683 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Maintainers usually reply within 1 day
-
microts: a model local that every branch assigns raises unused_assignments in the generated RustOpenbug
Difficulty 2/5 1-3 hours Newbie friendliness 80/100
pocket-stack/pocketjs#756 ·