Skip to content

perf: increase priority of instSMulOfMul - #34077

Closed
kbuzzard wants to merge 9 commits into
leanprover-community:masterfrom
kbuzzard:kbuzzard-MultoSMul-benchmarking
Closed

kbuzzard wants to merge 9 commits into
leanprover-community:masterfrom
kbuzzard:kbuzzard-MultoSMul-benchmarking

Conversation

@kbuzzard

@kbuzzard kbuzzard commented Jan 17, 2026 •

Copy link
Copy Markdown
Member

#31040 deprecated Mul.toSMul in place of the core instance instSMulOfMul, which is at a lowered priority of 910. This PR undeprecates it and makes it a higher priority instance (1100) which seems better for mathlib.


Open in Gitpod

This PR follows the philosophy seen in Mathlib's Algebra.id : Algebra R R, which also has raised priority. The idea is the same as Algebra.id: if you're looking for an instance of Algebra R S with R not equal to S then Algebra.id is extremely likely to fail quickly in practice, and if you're looking for an instance of Algebra R R then Algebra.id is unambiguously the right answer so should be tried ASAP. However the instance is defined so early in mathlib that in in practice it is tried last unless the priority is raised. The same philosophy holds here; if you're looking for an instance of SMul X X then you absolutely want to try Mul.toSMul first and if you're looking for an instance of SMul X Y with Y not X then Mul.toSMul will in practice be quick to fail.

This PR was inspired by #33908 (another "this should be quick to fail and if it fits then it's almost certainly what we want" prio change) which made an instance of IsScalarTower R A A high priority and gave a performance boost. This PR also gives a performance boost.

Note that the performance in #31040 looks very bad but the radar output is incorrect; there was a hardware change between the two runs. In fact #31040 produced no changes in profiling, as one might expect.

I don't know the correct way to change the priority of a core instance in mathlib, and I didn't know if just changing the priority naively would work repo-wide rather than just file-wide, which was why this PR introduces a second instance.

Zulip discussion at #mathlib4 > priority hacks

@kbuzzard

Copy link
Copy Markdown
Member Author

!radar

@leanprover-radar

leanprover-radar commented Jan 17, 2026 •

Copy link
Copy Markdown

Benchmark results for 2bb2a1e against c584568 are in! @kbuzzard

Notes (1🟥)

  • 🟥 main exited with code 1

No significant changes detected.

@github-actions github-actions Bot added the t-algebra Algebra (groups, rings, fields, etc) label Jan 17, 2026
@github-actions

github-actions Bot commented Jan 17, 2026 •

Copy link
Copy Markdown

PR summary 7e1d43ce0d

Import changes for modified files

No significant changes to the import graph

Import changes for all files
Files Import difference

Declarations diff

+ instance (priority := 1100) Mul.toSMul (α : Type*) [Mul α] : SMul α α := ⟨(· * ·)⟩
- Mul.toSMul

You can run this locally as follows
## summary with just the declaration names:
./scripts/pr_summary/declarations_diff.sh <optional_commit>

## more verbose report:
./scripts/pr_summary/declarations_diff.sh long <optional_commit>

The doc-module for scripts/pr_summary/declarations_diff.sh contains some details about this script.


No changes to technical debt.

This script lives in the mathlib-ci repository. To run it locally, from your mathlib4 directory:

git clone https://github.com/leanprover-community/mathlib-ci.git ../mathlib-ci
../mathlib-ci/scripts/reporting/technical-debt-metrics.sh pr_summary
  • The relative value is the weighted sum of the differences with weight given by the inverse of the current value of the statistic.
  • The absolute value is the relative value divided by the total sum of the inverses of the current values (i.e. the weighted average of the differences).

@kbuzzard

Copy link
Copy Markdown
Member Author

!radar

@leanprover-radar

leanprover-radar commented Jan 17, 2026 •

Copy link
Copy Markdown

Benchmark results for d787386 against c584568 are in! @kbuzzard

Large changes (1✅, 1🟥)

  • 🟥 build/module/Mathlib.RingTheory.Kaehler.Polynomial//instructions: +85.8G (+50.1%)
  • ✅ build/module/Mathlib.RingTheory.TensorProduct.Maps//instructions: -30.5G (-11.5%)

