perf: scale Expr.replace
cache with input size
#2151
Merged
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.
When the cache is much bigger than the traversed term, we're spending most of the time with allocating and deallocating it. This change speeds up mathlib4 cumulative CPU time by ~20% and TC inference time by ~32%. In particular,
instantiateLevelParams
falls in one arbitrarily selected test case (Mathlib.Algebra.Order.Ring.Defs
) from 12.7% cumulative time share to 3.4%.Other approaches are conceivable such as doing no caching at all below a certain term depth, but this one does not introduce any new magic numbers and the potential for further savings in this part is unclear.