Skip to content

perf(Algebra/*/{InjSurj, TransferInstance}): reduce instance Exprs - #38095

Open
astrainfinita wants to merge 83 commits into
leanprover-community:masterfrom
astrainfinita:FR_delta
Open

astrainfinita wants to merge 83 commits into
leanprover-community:masterfrom
astrainfinita:FR_delta

Conversation

@astrainfinita

@astrainfinita astrainfinita commented Apr 16, 2026 •

Copy link
Copy Markdown
Collaborator

We can not just use fast_instance%, because fast_instance% reduces instances into constructor applications, but their arguments may still not be reduced.

This PR continues the work from #13795.

Original PR: #13795

Zulip

@mathlib-triage mathlib-triage Bot assigned kim-em and unassigned jcommelin May 30, 2026
@Vierkantor Vierkantor self-assigned this Jun 22, 2026
@Vierkantor

Copy link
Copy Markdown
Contributor

Let's get this PR moving again! I see some nice benchmark results, but we'd need to make this pattern useful in general. In other words:

  • Can we have one elaborator that does all the work? (In particular, can the elaborator replace the manual let add := e.add; let ... lines?)
  • Can this elaborator replace fast_instance%? Or maybe exist as an option for fast_instance%?
  • Can you write a docstring for the elaborator explaining when it should be used (and, if the previous question is a "no", when fast_instance% should be used instead)?
  • Can you write comments in the elaborator code explaining why the specific operations (and perhaps in their order) are the solution (and the current form of fast_instance% is not good enough)?

@Vierkantor Vierkantor added the awaiting-author Reply -awaiting-author to remove the label on your PR once you have addressed all comments. label Jun 22, 2026
@mathlib-merge-conflicts

Copy link
Copy Markdown

This pull request has conflicts, please merge master and resolve them.

@mathlib-merge-conflicts mathlib-merge-conflicts Bot added the merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) label Jul 8, 2026
@github-actions github-actions Bot removed the merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) label Aug 6, 2026
@astrainfinita

Copy link
Copy Markdown
Collaborator Author

!bench

@leanprover-radar

leanprover-radar commented Aug 6, 2026 •

Copy link
Copy Markdown

Benchmark results for 12de4b2 against 046187e are in. There are significant results. @astrainfinita

  • ✅ build//instructions: -23.9G (-0.02%)

Large changes (5🟥)

  • 🟥 build/module/Mathlib.Algebra.Field.Basic//instructions: +10.5G (+59.49%)
  • 🟥 build/module/Mathlib.Algebra.Field.TransferInstance//instructions: +5.9G (+121.17%)
  • 🟥 build/module/Mathlib.Algebra.Group.InjSurj//instructions: +6.4G (+42.38%)
  • 🟥 build/module/Mathlib.Algebra.Ring.InjSurj//instructions: +21.5G (+89.88%)
  • 🟥 build/module/Mathlib.Algebra.Ring.TransferInstance//instructions: +7.8G (+88.87%)

Medium changes (1✅, 4🟥)

  • ✅ build/module/Mathlib.Algebra.Category.ModuleCat.Sheaf.PushforwardContinuous//instructions: -15.9G (-12.79%)
  • 🟥 build/module/Mathlib.Algebra.GroupWithZero.InjSurj//instructions: +2.9G (+42.50%)
  • 🟥 build/module/Mathlib.Algebra.Module.RingHom//instructions: +1.2G (+26.53%)
  • 🟥 build/module/Mathlib.LinearAlgebra.Multilinear.DirectSum//instructions: +3.9G (+14.92%)
  • 🟥 build/module/Mathlib.LinearAlgebra.PiTensorProduct.DirectSum//instructions: +4.8G (+12.94%)

Small changes (16✅, 5🟥)

  • ✅ build/module/Mathlib.Algebra.Category.AlgCat.Limits//instructions: -1.6G (-4.86%)
  • ✅ build/module/Mathlib.Algebra.Category.Grp.Limits//instructions: -1.9G (-4.81%)
  • ✅ build/module/Mathlib.Algebra.Category.ModuleCat.Monoidal.Adjunction//instructions: -1.5G (-5.13%)
  • ✅ build/module/Mathlib.Algebra.Category.ModuleCat.Presheaf.Pushforward//instructions: -2.6G (-6.45%)
  • ✅ build/module/Mathlib.Algebra.Category.ModuleCat.Sheaf.PullbackContinuous//instructions: -1.7G (-4.21%)
  • 🟥 build/module/Mathlib.Algebra.Category.ModuleCat.Sheaf.Quasicoherent//instructions: +1.8G (+1.64%)
  • ✅ build/module/Mathlib.Algebra.Category.Ring.Limits//instructions: -2.1G (-2.94%)
  • 🟥 build/module/Mathlib.Algebra.DirectSum.Module//instructions: +1.7G (+4.47%)
  • 🟥 build/module/Mathlib.Algebra.Group.TransferInstance//instructions: +1.1G (+14.65%)
  • ✅ build/module/Mathlib.Algebra.Ring.ULift//instructions: -345.1M (-3.35%)
  • ✅ build/module/Mathlib.Analysis.CStarAlgebra.ApproximateUnit//instructions: -1.6G (-2.85%)
  • ✅ build/module/Mathlib.Analysis.CStarAlgebra.Matrix//instructions: -4.6G (-6.83%)
  • ✅ build/module/Mathlib.Analysis.Distribution.TestFunction//instructions: -2.8G (-2.53%)
  • ✅ build/module/Mathlib.CategoryTheory.Sites.SheafCohomology.MayerVietoris//instructions: -3.0G (-4.91%)
  • ✅ build/module/Mathlib.LinearAlgebra.Matrix.WithConv//instructions: -1.3G (-6.60%)
  • ✅ build/module/Mathlib.NumberTheory.NumberField.Completion.InfinitePlace//instructions: -1.7G (-5.55%)
  • ✅ build/module/Mathlib.NumberTheory.NumberField.Ideal.Asymptotics//instructions: -2.0G (-6.80%)
  • ✅ build/module/Mathlib.RingTheory.AdjoinRoot//instructions: -3.8G (-4.07%)
  • 🟥 build/module/Mathlib.RingTheory.Coalgebra.MonoidAlgebra//instructions: +1.0G (+10.72%)
  • ✅ build/module/Mathlib.RingTheory.Morita.Matrix//instructions: -3.9G (-5.15%)
  • and 1 more

@astrainfinita

astrainfinita commented Aug 6, 2026 •

Copy link
Copy Markdown
Collaborator Author

I tried reducing at .instances transparency to replace the manual lets, but the result was not satisfactory. (edit: It may also be that changes in Lean like .instances transparency have made this PR less effective than before. I’ll experiment with it some more.)

@astrainfinita

Copy link
Copy Markdown
Collaborator Author

!bench

@leanprover-radar

leanprover-radar commented Aug 7, 2026 •

Copy link
Copy Markdown

Benchmark results for 39cac35 against 046187e are in. There are significant results. @astrainfinita

  • ✅ build//instructions: -55.3G (-0.04%)

Medium changes (1🟥)

  • 🟥 build/module/Mathlib.Algebra.Group.InjSurj//instructions: +4.4G (+29.55%) (reduced significance based on absolute threshold)

Small changes (15✅, 5🟥)

  • ✅ build/module/Mathlib.Algebra.Category.AlgCat.Limits//instructions: -1.4G (-4.25%)
  • ✅ build/module/Mathlib.Algebra.Category.Grp.Limits//instructions: -1.3G (-3.37%)
  • ✅ build/module/Mathlib.Algebra.Category.ModuleCat.Sheaf.PushforwardContinuous//instructions: -7.4G (-5.98%)
  • ✅ build/module/Mathlib.Algebra.Category.Ring.Limits//instructions: -1.9G (-2.69%)
  • ✅ build/module/Mathlib.Algebra.Field.Subfield.Defs//instructions: -985.5M (-7.29%)
  • 🟥 build/module/Mathlib.Algebra.Field.TransferInstance//instructions: +801.0M (+16.52%)
  • 🟥 build/module/Mathlib.Algebra.Group.TransferInstance//instructions: +943.9M (+12.07%)
  • 🟥 build/module/Mathlib.Algebra.Ring.InjSurj//instructions: +2.7G (+11.22%) (reduced significance based on *//lines)
  • 🟥 build/module/Mathlib.Algebra.Ring.TransferInstance//instructions: +2.2G (+25.41%) (reduced significance based on *//lines)
  • ✅ build/module/Mathlib.Algebra.Ring.ULift//instructions: -326.8M (-3.17%)
  • ✅ build/module/Mathlib.Analysis.CStarAlgebra.Matrix//instructions: -3.3G (-4.94%)
  • ✅ build/module/Mathlib.CategoryTheory.Sites.SheafCohomology.MayerVietoris//instructions: -2.5G (-4.17%)
  • ✅ build/module/Mathlib.LinearAlgebra.Multilinear.DirectSum//instructions: -2.2G (-8.51%)
  • ✅ build/module/Mathlib.LinearAlgebra.PiTensorProduct.DirectSum//instructions: -2.6G (-7.01%)
  • ✅ build/module/Mathlib.NumberTheory.NumberField.Completion.InfinitePlace//instructions: -1.5G (-4.97%)
  • ✅ build/module/Mathlib.NumberTheory.NumberField.Ideal.Asymptotics//instructions: -1.4G (-4.79%)
  • ✅ build/module/Mathlib.RingTheory.AdicCompletion.AsTensorProduct//instructions: -1.9G (-4.53%)
  • ✅ build/module/Mathlib.RingTheory.WittVector.FrobeniusFractionField//instructions: -1.0G (-5.23%)
  • ✅ build/module/Mathlib.RingTheory.WittVector.Isocrystal//instructions: -1.3G (-5.09%)
  • 🟥 build/module/Mathlib.Util.TermReduce//instructions: +338.7M (+9.89%)