Medium changes (17✅, 1🟥)

  • ✅ build/module/Mathlib.Algebra.DualNumber//instructions: -10.7G (-21.2%)
  • ✅ build/module/Mathlib.Algebra.DualQuaternion//instructions: -1.9G (-3.7%)
  • ✅ build/module/Mathlib.Algebra.Star.Unitary//instructions: -2.9G (-5.7%)
  • ✅ build/module/Mathlib.Analysis.CStarAlgebra.CStarMatrix//instructions: -7.1G (-5.9%)
  • ✅ build/module/Mathlib.Analysis.CStarAlgebra.ContinuousFunctionalCalculus.NonUnital//instructions: -6.8G (-3.6%)
  • ✅ build/module/Mathlib.Analysis.CStarAlgebra.ContinuousFunctionalCalculus.Order//instructions: -3.3G (-2.2%)
  • ✅ build/module/Mathlib.Analysis.CStarAlgebra.ContinuousFunctionalCalculus.Pi//instructions: -1.3G (-5.5%)
  • ✅ build/module/Mathlib.Analysis.CStarAlgebra.Multiplier//instructions: -9.3G (-7.2%)
  • ✅ build/module/Mathlib.Analysis.CStarAlgebra.Unitization//instructions: -2.3G (-4.3%)
  • ✅ build/module/Mathlib.Analysis.Normed.Algebra.Unitization//instructions: -4.0G (-6.7%)
  • ✅ build/module/Mathlib.Analysis.SpecialFunctions.ContinuousFunctionalCalculus.PosPart.Basic//instructions: -3.0G (-3.2%)
  • ✅ build/module/Mathlib.GroupTheory.GroupAction.Jordan//instructions: -4.8G (-7.5%)
  • ✅ build/module/Mathlib.GroupTheory.GroupAction.SubMulAction.OfFixingSubgroup//instructions: -9.8G (-7.5%)
  • ✅ build/module/Mathlib.MeasureTheory.Integral.RieszMarkovKakutani.Basic//instructions: -7.1G (-14.0%)
  • ✅ build/module/Mathlib.MeasureTheory.Integral.RieszMarkovKakutani.NNReal//instructions: -2.9G (-11.3%)
  • ✅ build/module/Mathlib.MeasureTheory.Measure.Haar.Quotient//instructions: -2.8G (-5.6%)
  • ✅ build/module/Mathlib.ModelTheory.Arithmetic.Presburger.Semilinear.Defs//instructions: -9.7G (-18.9%)
  • 🟥 build/module/Mathlib.Topology.Algebra.Module.CharacterSpace//instructions: +1.3G (+6.3%)

Small changes (49✅, 6🟥)

Too many entries to display here. View the full report on radar instead.

@kbuzzard

Copy link
Copy Markdown
Member Author

Build with prio := high gives a saving of 283.6G (0.1%) on build instructions. Now trying again with prio back to 910 as in #31040

@kbuzzard

Copy link
Copy Markdown
Member Author

!radar

@leanprover-radar

leanprover-radar commented Jan 17, 2026 •

Copy link
Copy Markdown

Benchmark results for e87d88d against c584568 are in! @kbuzzard

No significant changes detected.

@kbuzzard

Copy link
Copy Markdown
Member Author

This is what I expected but I am confused about the benchmark results for 31040

@kbuzzard

Copy link
Copy Markdown
Member Author

Is it the deprecation??

@kbuzzard

Copy link
Copy Markdown
Member Author

!radar

@leanprover-radar

leanprover-radar commented Jan 17, 2026 •

Copy link
Copy Markdown

Benchmark results for e6fbe04 against c584568 are in! @kbuzzard

No significant changes detected.

@kbuzzard

Copy link
Copy Markdown
Member Author

Literally revert 31040 by copying the changes back

@kbuzzard

Copy link
Copy Markdown
Member Author

!radar

@leanprover-radar

leanprover-radar commented Jan 17, 2026 •

Copy link
Copy Markdown

Benchmark results for b843961 against c584568 are in! @kbuzzard

No significant changes detected.

Comment thread Mathlib/Algebra/Group/Action/Units.lean Outdated
fail_if_success rfl -- there is an instance diamond here
ext
rfl
u₁ • u₂ = u₁ * u₂ := rfl -- apparently I fixed an instance diamond??

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The diamond was most recently encountered here

@kbuzzard kbuzzard changed the title feat: increase priority of instSMulOfMul perf: increase priority of instSMulOfMul Jan 24, 2026
@kbuzzard

Copy link
Copy Markdown
Member Author

!radar

@leanprover-radar

leanprover-radar commented Jan 24, 2026 •

Copy link
Copy Markdown

Benchmark results for 5310dca against c584568 are in! @kbuzzard

  • ✅ build//instructions: -249.1G (-0.1%)

Large changes (1🟥)

  • 🟥 build/module/Mathlib.RingTheory.Kaehler.Polynomial//instructions: +85.0G (+49.6%)

Medium changes (13✅)

  • ✅ build/module/Mathlib.Algebra.DualNumber//instructions: -10.8G (-21.2%)
  • ✅ build/module/Mathlib.Algebra.DualQuaternion//instructions: -2.0G (-3.8%)
  • ✅ build/module/Mathlib.Analysis.CStarAlgebra.Multiplier//instructions: -9.1G (-7.1%)
  • ✅ build/module/Mathlib.Analysis.Normed.Algebra.Unitization//instructions: -4.0G (-6.8%)
  • ✅ build/module/Mathlib.GroupTheory.CosetCover//instructions: -3.1G (-5.3%)
  • ✅ build/module/Mathlib.GroupTheory.GroupAction.Jordan//instructions: -4.8G (-7.5%)
  • ✅ build/module/Mathlib.GroupTheory.GroupAction.MultiplePrimitivity//instructions: -5.0G (-8.1%)
  • ✅ build/module/Mathlib.GroupTheory.GroupAction.SubMulAction.OfFixingSubgroup//instructions: -9.9G (-7.6%)
  • ✅ build/module/Mathlib.MeasureTheory.Integral.RieszMarkovKakutani.Basic//instructions: -7.1G (-14.0%)
  • ✅ build/module/Mathlib.MeasureTheory.Integral.RieszMarkovKakutani.NNReal//instructions: -2.9G (-11.4%)
  • ✅ build/module/Mathlib.ModelTheory.Arithmetic.Presburger.Semilinear.Defs//instructions: -9.5G (-18.5%)
  • ✅ build/module/Mathlib.RingTheory.PiTensorProduct//instructions: -4.8G (-7.3%)
  • ✅ build/module/Mathlib.RingTheory.TensorProduct.Maps//instructions: -31.1G (-11.8%)

Small changes (55✅, 6🟥)

Too many entries to display here. View the full report on radar instead.

Comment thread Mathlib/Algebra/Group/Action/Units.lean Resolved
@kbuzzard

Copy link
Copy Markdown
Member Author

The bad behaviour of Mathlib.RingTheory.Kaehler.Polynomial should be fixed by #34088 so it might be best to wait on this until #34088 is merged.

@kbuzzard

kbuzzard commented Feb 5, 2026

Copy link
Copy Markdown
Member Author

!radar

@leanprover-radar

leanprover-radar commented Feb 5, 2026 •

Copy link
Copy Markdown

Benchmark results for 9a0e9f5 against c584568 are in! @kbuzzard

  • ✅ build//instructions: -463.2G (-0.24%)

Large changes (1✅)

  • ✅ build/module/Mathlib.RingTheory.Kaehler.Polynomial//instructions: -81.5G (-47.61%)

Medium changes (13✅)

  • ✅ build/module/Mathlib.Algebra.DualNumber//instructions: -10.9G (-21.48%)
  • ✅ build/module/Mathlib.Analysis.CStarAlgebra.ContinuousFunctionalCalculus.Unique//instructions: -5.3G (-4.67%)
  • ✅ build/module/Mathlib.Analysis.CStarAlgebra.Multiplier//instructions: -9.3G (-7.24%)
  • ✅ build/module/Mathlib.Analysis.Normed.Algebra.Unitization//instructions: -4.0G (-6.81%)
  • ✅ build/module/Mathlib.GroupTheory.GroupAction.Jordan//instructions: -4.8G (-7.59%)
  • ✅ build/module/Mathlib.GroupTheory.GroupAction.MultiplePrimitivity//instructions: -5.1G (-8.21%)
  • ✅ build/module/Mathlib.GroupTheory.GroupAction.SubMulAction.OfFixingSubgroup//instructions: -9.9G (-7.59%)
  • ✅ build/module/Mathlib.MeasureTheory.Integral.RieszMarkovKakutani.Basic//instructions: -7.1G (-13.89%)
  • ✅ build/module/Mathlib.MeasureTheory.Integral.RieszMarkovKakutani.NNReal//instructions: -2.9G (-11.42%)
  • ✅ build/module/Mathlib.ModelTheory.Arithmetic.Presburger.Semilinear.Defs//instructions: -9.4G (-18.26%)
  • ✅ build/module/Mathlib.RingTheory.PiTensorProduct//instructions: -4.8G (-7.25%)
  • ✅ build/module/Mathlib.RingTheory.TensorProduct.Maps//instructions: -30.7G (-11.60%)
  • ✅ build/module/Mathlib.Topology.MetricSpace.IsometricSMul//instructions: -5.9G (-13.18%)

Small changes (58✅, 5🟥)

Too many entries to display here. View the full report on radar instead.

fail_if_success rfl -- there is an instance diamond here
ext
rfl
u₁ • u₂ = u₁ * u₂ := rfl

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am assuming that there is now another instance diamond instead?

@eric-wieser eric-wieser Apr 27, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that means that we need to change the LHS to actually pick up the instance we wanted to demonstrate a diamond for.

@plp127 plp127 Apr 28, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing like this? nope this is wrong

Suggested change
u₁ • u₂ = u₁ * u₂ := rfl
(u₁ : M) • u₂ = u₁ * u₂ := by
fail_if_success rfl -- there is an instance diamond here
ext
rfl

@kbuzzard

Copy link
Copy Markdown
Member Author

!radar

@leanprover-radar

leanprover-radar commented Feb 17, 2026 •

Copy link
Copy Markdown

Benchmark results for 9c9ef2c against c584568 are in! @kbuzzard

  • ✅ build//instructions: -290.6G (-0.15%)

Large changes (1🟥)

  • 🟥 build/module/Mathlib.RingTheory.Kaehler.Polynomial//instructions: +85.9G (+50.19%)

Medium changes (13✅)

  • ✅ build/module/Mathlib.Algebra.DualNumber//instructions: -10.7G (-21.19%)
  • ✅ build/module/Mathlib.Analysis.CStarAlgebra.ContinuousFunctionalCalculus.Unique//instructions: -5.4G (-4.76%)
  • ✅ build/module/Mathlib.Analysis.CStarAlgebra.Multiplier//instructions: -9.2G (-7.17%)
  • ✅ build/module/Mathlib.Analysis.Normed.Algebra.Unitization//instructions: -4.0G (-6.74%)
  • ✅ build/module/Mathlib.GroupTheory.GroupAction.Jordan//instructions: -4.8G (-7.55%)
  • ✅ build/module/Mathlib.GroupTheory.GroupAction.MultiplePrimitivity//instructions: -5.1G (-8.14%)
  • ✅ build/module/Mathlib.GroupTheory.GroupAction.SubMulAction.OfFixingSubgroup//instructions: -9.8G (-7.50%)
  • ✅ build/module/Mathlib.MeasureTheory.Integral.RieszMarkovKakutani.Basic//instructions: -7.1G (-13.93%)
  • ✅ build/module/Mathlib.MeasureTheory.Integral.RieszMarkovKakutani.NNReal//instructions: -2.9G (-11.38%)
  • ✅ build/module/Mathlib.ModelTheory.Arithmetic.Presburger.Semilinear.Defs//instructions: -9.6G (-18.76%)
  • ✅ build/module/Mathlib.RingTheory.PiTensorProduct//instructions: -4.9G (-7.40%)
  • ✅ build/module/Mathlib.RingTheory.TensorProduct.Maps//instructions: -30.6G (-11.57%)
  • ✅ build/module/Mathlib.Topology.MetricSpace.IsometricSMul//instructions: -5.6G (-12.52%)

Small changes (57✅, 7🟥)

Too many entries to display here. View the full report on radar instead.

@kbuzzard kbuzzard removed the WIP Work in progress label Feb 17, 2026
@jcommelin jcommelin added the awaiting-zulip There is a Zulip discussion; the author should await and report/implement the decision reached there label Feb 18, 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 Mar 10, 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 Mar 26, 2026
@kbuzzard

Copy link
Copy Markdown
Member Author

Let's see if the phenomenon illustrated by this PR is still present after the v4.29 changes.

!radar

@leanprover-radar

leanprover-radar commented Apr 27, 2026 •

Copy link
Copy Markdown

