`cbv`: a `match` falling through a `String` pattern costs ~600ms

Open
#16,313 0 comments 0 reactions 0 assignees View on GitHub

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

bug
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

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from leanprover/lean4

All issues in leanprover/lean4

Similar issues

More Compilers issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.