lean4 icon indicating copy to clipboard operation
lean4 copied to clipboard

perf: short-circuit Sym pattern matching on pointer equality

Open sgraf812 opened this issue 5 months ago • 2 comments

This PR adds a pointer-equality fast path to the Sym pattern matcher: when a pattern subterm is pointer-equal to the target, it is a closed term equal to the target with no variables to bind, so matching succeeds immediately without traversing the subterm.

On its own this fires rarely, because patterns are built in a different hash-cons context than the targets. It pays off once patterns are internalized into the target's share table, as the backward-rule patterns (#14134) and the spec patterns (#14142) now are: their shared instance prefixes then match by pointer in constant time instead of being walked structurally.

The short-circuit fires overwhelmingly on the typeclass-instance telescopes of the monad operations. Once a spec pattern is internalized, the entire @MonadStateOf.get σ m inst application becomes pointer-shared with the program's get, so one pointer comparison replaces walking the whole instance argument. The saving per hit grows with monad-transformer depth: about 65 expression nodes skipped per get in a ReaderT/StateT stack, about 360 in a deeper ExceptT-over-StateT stack, which is why the deepest benchmark gains the most. The ReaderT/ExceptT transformer constructors inside those telescopes account for most of the remaining hits.

Benchmark wall-clock for the mvcgen' step (tests/bench/mvcgen/sym, single-threaded, min of 5), against the current master and with this PR on top:

case master + this PR
ReaderState(1000) 170 ms 151 ms
AddSubCancelDeep(700) 144 ms 118 ms
GetThrowSet(600) 119 ms 109 ms
MatchIota(150) 33 ms 30 ms

sgraf812 avatar Jun 22 '26 06:06 sgraf812

Mathlib CI status (docs):

  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase 1df6f699630747c449151cd8f05eb7da60e29864 --onto 75591856794182559e5a3713bdf8aa00c52fc7ca. You can force Mathlib CI using the force-mathlib-ci label. (2026-06-22 06:41:58)
  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase 2e72131c7f007ab9d2538b676106c5141b24ba22 --onto 75591856794182559e5a3713bdf8aa00c52fc7ca. You can force Mathlib CI using the force-mathlib-ci label. (2026-06-22 09:38:12)
  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase 38b4c63a85c3d4f7e0e109975af74bce37278bb5 --onto 2e72131c7f007ab9d2538b676106c5141b24ba22. You can force Mathlib CI using the force-mathlib-ci label. (2026-06-22 17:00:51)

Reference manual CI status:

  • ❗ Reference manual CI will not be attempted unless your PR branches off the nightly-with-manual branch. Try git rebase 1df6f699630747c449151cd8f05eb7da60e29864 --onto 803553a556fd82fa1060efb0c43eda542130cb16. You can force reference manual CI using the force-manual-ci label. (2026-06-22 06:42:00)
  • ❗ Reference manual CI will not be attempted unless your PR branches off the nightly-with-manual branch. Try git rebase 2e72131c7f007ab9d2538b676106c5141b24ba22 --onto 803553a556fd82fa1060efb0c43eda542130cb16. You can force reference manual CI using the force-manual-ci label. (2026-06-22 09:38:14)
  • ❗ Reference manual CI will not be attempted unless your PR branches off the nightly-with-manual branch. Try git rebase 38b4c63a85c3d4f7e0e109975af74bce37278bb5 --onto 803553a556fd82fa1060efb0c43eda542130cb16. You can force reference manual CI using the force-manual-ci label. (2026-06-22 17:00:53)

leanprover-bot avatar Jun 22 '26 06:06 leanprover-bot