Benchmark results for 5715a38 against 7e1d43c are in. There are significant results. @kbuzzard

  • ✅ build//instructions: -346.7G (-0.20%)

Medium changes (16✅)

  • ✅ build/module/Mathlib.Algebra.DualNumber//instructions: -11.3G (-26.03%)
  • ✅ build/module/Mathlib.Algebra.Star.Unitary//instructions: -2.2G (-5.86%)
  • ✅ build/module/Mathlib.Analysis.CStarAlgebra.ApproximateUnit//instructions: -4.6G (-4.45%)
  • ✅ build/module/Mathlib.Analysis.CStarAlgebra.Multiplier//instructions: -4.7G (-4.65%)
  • ✅ build/module/Mathlib.Analysis.Convex.SpecificFunctions.Pow//instructions: -1.5G (-12.56%)
  • ✅ build/module/Mathlib.Analysis.Normed.Algebra.Unitization//instructions: -2.5G (-5.34%)
  • ✅ build/module/Mathlib.Analysis.Normed.Module.Ball.Action//instructions: -4.1G (-15.19%)
  • ✅ build/module/Mathlib.GroupTheory.CosetCover//instructions: -2.7G (-6.25%)
  • ✅ build/module/Mathlib.GroupTheory.GroupAction.Jordan//instructions: -4.3G (-8.41%)
  • ✅ build/module/Mathlib.GroupTheory.GroupAction.MultiplePrimitivity//instructions: -4.6G (-10.05%)
  • ✅ build/module/Mathlib.GroupTheory.GroupAction.SubMulAction.OfFixingSubgroup//instructions: -6.5G (-9.01%)
  • ✅ build/module/Mathlib.MeasureTheory.Measure.Haar.Quotient//instructions: -2.6G (-6.76%)
  • ✅ build/module/Mathlib.RingTheory.PiTensorProduct//instructions: -3.4G (-6.59%)
  • ✅ build/module/Mathlib.RingTheory.TensorProduct.Maps//instructions: -23.3G (-12.00%)
  • ✅ build/module/Mathlib.Topology.Algebra.Monoid//instructions: -2.1G (-4.50%)
  • ✅ build/module/Mathlib.Topology.MetricSpace.IsometricSMul//instructions: -4.5G (-11.90%)

Small changes (49✅, 9🟥)

Too many entries to display here. View the full report on radar instead.

@kbuzzard

Copy link
Copy Markdown
Member Author

+awaiting-author

@github-actions github-actions Bot added the awaiting-author Reply -awaiting-author to remove the label on your PR once you have addressed all comments. label Apr 27, 2026
@kbuzzard

kbuzzard commented Apr 27, 2026 •

Copy link
Copy Markdown
Member Author

Another approach suggested by @eric-wieser would be to reuse instSMulOfMul and just additivise it and increase its priority (the priority change turns out to persist across files). Two reasons why this is not ideal:

  1. the docstring currently on Mul.toSMul, namely See also `Monoid.toMulAction` and `MulZeroClass.toSMulWithZero`. cannot be added to instSMulOfMul as this is in core (where these other classes don't exist, so this would not be suitable for a core docstring) and add_decl_doc only works in the same file as the declaration (probably something to do with .oleans?)

  2. If I leave the current docstring as a comment at the point where I increase instance prio, this is not very findable
    (indeed changing the prio of a core instance might lead to confusion in general).

If we do decide to go down this path, then it looks like

/- See also `Monoid.toMulAction` and `MulZeroClass.toSMulWithZero`. -/
attribute [instance 1100, to_additive /-- See also `AddMonoid.toAddAction` -/]
    instSMulOfMul

is the change we should be making to the file in this PR.

@kbuzzard kbuzzard removed awaiting-author Reply -awaiting-author to remove the label on your PR once you have addressed all comments. awaiting-zulip There is a Zulip discussion; the author should await and report/implement the decision reached there labels Apr 27, 2026
@kbuzzard

Copy link
Copy Markdown
Member Author

Currently experimenting on making the change in core so moving this to WIP for now

@kbuzzard kbuzzard added the WIP Work in progress label Apr 28, 2026
@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 Apr 29, 2026
@mathlib-merge-conflicts

Copy link
Copy Markdown

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

@kbuzzard

Copy link
Copy Markdown
Member Author

Closed in favour of #38704 which increases the priority of various related instances as well.

@kbuzzard kbuzzard closed this Apr 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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) WIP Work in progress

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants