perf: persist grind preprocessing caches across calls#12657
Draft
perf: persist grind preprocessing caches across calls#12657
Conversation
This PR persists per-call traversal caches in `markNestedSubsingletons`, `canonImpl`, and `unfoldReducible` across grind preprocessing calls. Previously these steps created fresh HashMaps each invocation, re-traversing all shared subexpressions. For workloads that generate many facts with growing shared structure (e.g. nested Nat subtraction chains), total work was O(n²). Persisting the caches and adding strategic `shareCommon` calls to restore pointer identity reduces `grind canon` time by ~94% and `grind mark subsingleton` time by ~74% on the included benchmark at n=100. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Collaborator
|
!bench |
|
Benchmark results for f81340c against 8f80881 are in! @nomeata
Large changes (3✅)
Medium changes (1✅)
Small changes (11✅, 7🟥)
|
|
Mathlib CI status (docs):
|
Collaborator
|
Reference manual CI status:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR persists per-call traversal caches in
markNestedSubsingletons,canonImpl, andunfoldReducibleacross grind preprocessing calls. Previously these steps created freshHashMaps each invocation, re-traversing all shared subexpressions. For workloads that generate many facts with growing shared structure (e.g. nested Nat subtraction chains), total work was O(n²).Changes:
Grind.State(markSubsingletonCache,unfoldReducibleCache) andCanon.State(visitCache)markNestedSubsingletons/markProof: load/save traversal cache fromStateinstead of starting freshcanonImpl: load/save visit cache fromCanon.Stateinstead of starting freshunfoldReducibleCached: new helper usingMeta.transformWithCachewith persistentExprStructEqcache, replacingSym.unfoldReduciblein bothpreprocessImplandpreprocessLightshareCommoncalls inpreprocessImplto restore pointer identity before cached stepsunfoldReducibleCachedinsidemarkNestedSubsingletons.preprocess(which previously calledSym.unfoldReduciblewith a fresh cache)Benchmark results
On nested
(+ s₂ - s₂)chain at n=100:grind canongrind mark subsingletonScaling analysis
Profiling across problem sizes reveals that the grind core (internalization, congruence closure) still dominates at larger n and scales poorly:
Preprocessing (simp + mark sub + canon) totals ~3.4s at n=600 and scales roughly as O(n^1.5). The grind core accounts for 70s of the 74s total at n=600, scaling approximately as O(n^4). This is a separate issue outside preprocessing.
Test plan
grind_add_sub_cancel_fvar.leanchecks that 4x problem size increase (n=25 → n=100) results in < 10x time increase🤖 Generated with Claude Code