@astrainfinita

Copy link
Copy Markdown
Collaborator Author

!bench

@leanprover-radar

leanprover-radar commented Aug 7, 2026 •

Copy link
Copy Markdown

Benchmark results for bfc5dea against 046187e are in. There are significant results. @astrainfinita

  • ✅ build//instructions: -69.2G (-0.05%)

Large changes (3🟥)

  • 🟥 build/module/Mathlib.Algebra.Group.InjSurj//instructions: +5.5G (+36.67%)
  • 🟥 build/module/Mathlib.Algebra.Ring.InjSurj//instructions: +10.0G (+41.95%)
  • 🟥 build/module/Mathlib.Algebra.Ring.TransferInstance//instructions: +5.3G (+60.80%)

Medium changes (2🟥)

  • 🟥 build/module/Mathlib.Algebra.Field.Basic//instructions: +3.6G (+20.41%)
  • 🟥 build/module/Mathlib.Algebra.Field.TransferInstance//instructions: +2.6G (+53.83%)

Small changes (16✅, 2🟥)

  • ✅ build/module/Mathlib.Algebra.Category.Grp.Limits//instructions: -1.4G (-3.55%)
  • ✅ build/module/Mathlib.Algebra.Category.ModuleCat.Presheaf.Pushforward//instructions: -2.1G (-5.15%)
  • ✅ build/module/Mathlib.Algebra.Category.ModuleCat.Sheaf.Colimits//instructions: -1.5G (-8.69%)
  • ✅ build/module/Mathlib.Algebra.Category.ModuleCat.Sheaf.PullbackContinuous//instructions: -2.5G (-6.15%)
  • ✅ build/module/Mathlib.Algebra.Category.ModuleCat.Sheaf.PushforwardContinuous//instructions: -5.2G (-4.22%)
  • ✅ build/module/Mathlib.Algebra.Category.Ring.Limits//instructions: -1.8G (-2.50%)
  • 🟥 build/module/Mathlib.Algebra.Group.TransferInstance//instructions: +1.1G (+14.42%)
  • 🟥 build/module/Mathlib.Algebra.GroupWithZero.InjSurj//instructions: +967.1M (+14.40%)
  • ✅ build/module/Mathlib.Algebra.Ring.ULift//instructions: -367.0M (-3.56%)
  • ✅ build/module/Mathlib.Analysis.CStarAlgebra.Matrix//instructions: -3.3G (-4.95%)
  • ✅ build/module/Mathlib.CategoryTheory.Sites.SheafCohomology.MayerVietoris//instructions: -2.7G (-4.45%)
  • ✅ build/module/Mathlib.LinearAlgebra.Multilinear.DirectSum//instructions: -3.2G (-12.35%)
  • ✅ build/module/Mathlib.LinearAlgebra.PiTensorProduct.DirectSum//instructions: -3.7G (-10.00%)
  • ✅ build/module/Mathlib.NumberTheory.NumberField.CanonicalEmbedding.Basic//instructions: -2.5G (-2.65%)
  • ✅ build/module/Mathlib.NumberTheory.NumberField.Ideal.Asymptotics//instructions: -1.6G (-5.38%)
  • ✅ build/module/Mathlib.RingTheory.AdicCompletion.AsTensorProduct//instructions: -4.0G (-9.51%)
  • ✅ build/module/Mathlib.RingTheory.AdicCompletion.Completeness//instructions: -2.0G (-6.08%)
  • ✅ build/module/Mathlib.RingTheory.ClassGroup.Basic//instructions: -1.8G (-3.59%)

@mathlib-merge-conflicts mathlib-merge-conflicts Bot added the merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) label Aug 11, 2026
@mathlib-merge-conflicts

Copy link
Copy Markdown

This pull request has conflicts, please merge master and resolve them.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting-author Reply -awaiting-author to remove the label on your PR once you have addressed all comments. merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) t-algebra Algebra (groups, rings, fields, etc)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants